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

实验一 类和对象(1)

时间:2022-09-30 00:00:47浏览次数:41  
标签:const cout Clock 对象 Point int 实验 构造函数

任务二源代码:

 1 //task2.cpp
 2 
 3 #include<iostream>
 4 using std::cout;
 5 using std::endl;
 6 
 7 //定义Point类
 8 class Point
 9 {
10     public:
11         Point(int x0=0,int y0=0);
12         Point(const Point &p);
13         ~Point()=default;
14         
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 
25 Point::Point(int x0,int y0):x{x0},y{y0}
26 {
27     cout<<"constructor called."<<endl;
28 } 
29 
30 //复制构造函数、
31 //参数必须是自身对象的引用类型
32 
33 Point::Point(const Point &p):x{p.x},y{p.y}
34 {
35     cout<<"copy constructor called."<<endl;
36  } 
37  
38  void Point::show() const
39  {
40      cout<<"("<<x<<","<<y<<")"<<endl;
41  }
42  
43  int main()
44  {
45      Point p1(9,8);//构造函数被调用 
46      p1.show();
47      
48      Point p2=p1;//复制构造函数被调用 
49      p2.show();
50      
51      Point p3{p2};//复制构造函数被调用 
52      p3.show();
53      cout<<p3.get_x()<<endl;
54  }

任务二运行结果:

 

任务三源代码:

 1 //时钟类Clock
 2 #include<iostream>
 3 #include<iomanip>
 4  using std::cout;
 5  using std::endl;
 6   
 7 //定义时钟类Clock
 8 class Clock
 9 {
10     public:
11         Clock(int h=0,int m=0, int s=0);
12         Clock(const Clock &t);
13         ~Clock()=default;
14         
15         void set_time(int h,int m=0,int s=0);
16         void show_time() const;
17         
18     private:
19         int hour,minute,second;
20  } ;
21  
22  //类clock的实现
23  Clock::Clock(int h,int m,int s): hour{h},minute{m},second{s}
24  {
25      cout<<"constructor called"<<endl;
26   } 
27   
28  Clock::Clock(const Clock &t): hour{t.hour},minute{t.minute},second{t.second}
29  {
30      cout<<"copy constructor called"<<endl;
31  }
32  
33  void Clock::set_time(int h,int m,int s)
34  {
35      hour=h;
36      minute=m;
37      second=s;
38  }
39   
40  void Clock::show_time() const
41  {
42      using std::setw;
43      using std::setfill;
44      
45      cout<<setfill('0')<<setw(2)<<hour<<":"
46     <<setw(2)<<minute<<":"
47     <<setw(2)<<second<<endl;
48  }
49  
50  //普通函数定义
51  Clock reset()
52  {
53      return Clock(0,0,0);//构造函数被调用 
54   } 
55   
56   int main()
57   {
58       Clock c1(2,8,1);//构造函数被调用 
59       c1.show_time();
60       
61       c1=reset();//理论上:复制构造函数被调用 
62       c1.show_time();
63       
64       Clock c2(c1);//复制构造函数被调用 
65       c2.set_time(8);
66       c2.show_time();
67   }

任务三运行结果截图: 

 

 

任务四源代码:

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

任务四运行结果截图:

 分析:

1.构造函数的调用及其顺序:line48-49执行时,调用默认构造函数X();line51-52执行时,调用构造函数X(int m);line54-55执行时,调用复制构造函数X(const X& obj);line57-58执行时,调用移动构造函数X(X&& obj) noexcept。

2.当对象被删除前调用析构函数,即在line58执行完后调用析构函数,顺序与构造函数(上一条)相反。

 

标签:const,cout,Clock,对象,Point,int,实验,构造函数
From: https://www.cnblogs.com/jane-d/p/16739910.html

相关文章

  • 第一次实验
    实验任务二1#include<iostream>2usingnamespacestd;3classPoint4{5public:6Point(intx0=0,inty0=0);7Point(constPoint&p);8......
  • 用spring 创建ComboPooledDataSource和JdbcTemplate对象
    用spring创建ComboPooledDataSource和JdbcTemplate对象3.1添加ioc相关jar包 <dependency><groupId>org.springframework</groupId><artifactId>spring-core<......
  • 实验2:Open vSwitch虚拟交换机实践
    实验2:OpenvSwitch虚拟交换机实践一、实验目的能够对OpenvSwitch进行基本操作;能够通过命令行终端使用OVS命令操作OpenvSwitch交换机,管理流表;能够通过Mininet的Pytho......
  • DOM文档对象模型
    目录​​DOM的概念​​​​HTMLDOM 的方法和属性​​​​代码举例​​DOM的概念DOM,全称DocumentObjectModel,是一个平台和语言都中立的接口,可以使程序和脚本能够动态访问......
  • DOM文档对象模型
    目录​​DOM的概念​​​​HTMLDOM 的方法和属性​​​​代码举例​​DOM的概念DOM,全称DocumentObjectModel,是一个平台和语言都中立的接口,可以使程序和脚本能够动态访问......
  • 第一次实验
    实验任务二#include<iostream>usingnamespacestd;classPoint{public:Point(intx0=0,inty0=0);Point(constPoint&p);~Point()=default;......
  • 实验2:Open vSwitch虚拟交换机实践
    一、实验目的能够对OpenvSwitch进行基本操作;能够通过命令行终端使用OVS命令操作OpenvSwitch交换机,管理流表;能够通过Mininet的Python代码运行OVS命令,控制网络拓扑中的......
  • 实验2:Open vSwitch虚拟交换机实践
    1.ovs-vsctl基础操作实践:创建OVS交换机,以ovs-xxxxxxxxx命名,其中xxxxxxxxx为本人学号。在创建的交换机上增加端口p0和p1,设置p0的端口号为100,p1的端口号为101,类型均为interna......
  • 实验1 类和对象(1)
    #include<iostream>#include<string>#include<vector>intmain(){usingnamespacestd;strings1;strings2{"cplusplus"};strings3{s2}......
  • 实验1 类和对象(1)
    实验任务2#include<iostream>usingstd::cout;usingstd::endl;classPoint{public:Point(intx0=0,inty0=0);Point(constPoint&p);~Point()=default......