实验1
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 23 Point::Point(const Point& p): x{p.x}, y{p.y} { 24 cout << "copy constructor called." << endl; 25 } 26 27 void Point::show() const { 28 cout << "(" << x << ", " << y << ")" << endl; 29 } 30 31 int main() { 32 Point p1(7, 8); 33 p1.show(); 34 35 Point p2 = p1; 36 p2.show(); 37 38 Point p3{p2}; 39 p3.show(); 40 cout << p3.get_x() << endl; 41 }
1 #include<iostream> 2 #include<iomanip> 3 using std::cout; 4 using std::endl; 5 6 class Clock { 7 public: 8 Clock(int h = 0, int m = 0, int s = 0); 9 Clock(const Clock& t); 10 ~Clock() = default; 11 12 void set_time(int h, int m = 0, int s = 0); 13 void show_time() const; 14 15 private: 16 int hour, minute, second; 17 }; 18 19 Clock::Clock(int h, int m, int s): hour{h}, minute{m}, second{s} { 20 cout << "constructor called " << endl; 21 } 22 23 Clock::Clock(const Clock& t): hour{t.hour}, minute{t.minute}, second{t.second} { 24 cout << "copy constructor called" << endl; 25 } 26 27 void Clock::set_time(int h, int m, int s) { 28 hour = h; 29 minute = m; 30 second = s; 31 } 32 33 void Clock::show_time() const { 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 return Clock(0, 0, 0); 43 } 44 45 int main() 46 { 47 Clock c1(11, 1, 6); 48 c1.show_time(); 49 50 c1 = reset(); 51 c1.show_time(); 52 53 Clock c2{c1}; 54 c2.set_time(7); 55 c2.show_time(); 56 }
1 #include<iostream> 2 using namespace std; 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 12 private: 13 int data; 14 }; 15 16 X::X(): data{42} { 17 cout << "default constructor called.\n"; 18 } 19 20 X::~X() { 21 cout << "destructor called.\n"; 22 } 23 24 X::X(int m): data{m} { 25 cout << "constructor called.\n"; 26 } 27 28 X::X(const X& obj): data{obj.data} { 29 cout << "copy constructor called.\n"; 30 } 31 32 X::X(X&& obj) noexcept: data{obj.data} { 33 cout << "move constructor called.\n"; 34 } 35 36 void X::show() const { 37 cout << data << endl; 38 } 39 40 int main() 41 { 42 X x1; 43 x1.show(); 44 45 X x2{2049}; 46 x2.show(); 47 48 X x3{x1}; 49 x3.show(); 50 51 X x4{ move(x2) }; 52 x4.show(); 53 }
line 42调用默认构造函数,line 45调用带参数的构造函数,line 48 调用复制构造函数
line 51 调用移动构造函数,当对象被删除时调用析构函数,顺序依次为 x3, x2, x1
1 #include <iostream> 2 #include <iomanip> 3 using namespace std; 4 // 矩形类Rectangle的定义和实现 5 // 补足代码 6 // ××× 7 class Rectangle{ 8 private: 9 float length; //矩形的长度 10 float width; //矩形的宽度 11 12 public: 13 Rectangle(); 14 Rectangle(float l, float w); 15 Rectangle(const Rectangle& rect); 16 ~Rectangle(); 17 float len() const; 18 float wide() const ; 19 float area() const ; 20 float circumference() const ; 21 void resize(float times); 22 void resize(float l_times, float w_times); 23 }; 24 25 Rectangle::Rectangle(): length{2.0}, width{1.0} {} 26 27 Rectangle::Rectangle(float l, float w): length{l}, width{w} {} 28 29 Rectangle::Rectangle(const Rectangle& rect): length{rect.length}, width{rect.width} { 30 } 31 32 Rectangle::~Rectangle() {} 33 34 float Rectangle::len() const { 35 return length; 36 } 37 38 float Rectangle::wide() const { 39 return width; 40 } 41 42 float Rectangle::area() const { 43 return length * width; 44 } 45 46 float Rectangle::circumference() const { 47 return 2 * (length + width); 48 } 49 50 void Rectangle::resize(float times) { 51 length *= times; 52 width *= times; 53 } 54 55 void Rectangle::resize(float ltimes, float wtimes) { 56 length *= ltimes; 57 width *= wtimes; 58 } 59 // 普通函数, 用于输出矩形信息 60 void output(const Rectangle& rect) { 61 using namespace std; 62 cout << "矩形信息: \n"; 63 cout << fixed << setprecision(2);// 控制输出格式:以浮点数形式输出、小数部分保留两位 64 // 补足代码:分行输出矩形长、宽、面积、周长 65 // ××× 66 cout <<"长: " << left << setw(6) << rect.len() << endl; 67 cout <<"宽: " << left << setw(6) << rect.wide() << endl; 68 cout <<"面积:" << left << setw(6) << rect.area() << endl; 69 cout <<"周长:" << left << setw(6) << rect.circumference() << endl; 70 cout<<endl; 71 } 72 // 主函数,测试Rectangle类 73 int main() { 74 Rectangle rect1; // 默认构造函数被调用 75 output(rect1); 76 Rectangle rect2(10, 5); // 带有两个参数的构造函数被调用 77 output(rect2); 78 Rectangle rect3(rect1); // 复制构造函数被调用 79 rect3.resize(2); // 矩形rect3的长和宽同时缩放2倍 80 output(rect3); 81 rect3.resize(5, 2); // 矩形rect3的长缩放5倍, 宽缩放2倍 82 output(rect3); 83 }
我通过这次的实验理解了:
类、对象的基础理解,知道了什么是类的抽象、封装,类的接口
如何用C++正确定义、实现、测试类
如何用C++正确创建对象,并基于对象编程
如何对C++内存资源管理技术的初步了解,知道构造函数、析构函数的用途和调用时机