C++的类与对象(三)

目录

类的6个默认成员函数

构造函数

语法

特性

析构函数

特性

对象的销毁顺序​​​​​​​


类的6个默认成员函数

问题:一个什么成员都没的类叫做空类,空类中真的什么都没有吗?

基本概念:任何类在什么都不写时,编译器会自动生成以下六个默认成员函数(无参的)**

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

注意事项:如果我们自己实现了这些函数,那么编译器就不会生成默认的成员函数

构造函数

产生原因:初始化容易被遗忘,且有时候会太麻烦

作用:完成初始化工作(Init)**

cpp 复制代码
class Date
{
public:
     void Init(int year, int month, int day)
     {
         _year = year;
         _month = month;
         _day = day;
     }

     void Print()
     {
         cout << _year << "-" << _month << "-" << _day << endl;
     }

private:
     int _year;
     int _month;
     int _day;
};

int main()
{
 Date d1;
 d1.Init(2022, 7, 5);
 d1.Print();
 Date d2;
 d2.Init(2022, 7, 6);
 d2.Print();
 return 0;
}

对于Data类,之前我们会通过Init公有方法给对象设置日期,但每次创建对象时都需要调用该方法设置信息就显得有点麻烦,C++的构造函数就可以做到在对象创建时,就将信息设置进去

语法

1、构造函数是特殊的成员函数

2、构造函数的主要任务是在类实例化对象时初始化对象,而不是去开空间创建对象

3、构造函数的函数名与所在类的类名相同***(类名是A,构造函数的名字就叫A,客随主便)***

4、构造函数无返回值***(不是void,而是连void都没)***

5、对象实例化时编译器会自动调用合适的构造函数***(对象后没括号就调无参构造函数,有实参就调用带参构造函数)***

cpp 复制代码
#include <iostream>
using namespace std;

 class Date
 {
  public:
      // 1.无参构造函数
      Date()
     {
        _year = 1;
        _month = 1;
        _day = 1;
     }

      void Print()
     {
        cout << _year << "-" << _month << "-" << _day << endl;
     }

  private:
      int _year;
      int _month;
      int _day;
 };

int main()
{
    Date d1; // 调用无参构造函数
    d1.Print();
    return 0;
}

6、构造函数可以重载

cpp 复制代码
#include <iostream>
using namespace std;

class Date
{
public:
    // 1.无参构造函数
    Date()
    {
        _year = 1;
        _month = 1;
        _day = 1;
    }
    void Print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }
    // 2.带参构造函数
    Date(int year, int month, int day)
    {
        _year = year;
        _month = month;
        _day = day;
    }
private:
    int _year;
    int _month;
    int _day;
};

int main()
{
    Date d1; // 调用无参构造函数
    d1.Print();

    Date d2(2015, 1, 1); // 调用带参的构造函数
    d2.Print();
    return 0;
}

7、构造参数没有形参时,对象实例化时不能在对象后加括号

8、 对象实例化时必须调用构造函数,没有或者没有合适的构造函数就会报错***(d1原本想要调用无参的构造函数,但是只有带参的构造函数,有了带参的构造参数编译器就算像提供不带参的默认构造参数也没办法了)***

cpp 复制代码
#include <iostream>
using namespace std;

class Date
{
public:
    void Print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }

    //带参构造函数
    Date(int year, int month, int day)
    {
        _year = year;
        _month = month;
        _day = day;
    }

private:
    int _year;
    int _month;
    int _day;
};

int main()
{
    Date d1;
    d1.Print();
    return 0;
}

9、无参的构造函数和全缺省的构造函数在语法上因为函数重载可以同时存在,但是在实践中不可以,会造成调用歧义

cpp 复制代码
#include <iostream>
using namespace std;

class Date
{
public:

    Date()
    {
        _year = 1;
        _month = 1;
        _day = 1;
    }
    void Print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }

    Date(int year = 1, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }
private:
    int _year;
    int _month;
    int _day;
};

int main()
{
    Date d1;
    d1.Print();
    return 0;
}

特性

1(重点)、实例化对象时我们不写构造函数***(包括默认构造函数和普通的构造函数),编译器就会生成默认构造函数,从而初始化对象,我们写了构造参数编译器就不会生成默认构造参数(普通的构造参数是指带参的构造参数)***

2、编译器生成的默认构造参数什么也不干,为什么?(我们程序员没有定义一个构造参数是我们的问题,但是你编译器既然为我们自动生成了一个默认构造参数,你总得让它起点作用吧,就是只让成员变量初始化为0也行啊)

cpp 复制代码
#include <iostream>
using namespace std;

class Date
{
public:
    void Print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }

private:
    int _year;
    int _month;
    int _day;
};

int main()
{
    Date d1;
    d1.Print();
    return 0;
}

这是因为,C++将数据分为了内置(基本)类型和自定义类型:

  • 内置类型:语言自身定义的类型(int、char、double、任何类型的指针等)**
  • 自定义类型:程序员根据自己需求定义的类型(struct、class等)**

3、C++98规定编译器生成的默认构造函数***(程序员没提过时),对内置类型不做处理,对自定义类型会去调用它的!!默认构造函数!!(一定是默认构造函数而不是普通的构造函数)***

cpp 复制代码
#include <iostream>
using namespace std;

//编译器对自定义类型会调用它的默认构造
class A
{
public:
    A()
    {
        cout << "A()" << endl;
        _a = 0;
    }
private:
    int _a;

};

//编译器对内置类型不做处理
class Date
{
public:

//这里没有自定义构造函数,编译器默认生成构成函数

    void Print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }

private:
    int _year;
    int _month;
    int _day;

    A _aa;
};

int main()
{
    Date d1;
    d1.Print();
    return 0;
}

大致步骤如下:
①Date d1:实例化对象d1

②Data{}:编译器自动生成默认构造函数,并开始对成员变量开始初始化

③__aa:_aa的类型是A类,属于自定义类型,调用A类的提供的!默认!构造函数

如果A类也没提供默认构造函数,那么_aa也是随机值(虽然编译器也可以为A提供但是没用):

cpp 复制代码
#include <iostream>
using namespace std;

class A
{
public:
    int getA() // 添加一个公共方法以获取 _a 的值
    {
        return _a;
    }

private:
    int _a;

};

class Date
{
public:

    void Print()
    {
        cout << "Year: " << _year << ", Month: " << _month << ", Day: " << _day << endl;
        cout << "Value of '_aa' member variable in class 'Date': " << _aa.getA() << endl; // 打印_aa对象内部_a变量的值
    }

private:
    int _year;
    int _month;
    int	_day;

    A _aa;
};


int main()
{
    Date d1;
    d1.Print();
    return 0;
}

④_year、_month、_day:是int类型,属于内置类型,不做任何操作,结果为随机值

为什么在调试时,先初始化对象_aa?

答:C++中,类的成员变量初始化顺序是由它们在类中声明的顺序决定的,而不是由它们在构造函数初始化列表中出现的顺序决定

注意事项:有些新的编译器会对内置类型也做处理,但是C++的标准没有规定

4、C++11规定内置类型的成员变量声明时可给默认值***(为C++98打补丁,没给的依然是随机值)***

cpp 复制代码
#include <iostream>
using namespace std;

class A
{
public:
    A()
    {
        cout << "A()" << endl;
        _a = 0;
    }

private:
    int _a;

};

class Date
{
public:
    void Print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }

private:
    //声明时给缺省值
    int _year = 2024;
    int _month =3 ;
    int _day;

    A _aa;
};

int main()
{
    Date d1;
    d1.Print();
    return 0;
}

5、 只有无参 / 全缺省构造函数、编译器默认生成的构造函数都叫默认构造函数,其余的都是构造函数而不是默认构造函数,默认构造函数只能有一个

cpp 复制代码
//默认构造参数只能有一个:
#include <iostream>
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;
    }
private:
    int _year;
    int _month;
    int _day;
};

int main()
{
    Date d1;
    return 0;
}
cpp 复制代码
//成员变量在声明时未给缺省值
#include <iostream>
using namespace std;
class Date
{
public:

    Date(int year = 1, int month , int day)
    {
        _year = year;
        _month = month;
        _day = day;
    }

    void Print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }

     ~Date()
    {
         cout << this << endl;
         cout << " ~Date()" << endl;
    }

private:
    int _year;
    int _month;
    int _day;

};

int main()
{
    Date d1;
    d1.Print();

    return 0;
}
cpp 复制代码
//成员变量在声明时提供缺省值:
#include <iostream>
using namespace std;
class Date
{
public:

    Date(int year = 1, int month , int day)
    {
        _year = year;
        _month = month;
        _day = day;
    }

    void Print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }

     ~Date()
    {
         cout << this << endl;
         cout << " ~Date()" << endl;
    }

private:
    int _year = 1;
    int _month = 1;
    int _day =1;
};

int main()
{
    Date d1;
    d1.Print();
    return 0;
}

为什么成员变量在声明时已经给了缺省值,为什么Data还要再给?

虽然为 Date 类中的私有成员变量 _year, _month, 和 _day 提供了默认值(1, 1, 1),但这些缺省值仅在默认构造函数未初始化成员变量时才会被用于初始化成员变量。实例化d1时,类中定义了一个貌似是全缺省的构造函数,因此编译器不会自动生成默认构造函数***(那个位置上已经有人了,只是那个人有点缺陷)***,且因为该貌似的全缺省构造函数缺少两个缺省值所以报错

cpp 复制代码
#include <iostream>
using namespace std;

class Time
{
public:
    //带参构造函数,不是默认构造函数
    Time(int hour)
    {
        cout << "Time()" << endl;
        _hour = 0;
        _minute = 0;
        _second = 0;
    }
private:
    int _hour;
    int _minute;
    int _second;
};

class Date
{
private:
    //声明给缺省值
    int _year;
    int _month;
    int _day;

    Time _t;
};

int main() 
{
    Date d;
    return 0;
}

为什么会提示_t没有默认构造函数?

答:实例化对象d时,Date类没给构造函数,编译器自动生成默认构造函数(无参),_year、_month、_day是内置类型不做处理,_t是自定义类型会调用Time类的默认构造函数,但是Time类中已经有了一个带参的构造函数(普通构造函数)所以Time类中不会有默认构造函数了(只要我们写了构造函数,即使它是带参的普通构造函数,编译器就不会给我们提供默认构造参数了)故报错
结论:绝大多数场景下都需要自己实现构造函数,且更推荐用全缺省构造函数​​​​​​​

析构函数

产生原因:需要人为销毁的空间容易忘记销毁,内存泄漏可能不会报错

cpp 复制代码
#include <iostream>
using namespace std;
class Date
{
public:
    void Print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }

    ~Date()
    {
        cout << this << endl;
        cout << " ~Date()" << endl;
    }

private:
    //声明给缺省值
    int _year = 1;
    int _month = 1;
    int _day = 1;

};

void func()
{
    Date d2;
}

class Stack
{
public:
    Stack(size_t capacity = 4)
    {
        _array = (int*)malloc(sizeof(int*) * capacity);
        if (NULL == _array)
        {
            perror("malloc申请空间失败!!!");
            return;
        }
        _capacity = capacity;
        _size = 0;
    }
    void Push(int data)
    {
        // CheckCapacity();
        _array[_size] = data;
        _size++;
    }

    //~Stack()
    //{
    //    cout << "~Stack()" << endl;
    //    if (_array)
    //    {
    //        free(_array);
    //        _array = nullptr;
    //    }
    //    _size = _capacity = 0;

    //}

private:
    int* _array;
    int _capacity;
    int _size;
};

int main()
{
    func();
    Date d1;
    Stack st1; //内存泄漏
    return 0;
}

如果Stack类中没有自定义的析构函数就会出现内存泄漏**,因为我们即没有定义Destory函数去销毁在堆上开辟的空间,析构函数也不起作用**

作用:完成清理工作(Destory)**

**基本概念:**析构函数不是完成对对象本身的销毁,局部对象销毁工作是由编译器完成的,而对象在销毁时会自动调用析构函数,完成对象中资源的清理工作

特性

1、析构函数名是在类名前加上字符~

2、析构函数无参数无返回值类型

3、一个类只能有一个析构函数,若未显示定义,编译器会自动生成默认的析构函数

4、析构函数不能重载***(清理一遍数据就应该被清理完成)***

5、对象生命周期结束***(函数结束)***时,程序会自动调用析构函数

cpp 复制代码
#include <iostream>
using namespace std;
class Date
{
public:

     ~Date()
    {
         cout << this << endl;
         cout << " ~Date()" << endl;
    }

private:
    //声明给缺省值
    int _year = 1;
    int _month = 1;
    int _day =1;
};

void func()
{
    Date d2;//实例化对象d2
}//函数结束,调用析构函数销毁数据

int main()
{
    func();  
    Date d1;//实例化对象d1
    return 0;
}//函数结束,调用析构函数销毁数据

6、编译器生成的析构函数,对内置成员变量不做处理,对自定成员变量会调用它的析构函数

cpp 复制代码
#include <iostream>
using namespace std;

class Time
{
public:
	~Time()
	{
		cout << "~Time()" << endl;
	}
private:
	int _hour;
	int _minute;
	int _second;
};

class Date
{
private:
	// 基本类型(内置类型)
	int _year = 1970;
	int _month = 1;
	int _day = 1;
	// 自定义类型
	Time _t;
};
int main()
{
	Date d;
	return 0;
}

在main函数中没有直接创建Time类的对象,为什么最后会调用Time类的析构函数?

答:main函数中创建了Date类的对象d,d中包含了四个成员变量,其中_year、_month、_day是内置类型的成员变量,销毁时不需要人为清理资源main函数结束后系统直接回收,而_t是Time类的对象,为了销毁_t就需要Data类的析构函数,但是我们并没有给Date类定义析构函数,所以编译器就会为Date类提供一个默认析构函数,该函数对于自定义类型的成员变量就会调用它(_t)的析构函数(Time提供的析构函数)*,*

7、后定义的先析构***(定义->开辟帧栈->压栈,栈后进先出)***

cpp 复制代码
#include <iostream>
using namespace std;
class Date
{
public:
    ~Date()
    {
        cout << this << endl;
        cout << " ~Date()" << endl;
    }

private:
    //声明给缺省值
    int _year = 1;
    int _month = 1;
    int _day = 1;

};

class Stack
{
public:
    Stack(int capacity = 4)
    {
        _array = (int*)malloc(sizeof(int*) * capacity);
        if (NULL == _array)
        {
            perror("malloc申请空间失败!!!");
            return;
        }
        _capacity = capacity;
        _size = 0;
    }
    void Push(int data)
    {
        // CheckCapacity();
        _array[_size] = data;
        _size++;
    }

    ~Stack()
    {
        cout << this << endl;
        cout << "~Stack()" << endl;
        if (_array)
        {
            free(_array);
            _array = nullptr;
        }
        _size = _capacity = 0;

    }
private:
    int* _array;
    int _capacity;
    int _size;
};

int main()
{
    Date d1;
    Stack st1;
    return 0;
}

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

9、自定义类型的本质还是内置类型

对象的销毁顺序

从左至右:局部变量(后定义的先析构)------>局部的静态------>全局(后定义的先析构)

当成栈来理解,先进后出

cpp 复制代码
#include <iostream>
using namespace std;
class Date
{
public:
    //全缺省构造函数
    Date(int year = 1)
    {
        _year = year;
    }

    ~Date()
    {
        cout << " ~Date()->" << _year << endl;
    }

private:
    //声明给缺省值
    int _year;
    int _month;
    int _day;

};

Date d5(5);//全局
Date d6(6);//全局
static Date d7(7);//全局
static Date d8(8);//全局

void func()
{
    Date d3(3);//局部
    static Date d4(4);//局部的静态
}

int main() 
{
    Date d1(1);//局部
    Date d2(2);//局部
    func();
    return 0;
}
cpp 复制代码
#include <iostream>
using namespace std;
class Date
{
public:
    //全缺省构造函数
    Date(int year = 1)
    {
        _year = year;
    }

    ~Date()
    {
        cout << " ~Date()->" << _year << endl;
    }

private:
    int _year;
    int _month;
    int _day;

};

int main()
{
    Date d1(1);//局部
    Date d2(2);//局部
    static Date d3(3);//局部的静态
    return 0;
}

~over~

相关推荐
金池尽干16 分钟前
基于C语言的基数排序算法
c语言·算法·排序算法
红色石榴20 分钟前
Qt中文乱码解决
开发语言·qt
Htht11121 分钟前
【Qt】实现模拟触摸屏 上下滑动表格 的两种方式
开发语言·qt
A 八方22 分钟前
Python MongoDB
开发语言·python·mongodb
sz66cm2 小时前
Python基础 -- 使用Python实现ssh终端并实现数据处理与统计功能
开发语言·python·ssh
小灰灰爱代码2 小时前
C++——求3*3矩阵对角元素之和。
数据结构·c++·算法
老K(郭云开)2 小时前
allWebPlugin中间件自定义alert、confirm及prompt使用
c++·chrome·中间件·prompt·html5·edge浏览器
liangbm33 小时前
MATLAB系列02:MATLAB基础
开发语言·数据结构·笔记·matlab·教程·工程基础·高级绘图
change95134 小时前
PHP纯离线搭建(php 8.1.7)
开发语言·php
福鸦5 小时前
详解c++:new和delete
开发语言·c++