实验任务2
#include<iostream> using std::cout; using std::endl; class Point{ public: Point(int x0 = 0, int y0 = 0); Point(const Point &p); ~Point() = default; int get_x() const { return x;}; int get_y() const { return y;}; void show() const; private: int x, y; }; Point::Point(int x0, int y0): x{x0} , y{y0}{ cout << "constructor called." << endl; } Point::Point(const Point & p) : x{p.x}, y{p.y}{ cout << "copy constructor called." << endl; } void Point::show() const { cout << "(" << x << "," << y << ")" << endl; } int main(){ Point p1(4, 5); p1.show(); Point p2 = p1; p2.show(); Point p3{p2}; p3.show(); cout << p3.get_x() << endl; }
实验任务3
#include<iostream> #include<iomanip> using std::cout; using std::endl; class Clock { public: Clock(int h = 0, int m = 0, int s = 0); Clock(const Clock& t); ~Clock() = default; void set_time(int h, int m = 0, int s = 0); void show_time()const; private: int hour, minute, second; }; Clock::Clock(int h, int m, int s): hour{ h }, minute{ m }, second{ s } { cout << "copy constructor called" << endl; } Clock::Clock(const Clock& t) :hour{ t.hour }, minute{ t.minute }, second{ t.second } { cout << "copy consttructor called" << endl; } void Clock::set_time(int h, int m, int s) { hour = { h }, minute = { m }, second = { s }; } void Clock::show_time()const { using std::setw; using std::setfill; cout << setfill('0') << setw(2) << hour << ":" << setw(2) << minute << ":" << setw(2) << second << endl; } Clock reset() { return Clock(0, 0, 0); } int main() { Clock c1(17, 29, 45); c1.show_time(); c1 = reset(); c1.show_time(); Clock c2(c1); c2.set_time(6); c2.show_time(); }
实验任务4:
#include<iostream> class X { public: X(); ~X(); X(int m); X(const X& obj); X(X&& obj)noexcept; void show()const; private : int date; }; X::X() :date{ 42 } { std::cout << "default constructor called.\n"; } X::X(int m) :date{ m } { //date = m; std::cout << "constructor called.\n"; } X::~X() { std::cout << "destructor called.\n"; } X::X(const X& obj) :date{ obj.date } { std::cout << "copy destructor called.\n"; } X::X(X&& obj)noexcept :date{ obj.date } { std::cout << "move destructor called.\n"; } void X::show()const { std::cout << date << std::endl; } int main() { X x1; x1.show(); X x2{ 2049 }; x2.show(); X x3{ x1 }; x3.show(); X x4{ std::move(x2) }; x4.show(); }
在执行 X x1;时,不带参数的默认构造函数被调用;
在执行 X x2{2049};时,带参数的构造函数被调用;
在执行 X x3{x1};时,复制构造函数被调用;
在执行 X x4{std::move(x2)};时,移动构造函数被调用,将x2 的值给了x4;
通过运行结果可知,析构函数是在所有程序执行完毕后,才被调用的,将x1 x2 x3 x4所占的资源与对象进行了销毁。
实验任务五 源代码及运行结果
1 #include<iostream> 2 #include<iomanip> 3 class Rectangle{ 4 public: 5 Rectangle(); 6 Rectangle(double l, double w); 7 Rectangle(Rectangle& obj); 8 9 double len()const { return length; } 10 double wide()const { return width; } 11 double area() const { return length * width; } 12 double circumference () const { return 2 * (length + width); } 13 void resize(int times); 14 void resize(int l_times, int w_times); 15 ; private: 16 double length, width; 17 }; 18 Rectangle::Rectangle( ) { 19 length = 2.0; 20 width = 1.0; 21 } 22 Rectangle::Rectangle(double l, double w) { 23 length = l; 24 width = w; 25 } 26 27 Rectangle::Rectangle ( Rectangle& rect){ 28 length = rect.length; 29 width = rect.width; 30 } 31 void Rectangle::resize(int times) { 32 length = length * times; 33 width = width * times; 34 } 35 void Rectangle::resize(int l_times, int w_times) { 36 length = length * l_times; 37 width = width * w_times; 38 } 39 //普通函数,用于输出矩阵信息 40 void output(const Rectangle& rect) 41 { 42 using namespace std; 43 cout << "矩形信息:" << endl; 44 cout << "长: " << fixed << setprecision(2) << rect.len() << endl; 45 cout << "宽: " << fixed << setprecision(2) << rect.wide() << endl; 46 cout << "面积: " << fixed << setprecision(2) << rect.area() << endl; 47 cout << "周长: " << fixed << setprecision(2) << rect.circumference() << endl; 48 cout << endl; 49 } 50 int main() { 51 Rectangle rect1; 52 output(rect1); 53 Rectangle rect2(10, 5); // 带有两 55 Rectangle rect3(rect1); // 复制构造函数被调用 56 rect3.resize(2); // 矩形rect3的长和宽同时缩放2倍 57 output(rect3); 58 rect3.resize(5, 2); // 矩形rect3的长缩放5倍, 宽缩放2倍 59 output(rect3); 60 }
标签:const,width,对象,times,int,length,实验,Rectangle From: https://www.cnblogs.com/aomijia/p/16743341.html