实验1
实验任务二
1.源码
#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; }
2.测试截图


实验任务三
1.源码
#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(); }
2.测试截图


实验任务四
1.源码
#include<iostream> using namespace std; class X { public: X(); ~X(); X(int m); X(const X& obj); X(X&& obj)noexcept; void show()const; private: int data; }; X::X() :data(42) { cout << "default constructor called.\n"; } X::~X() { cout << "destructor called.\n"; } X::X(int m) : data(m) { cout << "constructor called.\n"; } X::X(const X& obj) : data(obj.data) { cout << "copy constructor called.\n"; } X::X(X&&obj)noexcept:data(obj.data) { cout << "move constructor called.\n"; } void X::show()const { cout << data << endl; } int main() { X x1; x1.show(); X x2{ 2049 }; x2.show(); X x3(x1); x3.show(); X x4(move(x2)); x4.show(); }
2.测试截图

3.分析
在执行line24时,默认构造器被调用。
调用了line16,在执行line27时,line18含参构造函数被调用。
在执行line30时,line19复制构造函数被调用。
在执行line33时line20移动构造函数被调用。
当程序执行到line35的 ,析构函数被编译器自动调用,析构函数在最后程序结束前被调用了。
实验任务五
1.源码
#include<iostream> #include<iomanip> using namespace std; class Rectangle { public: Rectangle(double length = 2.0, double width = 1.0); Rectangle(const Rectangle& r); ~Rectangle(); double len() { return length; }; double wide() { return width; }; double area() { return length*width; }; double circumference() { return(width + length) * 2; }; void resize(int m); void resize(int m,int n); private: double length, width; }; Rectangle::Rectangle(double l, double w) :length(l), width(w) {}; Rectangle::Rectangle(const Rectangle& r) :length(r.length), width(r.width) {}; Rectangle:: ~Rectangle() {}; void Rectangle::resize(int m) { length *= m; width *= m; }; void Rectangle::resize(int m,int n) { length *= m; width *= n; }; void output(Rectangle& rect) { cout << "矩形信息: \n"; cout << fixed << setprecision(2) << "长: " << rect.len() << endl << "宽: " << rect.wide() << endl << "面积: " << rect.area() << endl << "周长: " << rect.circumference() << endl << endl; } int main(){ Rectangle rect1; output(rect1); Rectangle rect2(10, 5); output(rect2); Rectangle rect3(rect1); rect3.resize(2); output(rect3); rect3.resize(5, 2); output(rect3); }
2.测试截图

浙公网安备 33010602011771号