实验一:类和对象

 

  1. task2
 1 #include<iostream>
 2 using std::cout;
 3 using std::endl;
 4 
 5 class Point{
 6     public:
 7         Point(int x0 = 0, int y0 = 0);    //构造函数 
 8         Point(const Point&p);    //复制构造函数 
 9         ~Point() = default;
10         
11         int get_x() const { return x; }        //内联成员函数? 
12         int get_y() const { return y; }        //内联成员函数? 
13         void show() const;
14         
15     private:
16         int x, y;
17 };
18 
19 Point::Point(int x0, int y0): x{x0},y{y0} {
20     cout << "constructor called. " << endl;
21 }
22 Point::Point(const Point& p): x{p.x}, y{p.y} {
23     cout << "copy constructor called. " << endl;
24 }
25 
26 void Point::show() const{
27     cout << "(" << x << ", "
28                << y << ")" << endl;
29 }
30 
31 int main()
32 {
33     //与实验一的创建string类类似  
34     Point p1(34, 56);        //调用构造函数 
35     p1.show();
36     
37     Point p2 = p1;        //复制构造函数 
38     p2.show();
39     
40     Point p3{p2};        //复制构造函数 
41     p3.show();
42     cout << p3.get_x() <<endl;
43 }

 

 

  2. task3

 1 //注意初始化列表的方式 
 2 // 使用操控符函数 
 3 
 4 #include<iostream>
 5 #include<iomanip>
 6 
 7 using std::cout;
 8 using std::endl;
 9 
10 class Clock {
11 public:
12     Clock(int h = 0, int m = 0, int s = 0);
13     Clock(const Clock& t);
14     ~Clock() = default; 
15     
16     void set_time(int h, int m = 0, int s = 0);
17     void show_time() const; 
18     
19 private:
20     int hour, minute, second; 
21 }; 
22  
23  //类 Clock的实现
24  Clock::Clock(int h, int m, int s): hour{h}, minute{m}, second{s} {
25      cout << "constructor called" << endl; 
26  } 
27  
28  Clock::Clock(const Clock& t):hour{t.hour},minute{t.minute},
29  second{t.second} {
30      cout << "copy constructor called" << endl;  
31  }
32  
33  void Clock::set_time(int h, int m, int s) {
34      hour = h;
35      minute = m;
36      second = s;
37  }
38  
39  void Clock::show_time() const {
40      using std::setw;
41      using std::setfill;
42      
43      cout << setfill('0') << setw(2) << hour << ":"
44           << setw(2) << minute << ":"
45           << setw(2) << second << endl;
46  }
47  
48  //定义普通函数, 函数返回值是 Clock类? 
49  Clock reset() {
50      return Clock(0, 0, 0);
51  } 
52 
53 int main()
54 {
55     Clock c1(12, 34, 55);
56     c1.show_time();
57     
58     c1 = reset();
59     c1.show_time();
60     
61     
62     Clock c2(c1);    //用c2 = c1、c2{c1} 也可以 
63     c2.set_time(6, 7);
64     c2.show_time();
65  } 

 

 

 

  3. task4

 1 #include<iostream>
 2 
 3 //定义一个简单抽象类
 4 class X{
 5 public:
 6     X();    //默认构造函数 
 7     ~X();    //析构函数 
 8     X(int m);    //构造函数 
 9     X(const X& obj);    //复制构造函数 
10     X(X&& obj) noexcept;     //移动构造函数 
11     void show() const;         //显示数据 
12     
13 private:
14     int data;
15 }; 
16 
17 X::X(): data{42} {
18     std::cout << "default constructor called.\n";
19 }
20 
21 X::~X() {
22     std::cout << "destructor called.\n";
23 }
24 
25 X::X(int m): data{m} {
26     std::cout << "constructor called,\n";
27 }
28 
29 X::X(const X& obj): data{obj.data} {
30     std::cout << "copy constructor called.\n";
31 }
32 
33 X::X(X&& obj) noexcept: data{obj.data} {
34     std::cout << "move constructor called.\n";
35 }
36 
37 void X::show() const {
38     std::cout << data << std::endl;
39 }
40 
41 int main(){
42     X x1;    //默认构造函数
43     x1.show();//42
44     
45     X x2{2049};    //构造函数
46     x2.show();//2049
47     
48     X x3{x1};    //复制构造函数
49     x3.show();//42
50     
51     X x4{ std::move(x2) };    //移动构造函数 
52     x4.show();//2049
53     //析构函数被调用4次,销毁和清理对象x1,x2,x3,x4占用的资源 
54 }

 

 

在42行 默认构造函数被调用,45行 带一个参数的构造函数被调用,48行 复制构造函数被调用,51行 移动构造函数被调用。

此题中,析构函数是在对象生命周期结束,被销毁时被调用,被调用了4次,分别销毁和清理了对象x1, x2, x3, x4占用的资源。

 

  4. task5

 1 #include<iostream>
 2 #include<iomanip>
 3 
 4 using namespace std;
 5 
 6 class Rectangle{
 7 public:
 8     Rectangle();    //默认构造函数
 9     Rectangle(double l, double w);    //构造函数
10     Rectangle(Rectangle& rect);    //复制函数 
11     ~Rectangle();    //析构函数 
12     
13     double len() const { return length; }
14     double wide() const { return width; }
15     double area() const { return width * length; }
16     double circumference() const { return (length + width) * 2; }
17     void resize(double times);
18     void resize(double l_times, double w_times);
19 
20 private: 
21     double length,width;
22 };
23 
24 //Rectangle类的实现 
25 Rectangle::Rectangle(): length{2.0}, width{1.0} {} 
26 Rectangle::Rectangle(double l, double w): length{l}, width{w} {}
27 Rectangle::Rectangle(Rectangle& rect): length{rect.length}, width{rect.width} {}
28 Rectangle::~Rectangle() {}
29 
30 void Rectangle::resize(double times) { length *= times; width *= times; }
31 void Rectangle::resize(double l_times, double w_times) { length *= l_times; width *= w_times; }
32 
33 //普通函数
34 void output(Rectangle& rect)
35 {
36     cout << "矩形信息:\n"
37          << fixed << setprecision(2)    //setprecision(n)与setiosflags(ios::fixed)合用,控制小数点右边的数字个数
38          << left << setw(8) << "长:" << rect.len() << endl
39                   << setw(8) << "宽:" << rect.wide() << endl
40                   << setw(8) << "面积:" << rect.area() << endl
41                   << setw(8) << "周长:" << rect.circumference() << endl
42                   << endl;
43  }
44  
45 //主函数 
46 int main() {
47     Rectangle rect1; // 默认构造函数
48     output(rect1);
49     
50     Rectangle rect2(10, 5); // 构造函数
51     output(rect2);
52     
53     Rectangle rect3(rect1); // 复制构造函数
54     rect3.resize(2); 
55     output(rect3);
56     
57     rect3.resize(5, 2); 
58     output(rect3);
59 }
60  

 

 

 5. 结论:(补充说明)task5中,len(),wide(),area(),circumference()函数体放在类体内,是内联函数。

 

posted @ 2022-09-30 08:05  Weslie_xyy  阅读(39)  评论(0编辑  收藏  举报