#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(23, 45);
p1.show();
Point p2 = p1;
p2.show();
Point p3{ p2 };
p3.show();
cout << p3.get_x() << endl;
}
![]()
#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 << "constructor called" << endl;
}
Clock::Clock(const Clock& t) :hour{ t.hour }, minute{ t.minute },
second{ t.second } {
cout << "copy constructor 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(2, 12, 8);
c1.show_time();
c1 = reset();
c1.show_time();
Clock c2(c1);
c2.set_time(10);
c2.show_time();
}
![]()
定义X x1时,默认构造函数被调用。定义X x2{ 1234 }时,自己设置的构造函数被调用,定义X x3{ x1 }时,复制构造函数被调用。定义X x4{ std::move(x2) }时,移动构造函数被调用。析构函数的调用与构造函数调用顺序相反,最先被调用的构造函数最后被调用对应的析构函数,最后被调用的构造函数最先被调用对应的析构函数。
#include<iostream>
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{ 857 } {
std::cout << "default constructor called.\n";
}
X::~X() {
std::cout << "destructor called.\n";
}
X::X(int m) :data{ m } {
std::cout << "constructor called .\n";
}
X::X(const X& obj) :data{ obj.data } {
std::cout << "abc constructor called.\n";
}
X::X(X&& obj) noexcept :data{ obj.data } {
std::cout << "move constructor called.\n";
}
void X::show()const {
std::cout << data << std::endl;
}
int main() {
X x1;
x1.show();
X x2{ 1234 };
x2.show();
X x3{ x1 };
x3.show();
X x4{ std::move(x2) };
x4.show();
}
![]()
1 #include<iostream>
2 #include<iomanip>
3
4 class Rectangle {
5 public:
6 Rectangle();
7 ~Rectangle() = default;
8 Rectangle(double l, double w);
9 Rectangle(const Rectangle& obj);
10 double len() const;
11 double wide() const;
12 double area() const;
13 double circumference()const;
14 void resize(int times);
15 void resize(int l_times, int w_times);
16 private:
17 double length, wideth;
18 };
19
20 Rectangle::Rectangle() :length{2.0}, wideth{1.0} {
21 /*std::cout << "矩形信息" << std::endl;*/
22 }
23 Rectangle::Rectangle(double l, double w) :length{ l }, wideth{ w } {
24 }
25 Rectangle::Rectangle(const Rectangle& obj) {
26 length = obj.length;
27 wideth = obj.wideth;
28 }
29 double Rectangle::len() const {
30 return length;
31 }
32 double Rectangle::wide() const{
33 return wideth;
34 }
35 double Rectangle::area() const {
36 return length * wideth;
37 }
38 double Rectangle::circumference() const {
39 return (length + wideth) * 2;
40 }
41 void Rectangle::resize(int times){
42 length = length * times;
43 wideth = wideth * times;
44 }
45
46 void Rectangle::resize(int l_times, int w_times) {
47 length = length * l_times;
48 wideth = wideth * w_times;
49 }
50 void output(const Rectangle& rect) {
51 using namespace std;
52 cout << "矩形信息:\n";
53 cout << fixed << setprecision(2);
54 std::cout << "长: " <<setw(8)<< rect.len() << "\n";
55 std::cout << "宽: " <<setw(8)<< rect.wide() << "\n";
56 std::cout << "面积:" <<setw(7)<< rect.area() << "\n";
57 std::cout << "面积:" <<setw(7)<< rect.circumference() << "\n";
58 }
59
60 int main() {
61 Rectangle rect1;
62 output(rect1);
63
64 Rectangle rect2(5,4 );
65 output(rect2);
66
67 Rectangle rect3(rect1);
68 rect3.resize(2);
69 output(rect3);
70
71 rect3.resize(6,8 );
72 output(rect3);
73
74 }
![]()