实验1
实验1.2
1 #include <iostream> 2 using std::cout; 3 using std::endl; 4 class Point{ 5 public: 6 Point(int x0=0,int y0=0); 7 Point(const Point& p); 8 ~Point()=default; 9 10 int get_x() const{return x;} 11 int get_y() const{return y;} 12 void show() const; 13 private: 14 int x,y; 15 }; 16 Point::Point(int x0,int y0):x{x0},y{y0}{ 17 cout<<"constructor called."<<endl; 18 } 19 Point::Point(const Point& p):x{p.x},y{p.y}{ 20 cout<<"copy constructor called."<<endl; 21 } 22 void Point::show()const{ 23 cout<<"("<<x<<","<<y<<")"<<endl; 24 } 25 int main(){ 26 Point p1(6,1); 27 p1.show(); 28 29 Point p2=p1; 30 p2.show(); 31 32 Point p3{p2}; 33 p3.show(); 34 cout<<p3.get_x()<<endl; 35 }
实验1.3
1 #include <iostream> 2 #include <iomanip> 3 4 using std::cout; 5 using std::endl; 6 7 class Clock{ 8 public: 9 Clock(int h=0,int m=0,int s=0); 10 Clock(const Clock& t); 11 ~Clock()=default; 12 13 void set_time(int h,int m=0,int s=0); 14 void show_time()const; 15 private: 16 int hour,minute,second; 17 }; 18 Clock::Clock(int h,int m,int s):hour{h},minute{m},second{s}{ 19 cout<<"constructor called"<<endl; 20 } 21 Clock::Clock(const Clock& t):hour{t.hour},minute{t.minute},second{t.second} 22 { 23 cout<<"copy constructor called"<<endl; 24 } 25 void Clock::set_time(int h,int m,int s){ 26 hour=h; 27 minute=m; 28 second=s; 29 } 30 void Clock::show_time()const{ 31 using std::setw; 32 using std::setfill; 33 34 cout<<setfill('0')<<setw(2)<<hour<<":" 35 <<setw(2)<<minute<<":" 36 <<setw(2)<<second<<endl; 37 } 38 Clock reset(){ 39 return Clock(0,0,0); 40 } 41 int main(){ 42 Clock c1(11,12,5); 43 c1.show_time(); 44 45 c1=reset(); 46 c1.show_time(); 47 48 Clock c2(c1); 49 c2.set_time(4); 50 c2.show_time(); 51 }
实验1.4
1 #include <iostream> 2 class X{ 3 public: 4 X(); 5 ~X(); 6 X(int m); 7 X(const X& obj); 8 X(X&& obj) noexcept; 9 void show() const; 10 private: 11 int data; 12 }; 13 X::X():data{42}{ 14 std::cout<<"default constructor called.\n"; 15 } 16 X::~X(){ 17 std::cout<<"destructor called.\n"; 18 } 19 X::X(int m):data{m}{ 20 std::cout<<"constructor called.\n"; 21 } 22 X::X(const X& obj): data{obj.data}{ 23 std::cout<<"copy constructor called.\n"; 24 } 25 X::X(X&& obj) noexcept:data{obj.data}{ 26 std::cout<<"move constructor called.\n"; 27 } 28 void X::show()const{ 29 std::cout<<data<<std::endl; 30 } 31 int main(){ 32 X x1; 33 x1.show(); 34 35 X x2{2049}; 36 x2.show(); 37 38 X x3{x1}; 39 x3.show(); 40 41 X x4{ std::move(x2)}; 42 x4.show(); 43 }
分析:第32行调用默认构造函数、第35行调用构造函数、第38行调用复制构造函数、第41行调用移动构造函数
析构函数在程序结束时被调用顺序为,x4-x3-x2-x1.
实验1.5
1 #include <iostream> 2 #include <iomanip> 3 class Rectangle{ 4 public: 5 Rectangle(double length=2.0,double wideth=1.0); 6 Rectangle(const Rectangle& rect); 7 ~Rectangle()=default; 8 double len()const; 9 double wide()const ; 10 double area()const ; 11 double circumference()const ; 12 void resize(auto t=1); 13 void resize(double l_t=1.0,double w_t=1.0); 14 15 16 private: 17 double l;double w;double times;double l_times;double w_times; 18 }; 19 Rectangle::Rectangle(double length,double wideth):l{length},w{wideth}{ 20 } 21 Rectangle::Rectangle(const Rectangle& rect){ 22 l=rect.l; 23 w=rect.w; 24 25 } 26 void Rectangle::resize(auto t){ 27 times=t; 28 l*=times; 29 w*=times; 30 } 31 void Rectangle::resize(double l_t,double w_t){ 32 l_times=l_t; 33 w_times=w_t; 34 l*=l_times; 35 w*=w_times; 36 } 37 double Rectangle::len()const{ 38 39 return l; 40 } 41 double Rectangle::wide()const{ 42 43 return w; 44 } 45 double Rectangle::area()const{ 46 return l*w; 47 } 48 double Rectangle::circumference()const{ 49 return 2*(l+w); 50 } 51 void output(const Rectangle &rect){ 52 using namespace std; 53 cout<<"矩阵信息:\n"; 54 cout<<fixed<<setprecision(2); 55 cout<<"长: "<<rect.len()<<endl 56 <<"宽: "<<rect.wide()<<endl 57 <<"面积: "<<rect.area()<<endl 58 <<"周长: "<<rect.circumference()<<endl; 59 cout<<endl; 60 } 61 int main(){ 62 Rectangle rect1; 63 output(rect1); 64 65 Rectangle rect2(10,5); 66 output(rect2); 67 68 Rectangle rect3(rect1); 69 rect3.resize(2); 70 output(rect3); 71 72 rect3.resize(5,2); 73 output(rect3); 74 75 }