实验 2
#include<iostream> using namespace std; 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(100, 13); 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, min, sec; }; Clock::Clock(int h, int m, int s) :hour{ h }, min{ m }, sec{ s } { cout << "constructor called" << endl; } Clock::Clock(const Clock& t) :hour{ t.hour }, min{ t.min }, sec{ t.sec } { cout << "copy constructor called" << endl; } void Clock::set_time(int h, int m, int s) { hour = h; min = m; sec = s; } void Clock::show_time() const { using std::setw; using std::setfill; cout << setfill('0') << setw(2) << hour << ":" << setw(2) << min << ":" << setw(2) << sec << endl; } Clock reset() { return Clock(0, 0, 0); } int main() { Clock c1(1, 50, 5); c1.show_time(); c1 = reset(); c1.show_time(); Clock c2(c1); c2.set_time(2,10,2); c2.show_time(); }
实验4
#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 }
实验5
#include <iostream> #include <iomanip> class Rectangle { public: Rectangle(double l=2.0 ,double w=1.0 ); void output(const Rectangle& rect); Rectangle(const Rectangle& obj); void area(double l, double w); void circumference(double l, double w); void resize(int k,int j=2); double length, width, are, cir; }; Rectangle::Rectangle(double l, double w) :length{ l }, width{ w } { area(l, w); circumference(l,w); } void Rectangle::area(double l,double w){ are = l * w; } void Rectangle::circumference(double l, double w) { cir = 2 * (l + w); } Rectangle::Rectangle(const Rectangle& obj) : length{ obj.length }, width{ obj.width },are{obj.are},cir{obj.cir} { } void Rectangle::resize(int k,int j) { length = length * k; width = width * j; area(length, width); circumference(length, width); } void output(const Rectangle& rect) { using namespace std; cout << "矩形信息: \n"; cout << fixed << setprecision(2) << setw(5); cout << endl; cout << "长: " << rect.length << endl; cout << "宽: " << rect.width << endl; cout << "面积:" << rect.are << endl; cout << "周长:" << rect.cir << 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); }
标签:const,int,double,void,width,实验,Rectangle From: https://www.cnblogs.com/qazmlp10/p/16754111.html