实验一

实验任务2

源代码:

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

 

运行结果:

修改数据后运行结果

 

实验任务3

源代码:

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

 

运行结果:

更改数据后运行结果:

 

实验任务4

源代码:

 1 #include <iostream>
 2 class X
 3 {
 4 public:
 5     X();
 6     ~X();
 7     X(int m);
 8     X(const X& obj);
 9     X(X&& obj) noexcept;
10     void show() const;
11 private:
12     int data;
13 };
14 X::X() : data{ 42 }
15 {
16     std::cout << "default constructor called.\n";
17 }
18 X::~X()
19 {
20     std::cout << "destructor called.\n";
21 }
22 X::X(int m) : data{ m }
23 {
24     std::cout << "constructor called.\n";
25 }
26 X::X(const X& obj) : data{ obj.data }
27 {
28     std::cout << "copy constructor called.\n";
29 }
30 X::X(X&& obj) noexcept : data{ obj.data }
31 {
32     std::cout << "move constructor called.\n";
33 }
34 void X::show() const 
35 {
36     std::cout << data << std::endl;
37 }
38 int main() {
39     X x1;
40     x1.show();
41     X x2{ 2049 };
42     x2.show();
43     X x3{ x1 };
44     x3.show();
45     X x4{ std::move(x2) };
46     x4.show();
47 }

 

运行结果:

分析:

line39调用默认构造函数;line41调用构造函数;line43调用复制构造函数;line45调用移动构造函数;

line47析构函数被调用了4次,销毁和清理了对象x1、x2、x3、x4占用的资源。

 实验任务5

源代码:

 1 #include <iostream> 
 2 #include <iomanip> 
 3  
 4 class Rectangle
 5 {
 6 public:
 7     Rectangle();
 8     Rectangle(double l , double w );
 9     Rectangle(const    Rectangle& rect);
10     ~Rectangle()=default;
11     double len()const{return length;}
12     double wide()const { return width; }
13     double area()const { return length * width; }
14     double circumference()const { return 2 * length + 2 * width; }
15     void resize(int times);
16     void resize(int l_times, int w_times);
17     private:
18         double length, width;
19 };
20 Rectangle::Rectangle()
21 {
22     length = 2.0;
23     width = 1.0;
24 }
25 Rectangle::Rectangle(double l, double w)
26 {
27     length = l;
28     width = w;
29 }
30 Rectangle::Rectangle(const Rectangle& rect)
31 {
32     length = rect.length;
33     width = rect.width;
34 }
35 void Rectangle::resize(int times)
36 {
37     length *= times;
38     width *= times;
39 }
40 void Rectangle::resize(int l_times, int w_times)
41 {
42     length *= l_times;
43     width *= w_times;
44 }
45 
46 
47 void output(const Rectangle& rect)
48 {
49     using namespace std;
50     cout << "矩形信息: \n";
51     cout << "长:     " << fixed << setprecision(2) << rect.len() << endl;
52     cout << "宽:     " << fixed << setprecision(2) << rect.wide() << endl;
53     cout << "面积:   " << fixed << setprecision(2) << rect.area() << endl;
54     cout << "周长:   " << fixed << setprecision(2) << rect.circumference() << endl;
55 }
56 int main() 
57 { 
58     Rectangle rect1;
59     output(rect1);
60     Rectangle rect2(10, 5); 
61     output(rect2); 
62     Rectangle rect3(rect1); 
63     rect3.resize(2); 
64     output(rect3);
65     rect3.resize(5, 2); 
66     output(rect3); 
67 }

 

运行结果:

 

posted @ 2022-09-28 19:36  段彦博  阅读(10)  评论(0)    收藏  举报