小工具知识网

当前位置: 首页 > 行业知识 > C++类与对象(中)

行业知识

C++类与对象(中)

2026-11-17 09:01:57 来源:互联网转载

文章目录

  • 1.类的6个默认成员函数
  • 2.类的构造函数
  • 3.析构函数
  • 4. 拷贝构造函数
  • 5.赋值运算符和运算符重载
  • 6.日期类实现
  • 7.const成员
  • 8.重载流插入>
    • 1.流插入
    • 2.流提取
    • 9.取地址及const取地址���作符重载

      1.类的6个默认成员函数

      空类:也就是什么成员都没有的类。

      但事实上,空类中真的什么都没有吗?并不是,任何类在什么都不写时,编译器会自动生成以下6个默认成员函数。

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

      2.类的构造函数

      概念:

      构造函数:创建类类型对象时由编译器自动调用,以保证每个数据成员都有一个合适的初始值,并且在对象整个生命周期内只调用一次

      #include using namespace std;class Date{public:// 以下构造函数只能存在一个!!!    // 无参默认构造函数    Date()    {        _year = 1900;        _month = 1;        _day = 1;    }    // 全缺省默认构造函数    Date(int year = 1900, int month = 1, int day = 1)    {        _year = year;        _month = month;        _day = day;    }    // 编译器生成的默认构造函数    // C++11补丁新增内置类型成员变量(int char double 指针....)在类中声明时可以给默认值,此处仍是声明不是定义!!!private:    int _year = 0;    int _month = 0;    int _day = 0;};// 以下测试函数能通过编译吗?void Test(){    Date d1;}int main(){    Test();    return 0;}

      特性:

      • 函数名与类名相同,无返回值类型
      • 对象实例化时编译器自动调用对应的构造函数
      • 构造函数可以重载。
      • 构造函数的并不是开辟空间创建对象,而是初始化对象
      • 如果类中没有显式定义构造函数,则编译器会自动生成一个无参的默认构造函数,而用户显式定义编译器将不再生成
      • 不实现构造函数的情况下,编译器会生成默认的构造函数,此默认构造函数对内置类型不起作用,只对自定义类型初始化
      • C++11中针对内置类型成员不初始化的缺陷,又打了补丁:内置类型成员变量在类中声明时可以给默认值。

        3.析构函数

        概念:

        析构函数:与构造函数功能相反,析构函数不是完成对对象本身的销毁,局部对象销毁工作是由编译器在栈区上完成的。而对象在销毁时会自动调用析构函数,完成对象中资源的清理工作,一般清理的是堆上malloc开辟出来的空间

        • 函数名是在类名前加上字符 ~,无参数,无返回值类型
        • 一个类只能有一个析构函数。若未显式定义,系统会自动生成默认的析构函数,析构函数不能重载
        • 对象生命周期结束时,C++编译系统系统自动调用析构函数
        • 如果类中没有申请资源时,析构函数可以不写,直接使用编译器生成的默认析构函数,有资源申请时,一定要写,否则会造成资源泄漏,比如Stack类需要动态开辟空间
          #include using namespace std;class stack{public:void Push(const int& data){// CheckCapacity();_array[_size] = data;_size++;}// 半缺省构造函数stack(int capacity = 10){cout _year = d._year;this->_month = d._month;this->_day = d._day;}return *this;}Date Date::operator+(int day) const{Date tmp = *this;// 赋值重载拷贝防止原来的日期被改变tmp._day = tmp._day + day;int monthday = Date::GetMonthDay(tmp._year, tmp._month);if (tmp._day  monthday){monthday = Date::GetMonthDay(tmp._year, tmp._month);tmp._day = tmp._day - monthday;if (tmp._month _day = this->_day + day;int monthday = Date::GetMonthDay(this->_year, this->_month);if (this->_day _day > monthday){monthday = Date::GetMonthDay(this->_year, this->_month);this->_day = this->_day - monthday;if (this->_month _month++;}else{this->_month = 1;this->_year++;}}return *this;}Date Date::operator-(int day) const{Date tmp = *this;// 赋值重载拷贝防止原来的日期被改变tmp._day = tmp._day - day;int monthday = Date::GetMonthDay(tmp._year, tmp._month);if (tmp._day > 0){return tmp;}while (tmp._day _day = this->_day - day;int monthday = Date::GetMonthDay(this->_year, this->_month);if (this->_day > 0){return *this;}while (this->_day _month == 1){this->_month = 12;this->_year--;}else{this->_month--;}monthday = Date::GetMonthDay(this->_year, this->_month);this->_day = this->_day + monthday;}return *this;}// 前置++Date& Date::operator++(){this->_day++;int monthday = Date::GetMonthDay(this->_year, this->_month);if (this->_day > monthday){this->_month++;this->_day = this->_day - monthday;if (this->_month > 12){this->_month = 1;this->_year++;}}// 返回的是对象++后的全新的对象return *this;}// 后置++Date Date::operator++(int)// 注意为何返回值区分了对象本身和对象的引用,在后置++中我们返回的是tmp这个局部变量因此只能返回局部对象的拷贝而不能返回引用{Date tmp = *this;this->_day++;int monthday = Date::GetMonthDay(this->_year, this->_month);if (this->_day > monthday){this->_month++;this->_day = this->_day - monthday;if (this->_month > 12){this->_month = 1;this->_year++;}}//返回的是对象++之前的对象的拷贝return tmp;}// 前置--Date& Date::operator--(){this->_day--;if (this->_day > 0){return *this;}else{this->_month--;if (this->_month == 0){this->_month = 12;this->_year--;}int monthday = Date::GetMonthDay(this->_year, this->_month);this->_day = this->_day + monthday;return *this;}}// 后置--Date Date::operator--(int){Date tmp = *this;this->_day--;if (this->_day > 0){return tmp;}else{this->_month--;if (this->_month == 0){this->_month = 12;this->_year--;}int monthday = Date::GetMonthDay(this->_year, this->_month);this->_day = this->_day + monthday;return tmp;}}// 友元函数//// 流插入 

上一篇:揭秘源文件,定义、功能与重要性的深度解析

下一篇:天猫精灵下载教程,轻松获取智能助手,一键开启智能生活!