实验1 类和对象(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 private: 15 int x, y; 16 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 void Point::show() const { 26 cout << "(" << x << ", " 27 << y << ")" << endl; 28 } 29 int main(){ 30 Point p1(50,30); 31 p1.show(); 32 33 Point p2 = p1; 34 p2.show(); 35 36 Point p3{p2}; 37 p3.show(); 38 cout << p3.get_x() << endl; 39 }

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

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 private: 12 int data; 13 }; 14 X::X(): data{42} { 15 cout << "default constructor called.\n"; 16 } 17 X::~X() { 18 cout << "destructor called.\n"; 19 } 20 X::X(int m): data{m} { 21 cout << "constructor called.\n"; 22 } 23 X::X(const X& obj): data{obj.data} { 24 cout << "copy constructor called.\n"; 25 } 26 X::X(X&& obj) noexcept: data{obj.data} { 27 cout << "move constructor called.\n"; 28 } 29 void X::show() const { 30 cout << data << endl; 31 } 32 33 int main(){ 34 X x1; 35 x1.show(); 36 37 X x2{2049}; 38 x2.show(); 39 40 X x3{x1}; 41 x3.show(); 42 43 X x4{ move(x2) }; 44 x4.show(); 45 46 }
line 34调用默认构造函数,line 37调用构造函数,line 40调用复制构造函数,line 43调用移动构造函数;
程序结束时调用析构函数,释放x1,x2,x3,x4,共调用4次。

1 #include <iostream> 2 #include <iomanip> 3 using namespace std; 4 class Rectangle{ 5 public: 6 Rectangle(); 7 Rectangle(double l, double w); 8 Rectangle(const Rectangle& p); 9 ~Rectangle() = default; 10 double len() const; 11 double wide() const; 12 double area() const; 13 double circumference() const; 14 void resize(int times); 15 void resize(int l_times, int w_times); 16 private: 17 double length, width; 18 19 }; 20 Rectangle::Rectangle():length{2.0},width{1.0}{}; 21 Rectangle::Rectangle(double l, double w):length{l},width{w}{}; 22 Rectangle::Rectangle(const Rectangle& p):length{p.length},width{p.width}{}; 23 double Rectangle::len() const{ return length; } 24 double Rectangle::wide() const{ return width; } 25 double Rectangle::area() const{ return length*width; } 26 double Rectangle::circumference() const{ return 2*(length+width); } 27 void Rectangle::resize(int times) { length*=times;width*=times; } 28 void Rectangle::resize(int l_times, int w_times){ length*=l_times;width*=w_times;} 29 30 void output(const Rectangle &rect) { 31 using namespace std; 32 cout << "矩形信息: \n"; 33 cout << fixed << setprecision(2); 34 cout << "长: "<< left << rect.len() <<endl 35 << "宽: "<< left << rect.wide() <<endl 36 << "面积: "<< left << rect.area() <<endl 37 << "周长: "<< left << rect.circumference() <<endl 38 << endl; 39 40 } 41 int main() { 42 Rectangle rect1; // 默认构造函数被调用 43 output(rect1); 44 Rectangle rect2(10, 5); // 带有两个参数的构造函数被调用 45 output(rect2); 46 Rectangle rect3(rect1); // 复制构造函数被调用 47 rect3.resize(2); // 矩形rect3的长和宽同时缩放2倍 48 output(rect3); 49 rect3.resize(5, 2); // 矩形rect3的长缩放5倍, 宽缩放2倍 50 output(rect3); 51 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结