实验1 类与对象

 1 #include <iostream>
 2 using namespace std;
 3 // 定义Point类
 4 class Point {
 5 public:
 6     Point(int x0=0, int y0=0);
 7     Point(const Point&p );
 8     ~Point()=default;//使用编译器生成析构函数 
 9     int get_x() const {return x;} // 内联成员函数
10     int get_y() const {return y;} // 内联成员函数
11     void show() const;
12 private:
13     int x, y;
14 };
15 // Point类的实现
16 // 构造函数(带有默认形参值)
17 Point::Point(int x0, int y0): x{x0}, y{y0}{
18     cout << "constructor called." << endl;
19 }
20 // 复制构造函数
21 // 参数必须是自身对象的引用类型
22 Point::Point(const Point& p): x{p.x}, y{p.y}{
23     cout << "copy constructor called." << endl;
24 }
25 void Point::show() const {
26     cout << "(" << x << ", "<< y << ")" << endl;
27 }
28 int main() {
29     Point p1(3, 7); // 构造函数被调用
30     p1.show();
31     Point p2 = p1; // 复制构造函数被调用
32     p2.show();
33     Point p3{p2}; // 复制构造函数被调用
34     p3.show();
35     cout << p3.get_x() << endl;
36 }

 1 // 时钟类Clock
 2 // 相较于教材,做了以下微调整:
 3 // 1. 在构造函数的写法上,采用了业界更通用的初始化列表方式
 4 // 2. 对于时钟显示的格式,使用操控符函数,控制其输出格式
 5 #include <iostream>
 6 #include <iomanip>
 7 using std::cout;
 8 using std::endl;
 9 
10  // 定义时钟类Clock
11  class Clock {
12 public:
13     Clock(int h=0,int m=0,int s=0);
14     Clock(const Clock& t);
15     ~Clock() =default;
16     
17     void set_time(int h,int m=0,int s=0);
18     void show_time() const;
19     
20 private:
21     int hour,minute,second;
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 Clock::Clock(const Clock& t): hour{t.hour}, minute{t.minute},
28 second{t.second} {
29     cout << "copy constructor called" << endl;
30 }    
31  void Clock::set_time(int h, int m, int s) {
32     hour = h;
33     minute = m;
34     second = s;
35 }
36 
37 void Clock::show_time() const {
38     using std::setw;
39     using std::setfill;
40     cout << setfill('0') << setw(2) << hour << ":"
41         << setw(2) << minute << ":"
42         << setw(2) << second << endl;
43 }
44 
45 // 普通函数定义
46 Clock reset() {
47     return Clock(0, 0, 0); // 构造函数被调用
48 }
49 
50 int main()
51 {
52     Clock c1(16, 5, 37); // 构造函数被调用
53     c1.show_time();
54     c1 = reset(); // 理论上:复制构造函数被调用
55     c1.show_time();
56     Clock c2(c1); // 复制构造函数被调用
57     c2.set_time(8);
58     c2.show_time();
59 }

 1 #include <iostream>
 2 // 定义一个简单抽象类
 3 class X{
 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 
15 X::X(): data{42} {
16     std::cout << "default constructor called.\n";
17 }
18 
19 X::~X() {
20     std::cout << "destructor called.\n";
21 }
22 
23 X::X(int m): data{m} {
24     std::cout << "constructor called.\n";
25 }
26 
27 X::X(const X& obj): data{obj.data} {
28     std::cout << "copy constructor called.\n";
29 }
30 
31 X::X(X&& obj) noexcept: data{obj.data} {
32     std::cout << "move constructor called.\n";
33 }
34 
35 void X::show() const {
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 }

X x1,调用默认构造函数,X x2{2049},调用带参数的构造函数,X x3{x1},调用复制构造函数,X x4{std::move(x2)},调用移动构造函数。

析构函数是在程序结束之前才进行运行,最后对x1,x2,x3,x4进行销毁。

 

 

 1 #include <iostream>
 2 #include <iomanip>
 3 using namespace std; 
 4 // 矩形类Rectangle的定义和实现
 5 class Rectangle{
 6     public:
 7         Rectangle(double length=2.0,double width=1.0);
 8         Rectangle(const Rectangle& rect);
 9         ~Rectangle()=default;
10         double len() const {return length;}
11         double wide() const {return width;}
12         double area() const {return length*width;}
13         double circumference() const {return(width+length)*2;}
14         void resize(int times);
15         void resize(int l_times,int w_times);
16     private:
17         double length,width;
18 };
19 Rectangle::Rectangle(double l,double w):length{l},width{w}{}
20 Rectangle::Rectangle(const Rectangle& r): length{r.length},width{r.width}{}
21 void Rectangle::resize(int times){
22     length*=times;
23     width*=times;
24 }
25 void Rectangle::resize(int l_times,int w_times)
26 {
27    length*=l_times;
28    width*=w_times;
29 }
30 // 普通函数, 用于输出矩形信息
31 void output(const Rectangle &rect) {
32     using namespace std;
33     cout << fixed << setprecision(2); 
34 // 控制输出格式:以浮点数形式输出、小数部分保留两位
35 // 补足代码:分行输出矩形长、宽、面积、周长
36     cout << "矩形信息: \n";
37     cout<<"长:  " <<rect.len()<<endl;
38     cout<<"宽:  " <<rect.wide()<<endl;
39     cout<<"面积:" <<rect.area()<<endl;
40     cout<<"周长:" <<rect.circumference()<<endl; 
41 }
42 // 主函数,测试Rectangle类
43 int main() {
44     Rectangle rect1; // 默认构造函数被调用
45     output(rect1);
46     Rectangle rect2(10, 5); // 带有两个参数的构造函数被调用
47     output(rect2);
48     Rectangle rect3(rect1); // 复制构造函数被调用
49     rect3.resize(2); // 矩形rect3的长和宽同时缩放2倍
50     output(rect3);
51     rect3.resize(5, 2); // 矩形rect3的长缩放5倍, 宽缩放2倍
52     output(rect3);
53 }

 

posted on 2022-09-29 18:56  WREKER  阅读(26)  评论(0编辑  收藏  举报

导航