实验任务二
1.源码
1 #include<iostream> 2 using namespace std; 3 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 14 private: 15 int x , y; 16 17 }; 18 Point::Point(int x0, int y0) :x{ x0 }, y{ y0 } { cout << "constructor called" << endl; } 19 Point::Point(const Point& p) : x{ p.x }, y{ p.y } { cout << "Copy constructor called" << endl; } 20 void Point::show() const { 21 cout << "( " << x << " , " << y << " )" << endl; 22 } 23 int main() { 24 Point p1 (4,5); 25 p1.show(); 26 27 Point p2 = p1; 28 p2.show(); 29 30 Point p3{ p2 }; 31 p3.show(); 32 cout << p3.get_x() << endl; 33 }
实验任务三
源码
#include<iostream> #include<iomanip> using namespace std; class Clock { public: Clock(int h = 0, int m = 0, int s = 0); Clock(const Clock& t); ~Clock()=default; void setTime(int h, int m = 0, int s = 0); void showTime()const; private: int hour, minute, second; }; Clock::Clock(int h, int m, int s) :hour(h), minute(m), second(s) { cout << "constructor called" << endl; } Clock::Clock(const Clock& t) : hour(t.hour), minute(t.minute), second(t.second) { cout << "Copy constructor called" << endl; } void Clock::setTime(int h, int m, int s) { hour = h; minute = m; second = s; } void Clock::showTime() const { cout << setfill('0') << setw(2) << hour << ":" << setw(2) << minute << ":" << setw(2) << second << endl; } Clock reset() { return Clock(0, 0, 0); } int main() { Clock c1(12, 0, 5); c1.showTime(); c1= reset(); c1.showTime(); Clock c2(c1); c2.setTime(6); c2.showTime(); }
测试截图
实验任务四:
源码
1 #include<iostream> 2 using namespace std; 3 4 class X { 5 public: 6 X(); 7 ~X(); 8 X(int m); 9 X(const X& obj); 10 X(X&& obj)noexcept; 11 void show()const; 12 private: 13 int data; 14 }; 15 16 X::X() :data(42) { cout << "default constructor called.\n"; } 17 X::~X() { cout << "destructor called.\n"; } 18 X::X(int m) : data(m) { cout << "constructor called.\n"; } 19 X::X(const X& obj) : data(obj.data) { cout << "copy constructor called.\n"; } 20 X::X(X&&obj)noexcept:data(obj.data) { cout << "move constructor called.\n"; } 21 void X::show()const { cout << data << endl; } 22 23 int main() { 24 X x1; 25 x1.show(); 26 27 X x2{ 2049 }; 28 x2.show(); 29 30 X x3(x1); 31 x3.show(); 32 33 X x4(move(x2)); 34 x4.show(); 35 }
测试截图
分析:
在执行24行时,默认构造器被调用,调用了16行,在执行27行时,18行含参构造函数被调用,在执行30行时,19行复制构造函数被调用,在执行33行时20行移动构造函数被调用。
当程序执行到line35的 } ,析构函数被编译器自动调用,销毁和清理了对象x1、x2、x3、x4占用的资源,运行结果截图中最后4行输出,析构函数在最后程序结束前被调用了。
实验任务5:
源码
1 #include<iostream> 2 #include<iomanip> 3 using namespace std; 4 class Rectangle { 5 public: 6 Rectangle(double length = 2.0, double width = 1.0); 7 Rectangle(const Rectangle& r); 8 ~Rectangle(); 9 10 double len() { return length; }; 11 double wide() { return width; }; 12 double area() { return length*width; }; 13 double circumference() { return(width + length) * 2; }; 14 void resize(int m); 15 void resize(int m,int n); 16 //void output(const Rectangle& rect); 17 18 private: 19 double length, width; 20 }; 21 22 Rectangle::Rectangle(double l, double w) :length(l), width(w) {}; 23 Rectangle::Rectangle(const Rectangle& r) :length(r.length), width(r.width) {}; 24 Rectangle:: ~Rectangle() {}; 25 void Rectangle::resize(int m) { length *= m; width *= m; }; 26 void Rectangle::resize(int m,int n) { length *= m; width *= n; }; 27 28 void output(Rectangle& rect) { 29 cout << "矩形信息: \n"; 30 cout << fixed << setprecision(2) << "长: " << rect.len() << endl << "宽: " << rect.wide() 31 << endl << "面积: " << rect.area() << endl << "周长: " << rect.circumference() << endl << endl; 32 33 } 34 int main(){ 35 Rectangle rect1; 36 output(rect1); 37 Rectangle rect2(10, 5); 38 output(rect2); 39 Rectangle rect3(rect1); 40 rect3.resize(2); 41 output(rect3); 42 rect3.resize(5, 2); 43 output(rect3); 44 }
测试截图
标签:const,width,对象,void,int,length,实验,Rectangle From: https://www.cnblogs.com/Xl995/p/16737670.html