实验任务二

 1 #include<iostream>
 2 using namespace std;
 3 class Point
 4 {
 5 public:
 6     Point(int x0 = 0, int y0 = 0);
 7     Point(const Point& p);
 8     ~Point() = default;
 9     int getX() const { return x; }
10     int getY() const { return y; }
11     void show() const;
12 private:
13     int x, y;
14 };
15 //构造函数(带有默认函数值)
16 Point::Point(int x0, int y0) :x{ x0 }, y{ y0 }
17 {
18     cout << "constructor called." << endl;
19 }
20 //复制构造函数
21 Point::Point(const Point& p) :x{ p.x }, y{ p.y }
22 {
23     cout << "copy constructor called" << endl;
24 }
25 void Point::show() const
26 {
27     cout << "(" << x << "." << y << ")" << endl;
28 }
29 int main()
30 {
31     Point p1(3,9);
32     p1.show();
33 
34     Point p2 = p1;
35     p2.show();
36 
37     Point p3{ p2 };
38     p3.show();
39     cout << p3.getX() << endl;
40 
41 }

 

 

实验任务三

 

 1 #include<iostream>
 2 #include<iomanip>
 3 using namespace std;
 4 class Clock
 5 {
 6 public:
 7     Clock(int h = 0, int m = 0, int s = 0);
 8     Clock(const Clock& t);
 9     ~Clock() = default;
10 
11     void setTime(int h, int m = 0, int s = 0);
12     void showTime() const;
13 
14 private:
15     int hour, minute, second;
16 };
17 Clock::Clock(int hour, int minute, int second)
18 {
19     this->hour = hour;
20     this->minute = minute;
21     this->second = second;
22     cout << "constructor called" << endl;
23 }
24 Clock::Clock(const Clock& t)
25 {
26     this->hour = t.hour;
27     this->minute = t.minute;
28     this->second = t.second;
29     cout << "copy constructor called" << endl;
30 }
31 void Clock::setTime(int hour, int minute, int second)
32 {
33     this->hour = hour;
34     this->minute = minute;
35     this->second = second;
36 }
37 
38 void Clock::showTime() const
39 {
40     cout << setfill('0') << setw(2) << hour
41         << ":" << setw(2) << minute
42         << ":" << setw(2) << second << endl;
43 }
44 Clock reset()
45 {
46     return Clock(0, 0, 0);
47 }
48 int main()
49 {
50     Clock c1(13, 14, 0);
51     c1.showTime();
52 
53     c1 = reset();
54     c1.showTime();
55 
56     Clock c2(c1);
57     c2.setTime(13);
58     c2.showTime();
59 
60 }

 

 

实验任务四

 

#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被逐一清理

实验任务五

 1 #include<iostream>
 2 #include<iomanip>
 3 using namespace std;
 4 class Rectangle
 5 {
 6 public:
 7     Rectangle();
 8     Rectangle(double l,double w);
 9     double len() const;
10     double wide() const;
11     Rectangle(const Rectangle& p);
12     double area()const;
13     double circumference()const;
14     void resize(double times);
15     void resize(double l_times, double w_times);
16 private:
17     double length, width;
18 };
19 Rectangle::Rectangle()
20 {
21     length = 2.0;
22     width = 1.0;
23 }
24 Rectangle::Rectangle(double l, double w)
25 {
26     length = l;
27     width = w;
28 }
29 Rectangle::Rectangle(const Rectangle& p)
30 {
31     length = p.length;
32     width = p.width;
33 }
34 double Rectangle::len()const
35 {
36     return length;
37 }
38 double Rectangle::wide() const
39 {
40     return width;
41 }
42 double Rectangle::area() const
43 {
44     return length * width;
45 }
46 double Rectangle::circumference()const
47 {
48     return 2 * (length + width);
49 }
50 void Rectangle::resize(double times)
51 {
52     length = length * times;
53     width = width * times;
54 }
55 void Rectangle::resize(double l_times, double w_times)
56 {
57     length = length * l_times;
58     width = width * w_times;
59 }
60 void output(const Rectangle& rect)
61 {
62 
63     cout << "矩形信息: \n"; cout << fixed << setprecision(2)
64         <<left<<setw(10)<<"长:"<< rect.len() << endl
65         << left << setw(10) << "宽:"<<rect.wide() << endl
66         << left << setw(10) << "面积:"<<rect.area() << endl
67         << left << setw(10) << "周长:"<<rect.circumference() << endl<<endl;
68 }
69 int main()
70 {
71 
72     Rectangle rect1; // 默认构造函数被调用
73     output(rect1);
74     Rectangle rect2(10, 5); // 带有两个参数的构造函数被调用
75     output(rect2);
76     Rectangle rect3(rect1); // 复制构造函数被调用
77     rect3.resize(2); // 矩形rect3的长和宽同时缩放2倍
78     output(rect3); rect3.resize(5, 2); // 矩形rect3的长缩放5倍, 宽缩放2倍
79     output(rect3);
80 }

 

posted on 2022-09-29 23:02  离小离  阅读(38)  评论(0)    收藏  举报