实验一 类与对象
实验任务2
使用C++语言特性中支持面向对象的语法,实现一个Point类来描述点的基础属性和操作。
1 #include<iostream> 2 using namespace std; 3 class Point { 4 public: 5 Point(int x0 = 1, int y0 = 2); 6 Point(const Point& p); 7 ~Point() = default; 8 int get_x()const { return x; } 9 int get_y()const { return y; } 10 void show()const; 11 private: 12 int x, y; 13 }; 14 Point::Point(int x0, int y0) :x{ x0 }, y{ y0 }{ 15 cout << "constrctor called" << endl; 16 } 17 Point::Point(const Point& p) : x{ p.x }, y{ p.y }{ 18 cout << "copy constrctor called" << endl; 19 } 20 void Point::show()const { 21 cout << "(" << x << "," << y << ")" << endl; 22 } 23 int main() { 24 Point p1; 25 p1.show(); 26 Point p2(4, 5); 27 p2.show(); 28 Point p3 = p2; 29 p3.show(); 30 Point p4{ p3 }; 31 p4.show(); 32 cout << p4.get_x() << endl; 33 }

更改数据后

实验任务3
使用C++语言特性中支持面向对象的语法,实现一个简单时钟类Clock。通过它实现对时钟 的设置、显示等基础操作。
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 class Clock { 5 public: 6 Clock(int h = 0, int m = 0, int s = 0); 7 Clock(const Clock& t); 8 ~Clock() = default; 9 void set_time(int h, int m = 0, int s = 0); 10 void show_time()const; 11 private: 12 int hour; 13 int minute; 14 int second; 15 }; 16 Clock::Clock(int h, int m, int s) :hour{ h }, minute{ m }, second{ s }{ 17 cout << "constructor called" << endl; 18 } 19 Clock::Clock(const Clock& t) : hour{ t.hour }, minute{ t.minute }, second{ t.second }{ 20 cout << "copy constructor called" << endl; 21 } 22 void Clock::set_time(int h, int m, int s) { 23 hour = h; 24 minute = m; 25 second = s; 26 } 27 void Clock::show_time()const { 28 cout << setfill('0') << setw(2) << hour << ":" << setw(2) << minute << ":" << setw(2) << second << endl; 29 } 30 Clock reset() { 31 return Clock (0, 0, 0); 32 } 33 int main() { 34 Clock c1(12, 0, 5); 35 c1.show_time(); 36 c1 = reset(); 37 c1.show_time(); 38 Clock c2(c1); 39 c2.show_time(); 40 41 }

更改数据后

实验任务4
使用C++语言特性中支持面向对象的语法,实现一个抽象类X。它是对类和对象的基础操作 的一个抽象概括,覆盖了类的定义、实现、测试,包括各类构造函数、析构函数及其调用时机的观察。
1 #include <iostream> 2 3 class X { 4 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 X::X() : data{ 42 } { 16 std::cout << "default constructor called.\n"; 17 } 18 X::~X() { 19 std::cout << "destructor called.\n"; 20 } 21 X::X(int m) : data{ m } { 22 std::cout << "constructor called.\n"; 23 } 24 X::X(const X& obj) : data{ obj.data } { 25 std::cout << "copy constructor called.\n"; 26 } 27 X::X(X&& obj) noexcept : data{ obj.data } { 28 std::cout << "move constructor called.\n"; 29 } 30 void X::show() const { 31 std::cout << data << std::endl; 32 } 33 34 int main() { 35 X x1; 36 x1.show(); 37 X x2{ 2049 }; 38 x2.show(); 39 X x3{ x1 }; 40 x3.show(); 41 X x4{ std::move(x2) }; 42 x4.show(); 43 }

X x1; 默认构造函数调用;
X x2{ 2049 };构造函数被调用;
X x3{ x1 };复制构造函数被调用;
X x4{ std::move(x2) }; 移动构造函数被调用;
析构函数在对象的生存期即将结束的时候被调用;
即在 show() const; 之后调用;
实验任务5
设计并实现一个矩形类Rectangle.
1 #include<bits/stdc++.h> 2 using namespace std; 3 class Rectangle { 4 private: 5 double length, width; 6 public: 7 Rectangle(); 8 Rectangle(double l, double w); 9 Rectangle(const Rectangle& r); 10 ~Rectangle() = default; 11 double len()const{ return length; } 12 double wide()const{ return width; } 13 double area()const{ return length * width; } 14 double circumference()const{ return 2 * (length + width); } 15 void resize(double times); 16 void resize(double l_times, double w_times); 17 }; 18 Rectangle::Rectangle() :length{ 2.0 }, width{ 1.0 }{}; 19 Rectangle::Rectangle(double l, double w) :length{ l }, width{ w }{}; 20 Rectangle::Rectangle(const Rectangle& r) :length{ r.length }, width{ r.width }{}; 21 void Rectangle::resize(double times) { 22 length *= times; 23 width *= times; 24 } 25 void Rectangle::resize(double l_times, double w_times) { 26 length *= l_times; 27 width *= w_times; 28 } 29 void output(const Rectangle& rect) { 30 using namespace std; 31 cout << fixed << setprecision(2); 32 cout << "矩阵信息:" << endl; 33 cout << left<< setw(8) << "长:" 34 << rect.len()<<endl; 35 cout <<left << setw(8) << "宽:" 36 << rect.wide() << endl; 37 cout << left<< setw(8) << "面积:" 38 << rect.area() << endl; 39 cout << left<< setw(8) << "周长:" 40 <<rect.circumference() << endl; 41 cout << endl; 42 43 } 44 int main() { 45 Rectangle rect1; 46 47 output(rect1); 48 Rectangle rect2(10, 5); 49 50 output(rect2); 51 Rectangle rect3(rect1); 52 53 rect3.resize(2); 54 55 output(rect3); 56 rect3.resize(5, 2); 57 58 output(rect3); 59 }

浙公网安备 33010602011771号