• 类和对象(中)


    类中有6个默认成员函数,如果一个类中什么成员都没有,简称为空类。

    空类中并不是什么也没有,任何类在什么都不写时,编译器会自动生成以下6个默认成员函数。

    默认成员函数:用户没有显式实现,编译器会生成的成员函数称为默认成员函数。
    class Date {};

    一、构造函数

    1.1、构造函数的概念

    对于Date类,可以通过 Init 公有方法给对象设置日期,但如果每次创建对象时都调用该方法设置
    信息,未免有点麻烦,那能否在对象创建时,就将信息设置进去呢?
    构造函数是一个特殊的成员函数,名字与类名相同,创建类类型对象时由编译器自动调用,以保证
    每个数据成员都有 一个合适的初始值,并且在对象整个生命周期内只调用一次

    1. class Date
    2. {
    3. public:
    4. void Init(int year, int month, int day)
    5. {
    6. _year = year;
    7. _month = month;
    8. _day = day;
    9. }
    10. void Print()
    11. {
    12. cout << _year << "-" << _month << "-" << _day << endl;
    13. }
    14. private:
    15. int _year;
    16. int _month;
    17. int _day;
    18. };
    19. int main()
    20. {
    21. Date d1;
    22. d1.Init(2022, 7, 5);
    23. d1.Print();
    24. Date d2;
    25. d2.Init(2022, 7, 6);
    26. d2.Print();
    27. return 0;
    28. }

    1.2、构造函数的特性

    构造函数是特殊的成员函数,需要注意的是,构造函数虽然名称叫构造,但是构造函数的主要任
    务并不是开空间创建对象,而是初始化对象。

    特征如下:

    1.函数名与类名相同

    2.无返回值

    3.对象实例化时编译器自动调用对应的构造函数4

    4.构造函数可以重载

    1. class Date
    2. {
    3. public:
    4. // 1.无参构造函数
    5. Date()
    6. {}
    7. // 2.带参构造函数
    8. Date(int year, int month, int day)
    9. {
    10. _year = year;
    11. _month = month;
    12. _day = day;
    13. }
    14. private:
    15. int _year;
    16. int _month;
    17. int _day;
    18. };
    19. void TestDate()
    20. {
    21. Date d1; // 调用无参构造函数
    22. Date d2(2015, 1, 1); // 调用带参的构造函数
    23. // 注意:如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明
    24. // 以下代码的函数:声明了d3函数,该函数无参,返回一个日期类型的对象
    25. // warning C4930: “Date d3(void)”: 未调用原型函数(是否是有意用变量定义的?)
    26. Date d3();
    27. }

    5.如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦用户显式定义编译器将不再生成

    1. class Date
    2. {
    3. public:
    4. /*
    5. // 如果用户显式定义了构造函数,编译器将不再生成
    6. Date(int year, int month, int day)
    7. {
    8. _year = year;
    9. _month = month;
    10. _day = day;
    11. }
    12. */
    13. void Print()
    14. {
    15. cout << _year << "-" << _month << "-" << _day << endl;
    16. }
    17. private:
    18. int _year;
    19. int _month;
    20. int _day;
    21. };
    22. int main()
    23. {
    24. // 将Date类中构造函数屏蔽后,代码可以通过编译,因为编译器生成了一个无参的默认构造函
    25. //数
    26. // 将Date类中构造函数放开,代码编译失败,因为一旦显式定义任何构造函数,编译器将不再
    27. //生成
    28. // 无参构造函数,放开后报错:error C2512: “Date”: 没有合适的默认构造函数可用
    29. Date d1;
    30. return 0;
    31. }

    6.无参的构造函数和全缺省的构造函数都称为默认构造函数,并默认构造函数只能有一个。
    注意:无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为
    是默认构造函数。

    1. class Date
    2. {
    3. public:
    4. Date()
    5. {
    6. _year = 1900;
    7. _month = 1;
    8. _day = 1;
    9. }
    10. Date(int year = 1900, int month = 1, int day = 1)
    11. {
    12. _year = year;
    13. _month = month;
    14. _day = day;
    15. }
    16. private:
    17. int _year;
    18. int _month;
    19. int _day;
    20. };
    21. void Test()
    22. {
    23. Date d1;
    24. }

    二、析构函数

    2.1、析构函数的概念

    析构函数:与构造函数功能相反,析构函数不是完成对对象本身的销毁,局部对象销毁工作是由
    编译器完成的。而对象在销毁时会自动调用析构函数,完成对象中资源的清理工作。

    2.2析构函数的特性

    析构函数特征如下:
    1. 析构函数名是在类名前加上字符 ~。
    2. 无参数无返回值类型。
    3. 一个类只能有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。注意:析构函数不能重载
    4. 对象生命周期结束时,C++编译系统系统自动调用析构函数。

    1. typedef int DataType;
    2. class Stack
    3. {
    4. public:
    5. Stack(size_t capacity = 3)
    6. {
    7. _array = (DataType*)malloc(sizeof(DataType) * capacity);
    8. if (NULL == _array)
    9. {
    10. perror("malloc申请空间失败!!!");
    11. return;
    12. }
    13. _capacity = capacity;
    14. _size = 0;
    15. }
    16. void Push(DataType data)
    17. {
    18. // CheckCapacity();
    19. _array[_size] = data;
    20. _size++;
    21. }
    22. // 其他方法...
    23. ~Stack()
    24. {
    25. if (_array)
    26. {
    27. free(_array);
    28. _array = NULL;
    29. _capacity = 0;
    30. _size = 0;
    31. }
    32. }
    33. private:
    34. DataType* _array;
    35. int _capacity;
    36. int _size;
    37. };
    38. void TestStack()
    39. {
    40. Stack s;
    41. s.Push(1);
    42. s.Push(2);
    43. }

    5. 如果类中没有申请资源时,析构函数可以不写,直接使用编译器生成的默认析构函数,比如:Date类;有资源申请时,一定要写,否则会造成资源泄漏,比如Stack类。

    三、拷贝构造函数

    3.1、拷贝构造函数的概念

    拷贝构造函数:只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存
    在的类类型对象创建新对象时由编译器自动调用。

    3.2、拷贝构造函数的特征

    拷贝构造函数的特征如下:

    1. 拷贝构造函数是构造函数的一个重载形式。
    2. 拷贝构造函数的参数只有一个且必须是类类型对象的引用,使用传值方式编译器直接报错,因为会引发无穷递归调用。

    参数只有一的原因是:有一个隐藏的this指针指向一个对象,相当于有两个参数。

    1. class Date
    2. {
    3. public:
    4. Date(int year = 1900, int month = 1, int day = 1)
    5. {
    6. _year = year;
    7. _month = month;
    8. _day = day;
    9. }
    10. // Date(const Date& d) // 正确写法
    11. Date(const Date& d) // 错误写法:编译报错,会引发无穷递归
    12. {
    13. _year = d._year;
    14. _month = d._month;
    15. _day = d._day;
    16. }
    17. private:
    18. int _year;
    19. int _month;
    20. int _day;
    21. };
    22. int main()
    23. {
    24. Date d1;
    25. Date d2(d1);
    26. return 0;
    27. }

    3.若未显式定义,编译器会生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝

    1. class Time
    2. {
    3. public:
    4. Time()
    5. {
    6. _hour = 1;
    7. _minute = 1;
    8. _second = 1;
    9. }
    10. Time(const Time& t)
    11. {
    12. _hour = t._hour;
    13. _minute = t._minute;
    14. _second = t._second;
    15. cout << "Time::Time(const Time&)" << endl;
    16. }
    17. private:
    18. int _hour;
    19. int _minute;
    20. int _second;
    21. };
    22. class Date
    23. {
    24. private:
    25. // 基本类型(内置类型)
    26. int _year = 1970;
    27. int _month = 1;
    28. int _day = 1;
    29. // 自定义类型
    30. Time _t;
    31. };
    32. int main()
    33. {
    34. Date d1;
    35. // 用已经存在的d1拷贝构造d2,此处会调用Date类的拷贝构造函数
    36. // 但Date类并没有显式定义拷贝构造函数,则编译器会给Date类生成一个默认的拷贝构
    37. //造函数
    38. Date d2(d1);
    39. return 0;
    40. }
    41. // 这里会发现下面的程序会崩溃掉?这里就需要我们以后讲的深拷贝去解决。
    42. typedef int DataType;
    43. class Stack
    44. {
    45. public:
    46. Stack(size_t capacity = 10)
    47. {
    48. _array = (DataType*)malloc(capacity * sizeof(DataType));
    49. if (nullptr == _array)
    50. {
    51. perror("malloc申请空间失败");
    52. return;
    53. }

    注意:在编译器生成的默认拷贝构造函数中,内置类型是按照字节方式直接拷贝的,而自定
    义类型是调用其拷贝构造函数完成拷贝的

     

    4.编译器生成的默认拷贝构造函数已经可以完成字节序的值拷贝了,看到这大家是不是就有一个疑惑,既然编译器都能自己实现拷贝,那我们还需要自己手写拷贝构造函数吗?

    注意:类中如果没有涉及资源申请时,拷贝构造函数是否写都可以;一旦涉及到资源申请
    时,则拷贝构造函数是一定要写的,否则就是浅拷贝。

     

    5.拷贝构造函数典型调用场景

    · 使用已存在对象创建新对象
    · 函数参数类型为类类型对象
    · 函数返回值类型为类类型对象

     

    1. class Date
    2. {
    3. public:
    4. Date(int year, int minute, int day)
    5. {
    6. cout << "Date(int,int,int):" << this << endl;
    7. }
    8. Date(const Date& d)
    9. {
    10. cout << "Date(const Date& d):" << this << endl;
    11. }
    12. ~Date()
    13. {
    14. cout << "~Date():" << this << endl;
    15. }
    16. private:
    17. int _year;
    18. int _month;
    19. int _day;
    20. };
    21. Date Test(Date d)
    22. {
    23. Date temp(d);
    24. return temp;
    25. }
    26. int main()
    27. {
    28. Date d1(2022,1,13);
    29. Test(d1);
    30. return 0;
    31. }

    为了提高程序效率,一般对象传参时,尽量使用引用类型,返回时根据实际场景,能用引用
    尽量使用引用。

    四、赋值运算符重载

    4.1、运算符重载概念

    运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。

    函数名字:operator后面接需要重载的运算符符号

    函数原型:返回类型operator操作符(参数列表)

    注意:

    不能通过连接其他符号来创建新的操作符:比如operator@
    重载操作符必须有一个类类型参数
    用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不 能改变其含义
    作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐
    藏的this
    .*   ::   sizeof   ?:  .   注意以上5个运算符不能重载。
     

    1. class Date
    2. {
    3. public:
    4. Date(int year = 1900, int month = 1, int day = 1)
    5. {
    6. _year = year;
    7. _month = month;
    8. _day = day;
    9. }
    10. //private:
    11. int _year;
    12. int _month;
    13. int _day;
    14. };
    15. // 这里会发现运算符重载成全局的就需要成员变量是公有的,那么问题来了,封装性如何保证?
    16. // 这里其实可以用我们后面学习的友元解决,或者干脆重载成成员函数。
    17. bool operator==(const Date& d1, const Date& d2)
    18. {
    19. return d1._year == d2._year
    20. && d1._month == d2._month
    21. && d1._day == d2._day;
    22. }
    23. void Test ()
    24. {
    25. Date d1(2018, 9, 26);
    26. Date d2(2018, 9, 27);
    27. cout<<(d1 == d2)<
    28. }
    29. class Date
    30. {
    31. public:
    32. Date(int year = 1900, int month = 1, int day = 1)
    33. {
    34. _year = year;
    35. _month = month;
    36. _day = day;
    37. }
    38. // bool operator==(Date* this, const Date& d2)
    39. // 这里需要注意的是,左操作数是this,指向调用函数的对象
    40. bool operator==(const Date& d2)
    41. {
    42. return _year == d2._year;
    43. && _month == d2._month
    44. && _day == d2._day;
    45. }
    46. private:
    47. int _year;
    48. int _month;
    49. int _day;
    50. };

    4.2赋值运算符重载

    1. 赋值运算符重载格式
    参数类型:const T&,传递引用可以提高传参效率
    返回值类型:T&,返回引用可以提高返回的效率,有返回值目的是为了支持连续赋值
    检测是否自己给自己赋值
    返回*this :要复合连续赋值的含义

    1. class Date
    2. {
    3. public :
    4. Date(int year = 1900, int month = 1, int day = 1)
    5. {
    6. _year = year;
    7. _month = month;
    8. _day = day;
    9. }
    10. Date (const Date& d)
    11. {
    12. _year = d._year;
    13. _month = d._month;
    14. _day = d._day;
    15. }
    16. Date& operator=(const Date& d)
    17. {
    18. if(this != &d)
    19. {
    20. _year = d._year;
    21. _month = d._month;
    22. _day = d._day;
    23. }
    24. return *this;
    25. }
    26. private:
    27. int _year ;
    28. int _month ;
    29. int _day ;
    30. };

    2. 赋值运算符只能重载成类的成员函数不能重载成全局函数

    原因:赋值运算符如果不显式实现,编译器会生成一个默认的。此时用户再在类外自己实现
    一个全局的赋值运算符重载,就和编译器在类中生成的默认赋值运算符重载冲突了,故赋值
    运算符重载只能是类的成员函数

    3. 用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝。

    注意:内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类的赋值运算符重载完成赋值。
    还是那个问题,编译器既然可以自己实现赋值运算,那我们是否还要自己写赋值运算符重载函数呢?

    注意:如果类中未涉及到资源管理,赋值运算符是否实现都可以;一旦涉及到资源管理则必
    须要实现。

    4.3、前置++和后置++重载

    前置++:

    1. class Date
    2. {
    3. public:
    4. Date(int year = 1900, int month = 1, int day = 1)
    5. {
    6. _year = year;
    7. _month = month;
    8. _day = day;
    9. }
    10. // 前置++:返回+1之后的结果
    11. // 注意:this指向的对象函数结束后不会销毁,故以引用方式返回提高效率
    12. Date& operator++()
    13. {
    14. _day += 1;
    15. return *this;
    16. }

    后置++

    前置++和后置++都是一元运算符,为了让前置++与后置++形成能正确重载
    C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器
    自动传递
    注意:后置++是先使用后+1,因此需要返回+1之前的旧值,故需在实现时需要先将this保存
    一份,然后给this+1
    而temp是临时对象,因此只能以值的方式返回,不能返回引用
     

    1. Date operator++(int)
    2. {
    3. Date temp(*this);
    4. _day += 1;
    5. return temp;
    6. }

    五、取地址操作符重载

    这个默认成员函数一般不用重新定义,编译器默认生成

    1. class Date
    2. {
    3. public :
    4. Date* operator&()
    5. {
    6. return this ; //想让别人获取到指定的内容,可以把this换成其他Date成员
    7. //不想让人访问地址时可以直接 return nullptr
    8. }
    9. const Date* operator&()const
    10. {
    11. return this ;
    12. }
    13. private :
    14. int _year ; // 年
    15. int _month ; // 月
    16. int _day ; // 日
    17. };

    六、const取地址重载

    这个默认成员函数一般不用重新定义,编译器默认生成

    1. const Date* operator&()const
    2. {
    3. return this ;
    4. }

    七、const成员

    将const修饰的“成员函数”称之为const成员函数,const修饰类成员函数,实际修饰该成员函数
    隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。

    下面是编译器对const成员函数的处理

    1. class Date
    2. {
    3. public:
    4. Date(int year, int month, int day)
    5. {
    6. _year = year;
    7. _month = month;
    8. _day = day;
    9. }
    10. void Print()
    11. {
    12. cout << "Print()" << endl;
    13. cout << "year:" << _year << endl;
    14. cout << "month:" << _month << endl;
    15. cout << "day:" << _day << endl << endl;
    16. }
    17. void Print() const
    18. {
    19. cout << "Print()const" << endl;
    20. cout << "year:" << _year << endl;
    21. cout << "month:" << _month << endl;
    22. cout << "day:" << _day << endl << endl;
    23. }
    24. private:
    25. int _year; // 年
    26. int _month; // 月
    27. int _day; // 日
    28. };
    29. void Test()
    30. {
    31. Date d1(2022,1,13);
    32. d1.Print();
    33. const Date d2(2022,1,13);
    34. d2.Print();
    35. }

    注:

    1. const对象不可以调用非const成员函数
    2. 非const对象可以调用const成员函数
    3. const成员函数内不可以调用其它的非const成员函数
    4. 非const成员函数内可以调用其它的const成员函数
    const成员权限只可以缩小不可以放大 

  • 相关阅读:
    Appium问题及解决:打开Appium可视化界面,点击搜索按钮,提示inspectormoved
    基于Java的药品管理系统设计与实现(源码+lw+部署文档+讲解等)
    redis底层数据结构之ziplist
    Plato Farm全新玩法,套利ePLATO稳获超高收益
    Python之第八章 模块和包 --- 模块
    docker run和docker start的区别
    智能网联汽车云控系统第5部分:平台服务场景规范
    java毕业设计校园商铺mybatis+源码+调试部署+系统+数据库+lw
    一幅长文细学Vue(二)——Vue基础入门
    RK3568-pcie接口
  • 原文地址:https://blog.csdn.net/m0_69323023/article/details/133846424