实验2
1 #include <iostream> 2 using std::cout; 3 using std::endl; 4 class Point 5 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 { 21 cout << "constructor called." << endl; 22 } 23 24 Point::Point(const Point& p): x{p.x}, y{p.y} 25 26 { 27 cout << "copy constructor called." << endl; 28 } 29 30 void Point::show() const 31 32 { 33 cout << "(" << x << ", " 34 << y << ")" << endl; 35 } 36 int main() 37 38 { 39 Point p1(4, 5); 40 p1.show(); 41 Point p2 = p1; 42 p2.show(); 43 Point p3{p2}; 44 p3.show(); 45 cout << p3.get_x() << endl; 46 }
运行结果以及更换数据元素后的结果。
实验3
1 #include <iostream> 2 #include <iomanip> 3 using std::cout; 4 using std::endl; 5 6 class Clock 7 8 { 9 public: 10 Clock(int h = 0, int m = 0, int s = 0); 11 Clock(const Clock& t); 12 ~Clock() = default; 13 void set_time(int h, int m = 0, int s = 0); 14 15 void show_time() const; 16 private: 17 int hour, minute, second; 18 }; 19 20 Clock::Clock(int h, int m, int s): hour{h}, minute{m}, second{s} 21 22 { 23 cout << "constructor called" << endl; 24 } 25 Clock::Clock(const Clock& t): hour{t.hour}, minute{t.minute}, 26 second{t.second} 27 28 { 29 cout << "copy constructor called" << endl; 30 } 31 void Clock::set_time(int h, int m, int s) 32 33 { 34 hour = h; 35 minute = m; 36 second = s; 37 } 38 void Clock::show_time() const 39 40 { 41 using std::setw; 42 using std::setfill; 43 cout << setfill('0') << setw(2) << hour << ":" 44 << setw(2) << minute << ":" 45 << setw(2) << second << endl; 46 } 47 48 Clock reset() 49 50 { 51 return Clock(0, 0, 0); 52 } 53 int main() 54 { 55 Clock c1(12, 0, 5); 56 c1.show_time(); 57 c1 = reset(); 58 c1.show_time(); 59 Clock c2(c1); 60 c2.set_time(6); 61 c2.show_time(); 62 }
运行结果以及更换数据元素后的实验结果
实验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 12 private: 13 int data; 14 15 }; 16 17 X::X(): data{42} 18 { 19 std::cout<<"default constructor called.\n"; 20 } 21 X::~X() 22 { 23 std::cout<<"destructor called.\n"; 24 } 25 X::X(int m): data{m} 26 { 27 std::cout<<"constructor called.\n"; 28 } 29 X::X(const X& obj): data{obj.data}
30 { 31 std::cout<<"copy constructor called.\n"; 32 } 33 X::X(X&& obj) noexcept:data{obj.data} 34 { 35 std::cout<<"move constructor called.\n"; 36 } 37 void X::show() const 38 { 39 std::cout<<data<<std::endl; 40 } 41 int main() 42 { 43 X x1; 44 x1.show(); 45 X x2{2049}; 46 x2.show(); 47 X x3{x1}; 48 x3.show(); 49 X x4{ std::move(x2) }; 50 x4.show(); 51 }
运行结果以及修改元素后的运行结果
分析
1.第43行,程序自动调用类中的X()默认构造函数。45行中,调用了X(int m)的构造函数。47行中,调用了复制构造函数。49行调用了移动构造函数。这些都可以从conmstructor called中可以看出来调用的时候。
2.程序结束前,系统调用析构函数。
实验5
1 #include<iostream> 2 #include<iomanip> 3 class Rectangle 4 { 5 public: 6 Rectangle(double l=2.0,double w=1.0); 7 Rectangle(const Rectangle& obj); 8 ~Rectangle()=default; 9 double len() const 10 { 11 return length; 12 } 13 double wide() const 14 { 15 return width; 16 } 17 double area() const 18 { 19 return length*width; 20 } 21 double circumference() const 22 { 23 return 2*(length+width); 24 } 25 void resize(int times); 26 void resize(int l_times,int w_times); 27 private: 28 double length, width; 29 }; 30 Rectangle::Rectangle(double l,double w) 31 { 32 length=l; 33 width=w; 34 } 35 Rectangle::Rectangle(const Rectangle& obj) 36 { 37 length=obj.length; 38 width=obj.width; 39 } 40 void Rectangle::resize(int times) 41 { 42 length*=times; 43 width*=times; 44 } 45 void Rectangle::resize(int l_times,int w_times) 46 { 47 length*=l_times; 48 width*=w_times; 49 } 50 51 void output(const Rectangle &rect) 52 { 53 using namespace std; 54 55 cout<<"矩阵信息:\n"; 56 cout<< fixed << setprecision(2); 57 cout<<std::left<<setw(8)<<"长:"<<std::right<<setw(8)<<rect.len()<<endl; 58 cout<<std::left<<setw(8)<<"宽:"<<std::right<<setw(8)<<rect.wide()<<endl; 59 cout<<std::left<<setw(8)<<"面积:"<<std::right<<setw(8)<<rect.area()<<endl; 60 cout<<std::left<<setw(8)<<"周长"<<std::right<<setw(8)<<rect.circumference()<<endl; 61 cout<<endl; 62 } 63 64 int main() 65 { 66 Rectangle rect1; 67 output(rect1); 68 Rectangle rect2(10, 5); 69 output(rect2); 70 Rectangle rect3(rect1); 71 rect3.resize(2); 72 output(rect3); 73 rect3.resize(5, 2); 74 output(rect3); 75 }
运行结果
标签:const,int,void,times,length,实验,Rectangle From: https://www.cnblogs.com/lc114514/p/16738961.html