首页 > 其他分享 >实验一

实验一

时间:2022-10-03 21:46:44浏览次数:46  
标签:const Point int double 实验 Rectangle 构造函数

  • 实验任务二

   

 1 #include<iostream>
 2 
 3 using std::cout;
 4 using std::cin;
 5 using std::endl;
 6 class Point {
 7 public:
 8     Point(int x0 = 0, int y0 = 0);
 9     Point(const Point& p);
10     ~Point() = default;
11 
12     int get_x()const { return x; }
13     int get_y()const { return y; }
14     void show()const;
15 private:
16     int x, y;
17     
18 };
19     //point 类的实现
20     //构造带有默认形参值的函数
21 Point::Point(int x0, int y0) :x{x0}, y{y0}{
22     cout << "constructor called." << endl;
23 }
24     //复制构造函数
25     //参数必须是自身对象的引用类型
26 Point::Point(const Point& p) :x{ p.x }, y{ p.y }{
27     cout << "copy constructor called." << endl;
28 }
29 void Point::show()const {
30     cout << "(" << x << "," << y << ")" << endl;
31 }
32 int main()
33 {
34     int x, y;
35     cin >> x >> y;
36     Point p1(x, y);
37     p1.show();
38     Point p2 = p1; //复制构造函数被调用
39     p2.show();
40     Point p3{ p2 };//复制构造函数被调用
41     p3.show();
42     cout << p3.get_x() << endl;
43 }

  •  实验任务三
 1 #include<iostream>
 2 #include<iomanip>
 3 using std::cout;
 4 using std::endl;
 5 //定义时钟类型clock
 6 class Clock {
 7 public:
 8     Clock(int h=0, int m=0, int s=0);
 9     Clock(const Clock& t);
10     ~Clock () = default;
11     void set_time(int h , int m , int s );
12     void show_time()const;
13 private:
14     int hour, minute, 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=0, int s=0) {
23     hour = h;
24     minute = m;
25     second = s;
26 }
27 void Clock::show_time()const {
28     using std::setw;
29     using std::setfill;
30 
31     cout << setfill('0') << setw(2) << hour << ":"
32         << setw(2) << minute << ":"
33         << setw(2) << second << endl;
34 }
35 Clock reset() {
36     return Clock(0, 0, 0);
37 }
38 int main()
39 {
40     Clock c1(8,9,4);
41     c1.show_time();
42 
43     c1 = reset();
44     c1.show_time();
45 
46     Clock c2(c1);//复制构造函数被调用
47     c2.set_time(7, 5);
48     c2.show_time();
49 }

  • 实验任务四

 

 1 #include<iostream>
 2 //定义一个简单抽象类X
 3 class X {
 4 public:
 5     X();//默认构造函数
 6     ~X();//析构函数
 7     X(int m);
 8     X(const X& obj);//复制构造函数
 9     X(X&& obj)noexcept;//移动构造函数
10     void show()const;
11 private:
12     int data;
13 };
14 X::X() :data{ 42 } {
15     std::cout << "default constructor called." << std::endl;
16 }
17 X::~X() {
18     std::cout << "destructor called." << std::endl;
19 }
20 X::X(int m) :data{ m } {
21     std::cout << "constructor called." << std::endl;
22 }
23 X::X(const X& obj) :data{ obj.data } {
24     std::cout << "copy constructor called." << std::endl;
25 }
26 X::X(X&& obj)noexcept :data{ obj.data } {
27     std::cout << "move constructor called." << std::endl;
28 }
29 void X::show()const {
30     std::cout << data << std::endl;
31 }int main()
32 {
33     X x1;//默认构造函数被编译器自动调用
34     x1.show();
35     X x2{ 2049 };//构造函数被编译器自动调用
36     x2.show();
37     X x3{ x1 };//复制构造函数被编译器自动调用
38     x3.show();
39     X x4{ std::move(x2) };//移动构造函数被编译器调用
40     x4.show();
41 }

line33,调用默认构造函数X();

line35,调用构造函数X(int m);

line37,调用复制构造函数X(const X& obj);

line39,调用移动构造函数X(X&& obj)..

析构函数会在对应对象的作用域最后被调用..例如在main里声明了一个类,那么这个类就会在main结束时被调用..

  • 实验五

 

 1 #include<iostream>
 2 #include<iomanip>
 3 class Rectangle {
 4 public:
 5     Rectangle();
 6     Rectangle(double l, double w);
 7     Rectangle(const Rectangle& obj);
 8     ~Rectangle()=default;
 9     double len()const { return length; }
10     double wide()const { return width; }
11     double area() const{ return length * width; }
12     double circumference()const { return 2 * (length + width); }
13     void resize(double times);
14     void resize(double l_times, double w_times);
15 private:
16     double length,width;
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& obj):length{obj.length},width{obj.width}{}
21 void Rectangle::resize(double times)
22 {
23     length *= times;
24     width *= times;
25 }
26  void Rectangle::resize(double l_times, double w_times)
27 {
28     length *= l_times;
29     width *= w_times;
30 }
31  void output(const Rectangle& rect) {
32      using namespace std;
33      cout << "矩阵信息: " << endl;
34      cout<<fixed << setprecision(2);
35      cout.width(8);
36      cout.setf(ios::left);
37      cout << "长:"  <<rect.len() << endl;
38      cout.width(8);
39      cout.setf(ios::left);
40      cout << "宽:"  << rect.wide() << endl;
41      cout.width(8);
42      cout.setf(ios::left);
43      cout << "面积:" << rect.area() << endl;
44      cout.width(8);
45      cout.setf(ios::left);
46      cout << "周长:"  <<rect.circumference() << endl;
47      cout << endl;
48  }
49  int main()
50  {
51      Rectangle rect1;
52      output(rect1);
53      Rectangle rect2(10, 5);
54      output(rect2);
55      Rectangle rect3(rect1);
56      rect3.resize(2);
57      output(rect3);
58      rect3.resize(5, 2);
59      output(rect3);
60  }

 

标签:const,Point,int,double,实验,Rectangle,构造函数
From: https://www.cnblogs.com/zzzys/p/16740715.html

相关文章

  • 实验4:开源控制器实践——OpenDaylight
    基本要求Mininet拓扑生成并连接控制器的结果、Mininet中ping测试截图通过Postman工具调用OpenDaylight提供的API下发流表,实现拓扑内主机h1和h3网络中断10s个人总结......
  • 实验4:开源控制器实践
    一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、实验......
  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、实验......
  • 实验4:开源控制器实践——OpenDaylight
    实验4:开源控制器实践——OpenDaylight一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ub......
  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的1.能够独立完成OpenDaylight控制器的安装配置;2.能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、实......
  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的1.能够独立完成OpenDaylight控制器的安装配置;2.能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、实......
  • 实验4:开源控制器实践——OpenDaylight
    实验4:开源控制器实践——OpenDaylight一、实验要求(一)基本要求1.利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器;拓扑图:启动ODL然后打开浏览器访问htt......
  • 实验3:OpenFlow协议分析实践
    实验3:OpenFlow协议分析实践一、实验目的能够运用wireshark对OpenFlow协议数据交互过程进行抓包;能够借助包解析工具,分析与解释OpenFlow协议的数据包交互过程与机制......
  • 实验1类和对象(1)
    1.实验任务11.template指的是模板。比如,我们要写一个swap函数来交换两个元素。但这两个元素可能是int也可能使char,如果不用模板的话,我们可能要写多个函数才能完成。但是......
  • 实验3:OpenFlow协议分析实践
    实验3:OpenFlow协议分析实践一、实验目的能够运用wireshark对OpenFlow协议数据交互过程进行抓包;能够借助包解析工具,分析与解释OpenFlow协议的数据包交互过程与机制......