实验任务二
#include<iostream> using namespace std; class Point { public: Point(int x0 = 0, int y0 = 0); Point(const Point& p); ~Point() = default; int getX() const { return x; } int getY() 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(3,9); p1.show(); Point p2 = p1; p2.show(); Point p3{ p2 }; p3.show(); cout << p3.getX() << endl; }
实验任务三
#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 hour, int minute, int second) { this->hour = hour; this->minute = minute; this->second = second; cout << "constructor called" << endl; } Clock::Clock(const Clock& t) { this->hour = t.hour; this->minute = t.minute; this->second = t.second; cout << "copy constructor called" << endl; } void Clock::setTime(int hour, int minute, int second) { this->hour = hour; this->minute = minute; this->second = second; } 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(13, 14, 0); c1.showTime(); c1 = reset(); c1.showTime(); Clock c2(c1); c2.setTime(13); c2.showTime(); }
实验任务四
#include<iostream> using namespace std; class X { public: X(); ~X(); X(int data); X(const X& obj); X(X&& obj) noexcept; void show()const; private: int data; }; X::X() { data = 42; cout << "default constructor called." << endl; } X::~X() { cout << "destructor called." << endl; } X::X(int data) { this->data = data; cout << "constructor called."<<endl; } X::X(const X& obj) { this->data = obj.data; cout << "copy constructor called."<<endl; } X::X(X&& obj) noexcept { this->data = obj.data; cout << "move constructor called."<<endl; } 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(); }
实验分析
* line48中的x1被定义时,调用默认构造函数
* line50中的x2被定义时,调用带参数的构造函数
* line52中的x3被定义时,调用复制构造函数
* line54中的x4被定义时,调用移动构造函数
* 代码结束时,析构函数被调用x1,x2,x3,x4被逐一清理
实验任务五
#include<iostream> #include<iomanip> using namespace std; class Rectangle { public: Rectangle(); Rectangle(double l,double w); double len() const; double wide() const; Rectangle(const Rectangle& p); double area()const; double circumference()const; void resize(double times); void resize(double l_times, double w_times); private: double length, width; }; Rectangle::Rectangle() { length = 2.0; width = 1.0; } Rectangle::Rectangle(double l, double w) { length = l; width = w; } Rectangle::Rectangle(const Rectangle& p) { length = p.length; width = p.width; } double Rectangle::len()const { return length; } double Rectangle::wide() const { return width; } double Rectangle::area() const { return length * width; } double Rectangle::circumference()const { return 2 * (length + width); } void Rectangle::resize(double times) { length = length * times; width = width * times; } void Rectangle::resize(double l_times, double w_times) { length = length * l_times; width = width * w_times; } void output(const Rectangle& rect) { cout << "矩形信息: \n"; cout << fixed << setprecision(2) <<left<<setw(10)<<"长:"<< rect.len() << endl << left << setw(10) << "宽:"<<rect.wide() << endl << left << setw(10) << "面积:"<<rect.area() << endl << left << setw(10) << "周长:"<<rect.circumference() << endl<<endl; } int main() { Rectangle rect1; // 默认构造函数被调用 output(rect1); Rectangle rect2(10, 5); // 带有两个参数的构造函数被调用 output(rect2); Rectangle rect3(rect1); // 复制构造函数被调用 rect3.resize(2); // 矩形rect3的长和宽同时缩放2倍 output(rect3); rect3.resize(5, 2); // 矩形rect3的长缩放5倍, 宽缩放2倍 output(rect3); }
标签:const,int,double,第一次,width,length,实验,Rectangle From: https://www.cnblogs.com/wsfpdy-qq1650829745/p/16743422.html