首页 > 其他分享 >类和对象

类和对象

时间:2022-09-30 21:34:05浏览次数:40  
标签:const Clock 对象 float int Rectangle 构造函数

 实验任务2:
1 // Point类 2 // 相较于教材,在构造函数的写法上,采用了业界更通用的初始化列表方式 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 // 定义Point类 7 class Point { 8 public: 9 Point(int x0 = 0, int y0 = 0); 10 Point(const Point& p); 11 ~Point() = default; 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 // Point类的实现 19 // 构造函数(带有默认形参值) 20 Point::Point(int x0, int y0) : x{ x0 }, y{ y0 } { 21 cout << "constructor called." << endl; 22 } 23 // 复制构造函数 24 // 参数必须是自身对象的引用类型 25 Point::Point(const Point& p) : x{ p.x }, y{ p.y } { 26 cout << "copy constructor called." << endl; 27 } 28 void Point::show() const { 29 cout << "(" << x << ", " 30 << y << ")" << endl; 31 } 32 int main() { 33 Point p1(4, 5); // 构造函数被调用 34 p1.show(); 35 Point p2 = p1; // 复制构造函数被调用 36 p2.show(); 37 Point p3{ p2 }; // 复制构造函数被调用 38 p3.show(); 39 cout << p3.get_x() << endl; 40 }
 实验任务三:
1 // 时钟类Clock 2 // 相较于教材,做了以下微调整: 3 // 1. 在构造函数的写法上,采用了业界更通用的初始化列表方式 4 // 2. 对于时钟显示的格式,使用操控符函数,控制其输出格式 5 #include <iostream> 6 #include <iomanip> 7 using std::cout; 8 using std::endl; 9 // 定义时钟类Clock 10 class Clock { 11 public: 12 Clock(int h = 0, int m = 0, int s = 0); 13 Clock(const Clock& t); 14 ~Clock() = default; 15 void set_time(int h, int m = 0, int s = 0); 16 void show_time() const; 17 private: 18 int hour, minute, second; 19 }; 20 // 类Clock实现 21 Clock::Clock(int h, int m, int s) : hour{ h }, minute{ m }, second{ s } { 22 cout << "constructor called" << endl; 23 } 24 Clock::Clock(const Clock& t) : hour{ t.hour }, minute{ t.minute }, 25 second{ t.second } { 26 cout << "copy constructor called" << endl; 27 } 28 void Clock::set_time(int h, int m, int s) { 29 hour = h; 30 minute = m; 31 second = s; 32 } 33 void Clock::show_time() const { 34 using std::setw; 35 using std::setfill; 36 cout << setfill('0') << setw(2) << hour << ":" 37 << setw(2) << minute << ":" 38 << setw(2) << second << endl; 39 } 40 // 普通函数定义 41 Clock reset() { 42 return Clock(0, 0, 0); // 构造函数被调用 43 } 44 int main() 45 { 46 Clock c1(12, 0, 5); // 构造函数被调用 47 c1.show_time(); 48 c1 = reset(); // 理论上:复制构造函数被调用 49 c1.show_time(); 50 Clock c2(c1); // 复制构造函数被调用 51 c2.set_time(6); 52 c2.show_time(); 53 }
 实验任务四:
1 #include <iostream> 2 // 定义一个简单抽象类 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.\n"; 16 } 17 X::~X() { 18 std::cout << "destructor called.\n"; 19 } 20 X::X(int m) : data{ m } { 21 std::cout << "constructor called.\n"; 22 } 23 X::X(const X& obj) : data{ obj.data } { 24 std::cout << "copy constructor called.\n"; 25 } 26 X::X(X&& obj) noexcept : data{ obj.data } { 27 std::cout << "move constructor called.\n"; 28 } 29 void X::show() const { 30 std::cout << data << std::endl; 31 } 32 int main() { 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 }
代码 X x1调用默认构造函数
代码 X X2{2049}调用有参构造函数
代码 X x3{x1}是利用已有对象初始化新对象,调用拷贝构造函数
代码 X x4{std::move(x2)}将x4转化成右值调用移动构造函数;
析构函数调用时期为对象被销毁之前调用,代码中即为main函数结束,析构函数被调用,每个对象的析构函数调用顺序为:先初始化的对象后析构,后初始化的对象先析构。


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

实验总结:

1.构造函数分类:普通构造和拷贝构造

2.调用拷贝构造调用时期:

利用已用对象去初始化新的对象

当函数参数为引用对象的时候

当函数返回值为对象本身的时候

3.构造函数调用方法:

例如创建一个person类型,声明对象 person p

person()为匿名对象,执行之后系统立马收回

person p()为默认构造

person p(参数)为有参构造

person p(对象)为拷贝构造

4.在创建一个对象的时候编译器会自动提供三个函数:无参构造,有参构造,拷贝构造函数

当用户自定义一个有参构造函数时,编译器不提供无参构造

当用户自定义一个拷贝构造函数时,编译器不提供无参和有参构造

5在对象里面声明函数,在对象外定义函数时,必须用二元运算符::

 

 

 

 

标签:const,Clock,对象,float,int,Rectangle,构造函数
From: https://www.cnblogs.com/1916wenle/p/16746304.html

相关文章

  • 学习笔记——Django项目中的F对象,Q对象,聚合函数,排序
    2022-09-30F对象:在shell中是用于两个有关联的属性之间的查询。使用实例:查询书籍表中阅读量大于评论量的记录前提,进入pycharm,进入虚拟环境,进入shell环境。首先,要......
  • 关于对象存储服务OBS,你真的了解么?
    自2015年提出“互联网+”以来,互联网信息技术对于企业发展的帮助作用越来越大,众多企业通过借助网络信息技术实现利润增值,企业对于网络信息数据也越来越依赖。 但网络信息数......
  • net List集合 只改变对象中的某一个属性
    classStudent{publicstringId{get;set;}publicstringName{get;set;}}List<Student>list=newList<Student>()list.add(newStudent{Id=1,Name="名......
  • python基础: 类和对象
    类里面定义的叫方法。类外面定义的叫函数  静态方法:不允许加self类方法:必须加cls实例方法:必须加self ......
  • 面向对象
    创建对象内存分析简单小结类与对象......
  • 实验一面向对象
      #include<iostream>usingnamespacestd;classpoint{public:point(intx0=0,inty0=0);point(constpoint&p);~point()=default;intget_x()con......
  • 实验一 类和对象
    实验任务二:#include<bits/stdc++.h>usingnamespacestd;classPoint{public:Point(intx0=0,inty0=0);Point(constPoint&p);~P......
  • List转为String数组 List对象.toArray(new String[0])
    List转为String数组List对象.toArray(newString[0])privateString[]getStringArray(){returnnewString[]{"one","two","three"};}@Testpublicvoidtes......
  • 实验1 类和对象(1)
    #include<iostream>#include<string>#include<vector>intmain(){usingnamespacestd;strings1;strings2{"cplusplus"};strings3{s2......
  • 2022-09-30 数据源对象,容器
    目录spring数据源对象管理DruidDataSourceComboPooledDataSource加载properties文件容器创建容器获取bean核心容器总结容器相关bean相关依赖注入相关spring数据源对象管......