首页 > 其他分享 >实验1 类和对象

实验1 类和对象

时间:2022-09-28 21:55:21浏览次数:37  
标签:const Clock 对象 double int 实验 Rectangle 构造函数

 1 // Point类
 2 // 相较于教材,在构造函数的写法上,采用了业界更通用的初始化列表方式
 3 
 4 #include <iostream>
 5 
 6 using std::cout;
 7 using std::endl;
 8 
 9 // 定义Point类
10 class Point {
11 public:
12     Point(int x0 = 0, int y0 = 0);
13     Point(const Point& p);
14     ~Point() = default;
15     int get_x() const { return x; } // 内联成员函数
16     int get_y() const { return y; } // 内联成员函数
17     void show() const;
18 private:
19     int x, y;
20 };
21 
22 // Point类的实现
23 // 构造函数(带有默认形参值)
24 Point::Point(int x0, int y0) : x{ x0 }, y{ y0 } {
25     cout << "constructor called." << endl;
26 }
27 
28 // 复制构造函数
29 // 参数必须是自身对象的引用类型
30 Point::Point(const Point& p) : x{ p.x }, y{ p.y } {
31     cout << "copy constructor called." << endl;
32 }
33 
34 void Point::show() const {
35     cout << "(" << x << ", "
36         << y << ")" << endl;
37 }
38 
39 int main() {
40     Point p1(3, 7); // 构造函数被调用
41     p1.show();
42     Point p2 = p1; // 复制构造函数被调用
43     p2.show();
44     Point p3{ p2 }; // 复制构造函数被调用
45     p3.show();
46     cout << p3.get_x() << endl;
47 }

 1 // 时钟类Clock
 2 // 相较于教材,做了以下微调整:
 3 // 1. 在构造函数的写法上,采用了业界更通用的初始化列表方式
 4 // 2. 对于时钟显示的格式,使用操控符函数,控制其输出格式
 5 
 6 #include <iostream>
 7 #include <iomanip>
 8 
 9 using std::cout;
10 using std::endl;
11 
12 // 定义时钟类Clock
13 class Clock {
14 public:
15     Clock(int h = 0, int m = 0, int s = 0);
16     Clock(const Clock& t);
17     ~Clock() = default;
18     void set_time(int h, int m = 0, int s = 0);
19     void show_time() const;
20 private:
21     int hour, minute, second;
22 };
23 
24 // 类Clock实现
25 Clock::Clock(int h, int m, int s) : hour{ h }, minute{ m }, second{ s } {
26     cout << "constructor called" << endl;
27 }
28 
29 Clock::Clock(const Clock& t) : hour{ t.hour }, minute{ t.minute },
30 second{ t.second } {
31     cout << "copy constructor called" << endl;
32 }
33 
34 void Clock::set_time(int h, int m, int s) {
35     hour = h;
36     minute = m;
37     second = s;
38 }
39 
40 void Clock::show_time() const {
41     using std::setw;
42     using std::setfill;
43     cout << setfill('0') << setw(2) << hour << ":"
44         << setw(2) << minute << ":"
45         << setw(2) << second << endl;
46 }
47 
48 // 普通函数定义
49 Clock reset() {
50     return Clock(0, 0, 0); // 构造函数被调用
51 }
52 
53 int main()
54 {
55     Clock c1(4, 1, 3); // 构造函数被调用
56     c1.show_time();
57     c1 = reset(); // 理论上:复制构造函数被调用
58     c1.show_time();
59     Clock c2(c1); // 复制构造函数被调用
60     c2.set_time(6);
61     c2.show_time();
62 }

 1 #include <iostream>
 2 
 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 } {
17     std::cout << "default constructor called.\n";
18 }
19 
20 X::~X() {
21     std::cout << "destructor called.\n";
22 }
23 
24 X::X(int m) : data{ m } {
25     std::cout << "constructor called.\n";
26 }
27 
28 X::X(const X& obj) : data{ obj.data } {
29     std::cout << "copy constructor called.\n";
30 }
31 
32 X::X(X&& obj) noexcept : data{ obj.data } {
33     std::cout << "move constructor called.\n";
34 }
35 
36 void X::show() const {
37     std::cout << data << std::endl;
38 }
39 
40 int main() {
41     X x1;           //默认构造函数被编译器自动调用
42     x1.show();
43     X x2{ 2049 };
44     x2.show();           // 构造函数被编译器自动调用
45     X x3{ x1 };          // 复制构造函数被编译器自动调用
46     x3.show();
47     X x4{ std::move(x2) };    // 移动构造函数被编译器调用
48     x4.show();
49 }

task4分析:

1.在 X x1; 执行时,默认构造函数 X::X() 被编译器自动调用       

2.在 X x2{ 2049 }; 执行时,构造函数 X::X( int m ) 被编译器自动调用     

3.在 X x3{ x1 }; 执行时,复制构造函数 X::X( const X& obj) 被编译器自动调用       

4.在 X x4{ std::move(x2) }; 执行时,移动构造函数 X::X(X&& obj) noexcept  被编译器自动调用        

析构函数与构造函数的作用几乎相反,析构函数是用来完成对象被删除前的一些清理工作,也就是做扫尾工作的。因此,析构函数是在对象的生存期即将结束的时刻被编译器自动调用的。

 1 #include<iostream>
 2 #include<iomanip>
 3 
 4 // 矩形类Rectangle的定义和实现
 5 // 补足代码
 6 class Rectangle{
 7 public:
 8     Rectangle();
 9     Rectangle(double l, double w);
10     Rectangle(const Rectangle& r);
11     ~Rectangle() = default;
12     double len();
13     double wide();
14     double area();
15     double circumference();
16     void resize(int times);
17     void resize(int l_times, int w_times);
18     
19 private:
20     double length, width;
21 };
22 
23 Rectangle::Rectangle() :length{ 2.0 }, width{ 1.0 }{
24 }
25 
26 Rectangle::Rectangle(double l, double w) {
27     length = l; width = w;
28 }
29 
30 Rectangle::Rectangle(const Rectangle& r) {
31     length = r.length;
32     width = r.width;
33 }
34 
35 double Rectangle::len() { return length; }
36 double Rectangle::wide() { return width; }
37 double Rectangle::area() { return length * width; }
38 double Rectangle::circumference() { return 2 * (length + width); }
39 
40 void Rectangle::resize(int times) {
41     length *= times; width *= times;
42 }
43 
44 void Rectangle::resize(int l_times, int w_times) {
45     length *= l_times;
46     width *= w_times;
47 }
48 
49 // 普通函数,用来输出矩形信息
50 void output(const Rectangle &rect) {
51     using namespace std;
52 
53     cout << "矩形信息: \n";
54     cout << fixed << setprecision(2);     //控制输出格式:以浮点数形式输出、小数部分保留两位
55     
56     // 补足代码:分行输出矩形长、宽、面积、周长
57     Rectangle r = rect;
58     cout << "长:   " << left << r.len() << endl;
59     cout << "宽:   " << left << r.wide() << endl;
60     cout << "面积: " << left << r.area() << endl;
61     cout << "周长: " << left << r.circumference() << endl;
62     cout << endl;
63 }
64 
65 //主函数,测试Rectangle类
66 int main() {
67     Rectangle rect1;            //默认构造函数被调用
68     output(rect1);
69 
70     Rectangle rect2(10, 5);     //带有两个参数的构造函数被调用
71     output(rect2);
72 
73     Rectangle rect3(rect1);     //复制构造函数被调用
74     rect3.resize(2);            //矩形rect3的长和宽同时缩放2倍
75     output(rect3);
76 
77     rect3.resize(5, 2);         //矩形rect3的长缩放5倍,宽缩放2倍
78     output(rect3);
79 }

实验总结:

1.收获:

首先,对于类的定义与实现有了清楚的认知,并且能够在一定程度上合理的运用;

其次,我能够对于一系列的构造函数有了清楚的认识,并能够知晓在什么情况下使用他们以及在何种情况下他们会被编译器调用;

然后,我能够熟练使用重载函数,并且可以熟练运用他们来解决许多重复的问题;

最后,就是对于C++的语法与特性有了更加深入的理解。

2.尚存问题:

对于类的使用还不是特变熟练,以及一些特殊情况的处理有一定的欠缺,比如task3中的“Class reset()”普通函数的定义,对于Class的理解还不到位,以及task5中output函数的定义想了好久~

不断试错,好好努力~~

标签:const,Clock,对象,double,int,实验,Rectangle,构造函数
From: https://www.cnblogs.com/xiao-shi/p/16739702.html

相关文章

  • 实验3:OpenFlow协议分析实践
    实验3:OpenFlow协议分析实践一、实验目的能够运用wireshark对OpenFlow协议数据交互过程进行抓包;能够借助包解析工具,分析与解释OpenFlow协议的数据包交互过程与机制......
  • 实验一
    实验21#include<iostream>2usingstd::cout;3usingstd::endl;4classPoint56{7public:8Point(intx0=0,inty0=......
  • JS中面向对象编程(OOP)的基本原理——this关键字、原型链、构造函数、继承
    面向对象编程(ObjectOrientedProgramming),是软件开发过程的主要方法之一。在OOP中,使用对象和类组织代码来描述事物及其功能。虽然点符号是访问对象属性的有效方法(如myobj.......
  • 实验1
    Test1#include<iostream>#include<cstring>#include<vector>usingnamespacestd;intmain(){ strings1; strings2{"cplusplus"}; strings3{s2}; strin......
  • 实验1 类和对象(1)
    #include<iostream>usingstd::cout;usingstd::endl;classPoint{public:Point(intx0=0,inty0=0);Point(constPoint&p);~Point()=default;......
  • 实验2:Open vSwitch虚拟交换机实践
    实验2:OpenvSwitch虚拟交换机实践一、实验目的能够对OpenvSwitch进行基本操作;能够通过命令行终端使用OVS命令操作OpenvSwitch交换机,管理流表;能够通过Mininet的Pytho......
  • 实验2:Open vSwitch虚拟交换机实践
    实验2:OpenvSwitch虚拟交换机实践一、实验目的能够对OpenvSwitch进行基本操作;能够通过命令行终端使用OVS命令操作OpenvSwitch交换机,管理流表;能够通过Mininet的Pytho......
  • 实验1 类和对象
    实验任务2#include<iostream>usingnamespacestd;classPoint{public:Point(intx0=0,inty0=0);Point(constPoint&p);~Point()=default......
  • 实验一:类和对象
    实验任务2#include<iostream>usingstd::cout;usingstd::endl;classPoint{public:Point(intx0=0,inty0=0);Point(constPoint&p);~Point()=defa......
  • 实验1 类和对象
    #include<iostream>usingstd::cout;usingstd::endl;classPoint{public:Point(intx0=0,inty0=0);Point(constPoint&p);Point()=default;intget_x......