实验一 类和对象(1)
任务二源代码:
 
1 //task2.cpp 2 3 #include<iostream> 4 using std::cout; 5 using std::endl; 6 7 //定义Point类 8 class Point 9 { 10 public: 11 Point(int x0=0,int y0=0); 12 Point(const Point &p); 13 ~Point()=default; 14 15 int get_x() const {return x;} 16 int get_y() const {return y;} 17 void show() const; 18 private: 19 int x,y; 20 }; 21 22 //Point类的实现 23 //构造函数(带有默认形参值) 24 25 Point::Point(int x0,int y0):x{x0},y{y0} 26 { 27 cout<<"constructor called."<<endl; 28 } 29 30 //复制构造函数、 31 //参数必须是自身对象的引用类型 32 33 Point::Point(const Point &p):x{p.x},y{p.y} 34 { 35 cout<<"copy constructor called."<<endl; 36 } 37 38 void Point::show() const 39 { 40 cout<<"("<<x<<","<<y<<")"<<endl; 41 } 42 43 int main() 44 { 45 Point p1(9,8);//构造函数被调用 46 p1.show(); 47 48 Point p2=p1;//复制构造函数被调用 49 p2.show(); 50 51 Point p3{p2};//复制构造函数被调用 52 p3.show(); 53 cout<<p3.get_x()<<endl; 54 }
任务二运行结果:

任务三源代码:
1 //时钟类Clock 2 #include<iostream> 3 #include<iomanip> 4 using std::cout; 5 using std::endl; 6 7 //定义时钟类Clock 8 class Clock 9 { 10 public: 11 Clock(int h=0,int m=0, int s=0); 12 Clock(const Clock &t); 13 ~Clock()=default; 14 15 void set_time(int h,int m=0,int s=0); 16 void show_time() const; 17 18 private: 19 int hour,minute,second; 20 } ; 21 22 //类clock的实现 23 Clock::Clock(int h,int m,int s): hour{h},minute{m},second{s} 24 { 25 cout<<"constructor called"<<endl; 26 } 27 28 Clock::Clock(const Clock &t): hour{t.hour},minute{t.minute},second{t.second} 29 { 30 cout<<"copy constructor called"<<endl; 31 } 32 33 void Clock::set_time(int h,int m,int s) 34 { 35 hour=h; 36 minute=m; 37 second=s; 38 } 39 40 void Clock::show_time() const 41 { 42 using std::setw; 43 using std::setfill; 44 45 cout<<setfill('0')<<setw(2)<<hour<<":" 46 <<setw(2)<<minute<<":" 47 <<setw(2)<<second<<endl; 48 } 49 50 //普通函数定义 51 Clock reset() 52 { 53 return Clock(0,0,0);//构造函数被调用 54 } 55 56 int main() 57 { 58 Clock c1(2,8,1);//构造函数被调用 59 c1.show_time(); 60 61 c1=reset();//理论上:复制构造函数被调用 62 c1.show_time(); 63 64 Clock c2(c1);//复制构造函数被调用 65 c2.set_time(8); 66 c2.show_time(); 67 }
任务三运行结果截图:

任务四源代码:
1 #include<iostream> 2 3 //定义一个简单抽象类 4 class X 5 { 6 public: 7 X();//默认构造函数 8 ~X();//析构函数 9 X(int m);//构造函数 10 X(const X& obj);//复制构造函数 11 X(X&& obj) noexcept;//移动构造函数 12 void show() const;//显示函数 13 14 private: 15 int data; 16 }; 17 18 X::X(): data{42} 19 { 20 std::cout<<"default constructor called.\n"; 21 } 22 23 X::~X() 24 { 25 std::cout<<"destructor called.\n"; 26 } 27 28 X::X(int m):data{m} 29 { 30 std::cout<<"constructor called.\n"; 31 } 32 X::X(const X& obj): data{obj.data} 33 { 34 std::cout<<"copy constructor called.\n"; 35 } 36 X::X(X&& obj) noexcept: data{obj.data} 37 { 38 std::cout<<"move constructor called.\n"; 39 } 40 41 void X::show() const 42 { 43 std::cout<<data<<std::endl; 44 } 45 46 int main() 47 { 48 X x1;//默认构造函数被编译器自动调用 49 x1.show(); 50 51 X x2{2049}; 52 x2.show();//构造函数被编译器自动调用 53 54 X x3{x1};//复制构造函数被编译器自动调用 55 x3.show(); 56 57 X x4{std::move(x2)};//移动构造函数被编译器调用 58 x4.show(); 59 }
任务四运行结果截图:

分析:
1.构造函数的调用及其顺序:line48-49执行时,调用默认构造函数X();line51-52执行时,调用构造函数X(int m);line54-55执行时,调用复制构造函数X(const X& obj);line57-58执行时,调用移动构造函数X(X&& obj) noexcept。
2.当对象被删除前调用析构函数,即在line58执行完后调用析构函数,顺序与构造函数(上一条)相反。
实验五代码:
1 #include<iostream> 2 #include<iomanip> 3 class rectangle 4 { 5 public: 6 rectangle(); 7 rectangle(double l, double w); 8 rectangle(const rectangle& rect); 9 ~rectangle() {} 10 double len() const { return length; } 11 double wide() const { return width; } 12 double area() const { return length * width; } 13 double circumference() const { return 2 * (length + width); } 14 void resize(int times); 15 void resize(int l_times, int w_times); 16 private: 17 double length, width; 18 }; 19 rectangle::rectangle() 20 { 21 length = 2.0; 22 width = 1.0; 23 } 24 rectangle::rectangle(double l, double w) 25 { 26 length = l; 27 width = w; 28 } 29 rectangle::rectangle(const rectangle& rect) 30 { 31 length = rect.length; 32 width = rect.width; 33 } 34 void rectangle::resize(int times) 35 { 36 length = length * times; 37 width = width * times; 38 } 39 void rectangle::resize(int l_times, int w_times) 40 { 41 length = length * l_times; 42 width = width * w_times; 43 } 44 rectangle output(const rectangle& rect) 45 { 46 using namespace std; 47 cout << "矩形信息:" << endl; 48 cout << "长: " << fixed << setprecision(2) << rect.len() << endl; 49 cout << "宽: " << fixed << setprecision(2) << rect.wide() << endl; 50 cout << "面积: " << fixed << setprecision(2) << rect.area() << endl; 51 cout << "周长: " << fixed << setprecision(2) << rect.circumference() << endl; 52 cout << endl; 53 } 54 int main() 55 { 56 rectangle rect1; 57 output(rect1); 58 rectangle rect2(10, 5); 59 output(rect2); 60 rectangle rect3(rect1); 61 rect3.resize(2); 62 output(rect3); 63 rect3.resize(5, 2); 64 output(rect3); 65 }
运行截图:

 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号