首页 > 其他分享 >第一次实验

第一次实验

时间:2022-09-29 23:00:55浏览次数:44  
标签:const int double 第一次 width length 实验 Rectangle

实验任务二

#include<iostream>
using namespace std;
class Point
{
public:
    Point(int x0 = 0, int y0 = 0);
    Point(const Point& p);
    ~Point() = default;
    int getX() const { return x; }
    int getY() const { return y; }
    void show() const;
private:
    int x, y;
};
//构造函数(带有默认函数值)
Point::Point(int x0, int y0) :x{ x0 }, y{ y0 }
{
    cout << "constructor called." << endl;
}
//复制构造函数
Point::Point(const Point& p) :x{ p.x }, y{ p.y }
{
    cout << "copy constructor called" << endl;
}
void Point::show() const
{
    cout << "(" << x << "." << y << ")" << endl;
}
int main()
{
    Point p1(3,9);
    p1.show();

    Point p2 = p1;
    p2.show();

    Point p3{ p2 };
    p3.show();
    cout << p3.getX() << endl;

}

 

 

 

 

实验任务三

#include<iostream>
#include<iomanip>
using namespace std;
class Clock
{
public:
    Clock(int h = 0, int m = 0, int s = 0);
    Clock(const Clock& t);
    ~Clock() = default;

    void setTime(int h, int m = 0, int s = 0);
    void showTime() const;

private:
    int hour, minute, second;
};
Clock::Clock(int hour, int minute, int second)
{
    this->hour = hour;
    this->minute = minute;
    this->second = second;
    cout << "constructor called" << endl;
}
Clock::Clock(const Clock& t)
{
    this->hour = t.hour;
    this->minute = t.minute;
    this->second = t.second;
    cout << "copy constructor called" << endl;
}
void Clock::setTime(int hour, int minute, int second)
{
    this->hour = hour;
    this->minute = minute;
    this->second = second;
}

void Clock::showTime() const
{
    cout << setfill('0') << setw(2) << hour
        << ":" << setw(2) << minute
        << ":" << setw(2) << second << endl;
}
Clock reset()
{
    return Clock(0, 0, 0);
}
int main()
{
    Clock c1(13, 14, 0);
    c1.showTime();

    c1 = reset();
    c1.showTime();

    Clock c2(c1);
    c2.setTime(13);
    c2.showTime();

}

 

 

 

 实验任务四

#include<iostream>
using namespace std;
class X
{
public:
    X();
    ~X();
    X(int data);
    X(const X& obj);
    X(X&& obj) noexcept;
    void show()const;

private:
    int data;


};
X::X()
{
    data = 42;
    cout << "default constructor called." << endl;
}
X::~X()
{
    cout << "destructor called." << endl;
}
X::X(int data)
{
    this->data = data;
    cout << "constructor called."<<endl;
}
X::X(const X& obj)
{
    this->data = obj.data;
    cout << "copy constructor called."<<endl;
}
X::X(X&& obj) noexcept
{
    this->data = obj.data;
    cout << "move constructor called."<<endl;
}
void X::show() const
{
    cout << data << endl;
}
int main()
{
    X x1; //默认构造函数被编译器自动调用
    x1.show();
    X x2{2049};
    x2.show(); // 构造函数被编译器自动调用
    X x3{x1}; // 复制构造函数被编译器自动调用
    x3.show();
    X x4{ move(x2) }; // 移动构造函数被编译器调用
    x4.show();
}

 

 

 实验分析

* line48中的x1被定义时,调用默认构造函数
* line50中的x2被定义时,调用带参数的构造函数
* line52中的x3被定义时,调用复制构造函数
* line54中的x4被定义时,调用移动构造函数
* 代码结束时,析构函数被调用x1,x2,x3,x4被逐一清理

实验任务五

#include<iostream>
#include<iomanip>
using namespace std;
class Rectangle
{
public:
    Rectangle();
    Rectangle(double l,double w);
    double len() const;
    double wide() const;
    Rectangle(const Rectangle& p);
    double area()const;
    double circumference()const;
    void resize(double times);
    void resize(double l_times, double w_times);
private:
    double length, width;
};
Rectangle::Rectangle()
{
    length = 2.0;
    width = 1.0;
}
Rectangle::Rectangle(double l, double w)
{
    length = l;
    width = w;
}
Rectangle::Rectangle(const Rectangle& p)
{
    length = p.length;
    width = p.width;
}
double Rectangle::len()const
{
    return length;
}
double Rectangle::wide() const
{
    return width;
}
double Rectangle::area() const
{
    return length * width;
}
double Rectangle::circumference()const
{
    return 2 * (length + width);
}
void Rectangle::resize(double times)
{
    length = length * times;
    width = width * times;
}
void Rectangle::resize(double l_times, double w_times)
{
    length = length * l_times;
    width = width * w_times;
}
void output(const Rectangle& rect)
{

    cout << "矩形信息: \n"; cout << fixed << setprecision(2)
        <<left<<setw(10)<<"长:"<< rect.len() << endl
        << left << setw(10) << "宽:"<<rect.wide() << endl
        << left << setw(10) << "面积:"<<rect.area() << endl
        << left << setw(10) << "周长:"<<rect.circumference() << endl<<endl;
}
int main()
{

    Rectangle rect1; // 默认构造函数被调用
    output(rect1);
    Rectangle rect2(10, 5); // 带有两个参数的构造函数被调用
    output(rect2);
    Rectangle rect3(rect1); // 复制构造函数被调用
    rect3.resize(2); // 矩形rect3的长和宽同时缩放2倍
    output(rect3); rect3.resize(5, 2); // 矩形rect3的长缩放5倍, 宽缩放2倍
    output(rect3);
}

 

标签:const,int,double,第一次,width,length,实验,Rectangle
From: https://www.cnblogs.com/wsfpdy-qq1650829745/p/16743422.html

相关文章

  • 实验2:Open vSwitch虚拟交换机实践
    一、实验目的能够对OpenvSwitch进行基本操作;能够通过命令行终端使用OVS命令操作OpenvSwitch交换机,管理流表;能够通过Mininet的Python代码运行OVS命令,控制网络拓扑中的......
  • 实验2:Open vSwitch虚拟交换机实践
    1.ovs-vsctl基础操作实践:创建OVS交换机,以ovs-xxxxxxxxx命名,其中xxxxxxxxx为本人学号。在创建的交换机上增加端口p0和p1,设置p0的端口号为100,p1的端口号为101,类型均为interna......
  • 实验1 类和对象(1)
    #include<iostream>#include<string>#include<vector>intmain(){usingnamespacestd;strings1;strings2{"cplusplus"};strings3{s2}......
  • 实验1 类和对象(1)
    实验任务2#include<iostream>usingstd::cout;usingstd::endl;classPoint{public:Point(intx0=0,inty0=0);Point(constPoint&p);~Point()=default......
  • Python第五章实验报告
    一、实验题目Python第五章实例和实战作业二、实验目的和要求1.熟悉Pycharm的运行环境2.学习并掌握Python的字符串及正则表达式三、主要仪器设备联想小新air15硬件:AM......
  • 实验一 类和对象(1)
    任务2://Point类//相较于教材,在构造函数的写法上,采用了业界更通用的初始化列表方式#include<iostream>usingstd::cout;usingstd::endl;//定义Point类classPoi......
  • 实验1
    #include<iostream>usingstd::cout;usingstd::endl;classPoint{public:Point(intx0=0,inty0=0);Point(constPoint&p);~Poin......
  • 实验一
    #include<iostream>usingstd::cout;usingstd::endl;usingnamespacestd;classPoint{public:Point(intx0=0,inty0=0);Point(constPoint&p);......
  • 实验2:Open vSwitch虚拟交换机实践
    a)/home/用户名/学号/lab2/目录下执行ovs-vsctlshow命令、以及p0和p1连通性测试的执行结果截图;b)/home/用户名/学号/lab2/目录下开启MininetCLI并执行pingall命令的......
  • 实验一
    实验任务1task1_1:#include<iostream>#include<string>#include<vector>intmain(){ usingnamespacestd; strings1; strings2{"cplusplus"}; str......