• 类与对象(下篇)


    再谈构造函数

    回顾构造函数

    在上一篇博客中提到了构造函数,构造函数其主要目的是给所创建的类对象一个合适的初始值:

    class Date {
    public:
    	Date()   //无参构造
    	{}
    	Date(int year, int month, int day)  //带参构造
    	{
    		_year = year;
    		_month = month;
    		_day = day;
    	}
    private:
    	int _year;
    	int _month;
    	int _day;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    虽然上述构造函数调用之后,对象中有了一个初始值,但不能将其称为类对象成员的初始化操作,而只能称之为赋值操作。

    初始化只能初始化一次,赋值可以赋值多次

    初始化列表

    初始化列表: 以一个冒号开始,并以逗号进行分隔每个数据成员,每个成员变量后跟一个放在括号中的初始化值/表达式

    class Date {
    public:
    	Date()   //无参构造
    	{}
    	Date(int year, int month, int day)
    		:_year(year)        //初始化列表
    		,_month(month)
    		,_day(day)
    	{}
    private:
    	int _year;
    	int _month;
    	int _day;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    初始化只会进行一次

    在这里插入图片描述

    注意

    (1)每个成员变量在初始化列表中只能出现一次(只能初始化一次)

    在这里插入图片描述

    (2)若类中包含以下成员(引用成员变量、const 成员变量、自定义类型成员变量),则必须放在初始化列表进行初始化

    1)引用成员变量

    在这里插入图片描述

    class A {
    public:
    	A(int a,int b)
    		:_a(a)
    		,_b(b)              //引用类型必须在初始化列表进行初始化操作
    	{}
    private:
    	int _a;
    	int& _b;
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2)const 成员变量

    在这里插入图片描述

    class A {
    public:
    	A(int a)
    		:_a(a)
    		,_b(a)                // const 变量必须在初始化列表进行初始化操作
    		//,_b(10)
    	{}
    private:
    	int _a;
    	const int _b;
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    3)自定义类型成员变量
    在这里插入图片描述

    class B {
    public:
    	B(int b )                 //此时 B 类中存在初始化列表,但是带参构造并没有带缺省值
    		:_b(b)
    	{}
    private:
    	int _b;
    };
    
    class A {
    public:
    	A(int a)
    		:_a(a)
    		,bb(20)   //自定义类型必须在初始化列表中进行初始化操作
    		//因为此时在 B 类中并没有缺省构造存在
    	{}
    private:
    	int _a;
    	B bb;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    假如 B 类中的构造函数是缺省构造:

    在这里插入图片描述

    结论:
    若 A 类成员变量中存在 B 类(自定义类型)对象时:
    (1)若 B 类中不存在全缺省构造函数,则在 A 类的构造函数初始化列表中必须对 B 类对象进行初始化操作;
    (2)若 B 类中存在全缺省构造函数,则在 A 类的构造函数初始化列表中对 B 类对象进行初始化操作可有可无(编译器在 A 类构造函数初始化列表会自动调用 B 类中全缺省构造进行 B 类对象的初始化操作)

    (3)尽量使用初始化列表进行初始化操作,因为对于自定义类型成员变量,一定会先使用初始化列表进行初始化操作

    初始化列表可以不写,但不代表编译器就不执行初始化
    若不写初始化列表,对内置类型的成员变量编译器采用随机值进行初始化,对自定义类型------调用无参或全缺省构造初始化,若没有无参或全缺省构造会报错

    在这里插入图片描述

    (4)成员变量在类中声明次序就是其在初始化列表中初始化的顺序

    
    class Date {
    public:
    	Date(int year, int month, int day)
    		:_year(year)
    		,_month(month)
    		,_day(day)
    	{}
    private:
    	int _year;
    	int _month;
    	int _day;
    };
    int main()
    {
    	Date d1(2022, 11, 28);
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    假如交换一下初始化列表的初始化顺序:(注:不同编译器的处理结果可能存在差异)

    class Date {
    public:
    	Date(int year,int month)
    		:_year(year)
    		,_day(month)     
    		,_month(_year)      //此时 _month 会为随机值,因为与变量的声明顺序不一致
    	{}
    	void Print()
    	{
    		cout << _year << "/" << _month << "/" << _day << endl;
    	}
    private:
    	int _year;
    	int _month;
    	int _day;
    };
    int main()
    {
    	Date d1(2022,11);
    	d1.Print();
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    explicit 关键字

    构造函数不仅可以构造和初始化对象,对于单个参数的构造函数,还具有类型转换的作用。

    在这里插入图片描述

    实际上,进行赋值之前,先创建了一个无名对象并对其进行初始化,然后将无名对象赋值给 d1 变量:

    在这里插入图片描述

    explicit 关键字修饰构造函数,将会禁止单参构造函数的隐式转换

    在这里插入图片描述

    拷贝构造函数也具有初始化列表

    class Date {
    public:
    	Date(int year, int month, int day)
    		:_year(year)
    		, _month(month)
    		, _day(day)		//成员变量的声明顺序======初始化顺序
    	{
    		cout << "Date(int,int,int)" << this << endl;
    	}
    
    	Date(const Date& d)          //带初始化列表的拷贝构造
    		:_year(d._year)
    		, _month(d._month)
    		, _day(d._day)
    	{}
    
    	~Date()
    	{
    		cout << "~Date()" << this << endl;
    	}
    
    	void Print()
    	{
    		cout << _year << "/" << _month << "/" << _day << endl;
    	}
    private:
    	int _year;
    	int _month;
    	int _day;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    友元 friend

    友元函数

    在之前,我们说过类中的 private 私有成员变量在类外是不能进行访问的,要解决这一问题引入了友元的概念:

    class Date {
    public:
    	Date(int year, int month, int day)
    		:_year(year)
    		, _month(month)
    		, _day(day)
    	{}
    	Date(const Date& d)
    		:_year(d._year)
    		, _month(d._month)
    		, _day(d._day)
    	{}
    	~Date()
    	{
    		cout << "~Date()" << endl;
    	}
    
    	friend void Print(const Date& d);     //声明友元函数
    	
    private:
    	int _year;
    	int _month;
    	int _day;
    };
    
    void Print(const Date& d)
    {
    	//cout << d._year << "/" << d._month << "/" << d._day << endl;     
    	 //私有成员变量不能在类外访问------------1、在类内定义 set get函数进行访问
    
    	//2、定义 friend 友元函数
    	cout << d._year << "/" << d._month << "/" << d._day << endl; //可以正常访问
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    **友元函数可以访问类内私有|保护的成员变量,但不是类中的成员函数(没有*this)
    友元函数不能被 const 修饰
    友元函数可以在类定义中任何位置进行声明
    一个函数可以是多个类的友元函数
    友元函数调用
    普通函数调用**

    输入输出流的重载

    (1)正常逻辑重载函数

    class Date {
    public:
    	Date(int year, int month, int day)
    		:_year(year), _month(month), _day(day)
    	{}
    
    	void operator<<(ostream& out)       //输出重载,会存在一个默认的第一个参数 *this,故 out 称为第二个参数
    	{
     	out << _year << "/" << _month << "/" << _day << endl;
    	}
    
    private:
    	int _year;
    	int _month;
    	int _day;
    };
    int main()
    {
    	Date d(2022, 11, 25);
       	//cout<
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    (2)改进重载

    由于在类内定义运算符重载函数称为类内成员函数,会默认存在一个*this指针作为成员函数的第一个参数,因此为了避免 *this 称为第一个参数,我们可以将重载函数定义在类外
    但在类外不能正常访问类中私有的成员变量,故采用 友元来解决:

    class Date {
    public:
    	Date(int year, int month, int day)
    		:_year(year), _month(month), _day(day)
    	{}
    
    
    	friend void operator<<(ostream& out, const Date& d);  //类内声明友元,类外定义
    
    private:
    	int _year;
    	int _month;
    	int _day;
    };
    
    //类外重载----------必须声明为友元函数,否则无法访问成员变量
    //由于在类外定义,因此函数内部没有隐含的 *this ,需要我们自己添加两个参数列表,将输出设为第一个参数,则可以正常逻辑输出内容
    void operator<<(ostream& out, const Date& d)
    {
    	out << d._year << "/" << d._month << "/" << d._day << endl;
    }
    
    int main()
    {
    	Date d(2022, 11, 25);
    	
    	cout << d;
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    在这里插入图片描述

    但是如果进行连续的输出,上述代码是否可以正常进行?

    在这里插入图片描述

    发生了报错,这是因为在输出 d1 之后并没有函数的返回值存在(void)

    (3)再改进

    
    class Date {
    public:
    	Date(int year, int month, int day)
    		:_year(year), _month(month), _day(day)
    	{}
    	//类内声明友元
    	friend ostream& operator<<(ostream& out, const Date& d);
    	//输入流重载
    	friend istream& operator>>(istream& in, Date& d);
    
    private:
    	int _year;
    	int _month;
    	int _day;
    };
    
    //类外进行定义
    //cout
    ostream& operator<<(ostream& out, const Date& d)
    {
    	out << d._year << "/" << d._month << "/" << d._day;
    	return out;
    }
    
    //cin
    istream& operator>>(istream& in, Date& d)
    {
    	in >> d._year >> d._month >> d._day;
    	return in;
    }
    
    int main()
    {
    	Date d(2022, 11, 25);
    
    	Date d1(2022, 12, 1);
    
    	cout << d << endl;     //可以正常逻辑输出
    	//cout << d << d1; //不能连续输出--------因为没有返回值类型
    
    
    	cout << d << "  " << d1 << endl;     //此时添加相应的返回值类型可以支持连续输出
    
    
    	//输入输出流重载之后可以正常使用
    	cin >> d;
    	cout << d << endl;
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    友元类

    class Time {
    public:
    	Time(int hour, int minute, int second)
    		:_hour(hour), _minute(minute), _second(second)
    	{}
    
    	friend class Date;   //声明友元类
    
    private:
    	int _hour;
    	int _minute;
    	int _second;
    };
    
    
    class Date {
    public:
    	Date(int year, int month, int day)
    		:_year(year), _month(month), _day(day), _t(10, 11, 24)
    	{}
    
    	void Print()const
    	{
    		cout << _year << "/" << _month << "/" << _day << "------";
    		//cout << _t._hour << ":" << _t._minute << ":" << _t._second << endl; //此时不能访问 Time 类中私有成员---需要声明 friend
    
    //在 Time 类中声明友元关系即可访问:
    		cout << _t._hour << ":" << _t._minute << ":" << _t._second << endl; 	}
    private:
    	int _year;
    	int _month;
    	int _day;
    	Time _t;
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    友元关系是单向的,即此时 Time 中不能访问 Date 类成员
    友元关系不能传递
    友元关系不能继承

    static 成员

    static 声明为类的静态成员:
    static 成员变量------静态成员变量
    static 成员函数------静态成员函数

    静态成员一定要在类外进行初始化

    class Date {
    public:
    	Date(int year, int month, int day)  
    		:_year(year)
    		, _month(month)
    		, _day(day)
    	{
    		cout << "Date(int,int,int)" << this << endl;
    	}
    	
    	Date(const Date& d)          //带初始化列表的拷贝构造
    		:_year(d._year)
    		, _month(d._month)
    		, _day(d._day)
    	{}
    	
    	~Date()
    	{
    		cout << "~Date()" << this << endl;
    	}
    
    	void Print()
    	{
    		cout << _year << "/" << _month << "/" << _day << endl;
    	}
    private:
    	int _year;
    	int _month;
    	int _day;
    
    public:
    	static int _count;    //静态成员变量,在类中只是声明,需要在类外进行初始化
    };
    
    //类外初始化 static 成员变量
    
    //静态成员并没有包含在具体对象当中,因此并不能通过构造函数初始化列表进行初始化赋值(所有对象共享的变量)
    //静态成员变量在类外定义
    
    int Date::_count = 0;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    注意

    静态成员为所有类对象所共享,并不属于某个具体的示例。

    静态成员变量必须在类外进行初始化定义,定义时需要限制作用域,不需要加 static 关键字。

    在这里插入图片描述

    类静态成员访问: 类名::静态成员 ; 对象.静态成员

    在这里插入图片描述

    静态成员函数没有 * this 指针,不能访问类内任何非静态成员

    在这里插入图片描述

    静态成员和类的普通成员一样,也有 public\protected\private 访问级别,也可以具有返回值

    static 不能修饰成员函数:构造、拷贝构造、析构、赋值运算符重载(因为 static 修饰的成员函数没有隐含的 *this)

    const 不能修饰构造、析构、拷贝构造(const 修饰函数相当于修饰 *this,即指针内容不能进行修改)

    内部类

    如果一个类定义在另一个类内部,则称之为内部类

    class A {  //外部类
    
    	class B {   //内部类
    	private:
    		int _b;
    	};
    
    public:
    	A(int a) :_a(a)
    	{}
    
    private:
    	int _a;
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    内部类是一个独立的类,不属于外部类,更不能通过外部类对象去调用内部类

    内部类是外部类的友元类–即内部类可以访问外部类中受保护/私有成员,但是外部类不是内部类的友元,不能访问内部类中受保护/私有成员

    class A {  //外部类
    
    	class B {   //内部类
    	public:
    		B()
    		{}
    		void Print(const A& a)
    		{
    			cout << a._a << endl;      //内部类可以访问外部类中私有成员
    		}
    	private:
    		int _b;
    	};
    
    public:
    	A(int a) :_a(a)
    	{}
    
    private:
    	int _a;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述

    外部类的大小等于外部类中成员变量的大小之和(内存对齐),并不会涉及内部类中成员变量

    在这里插入图片描述

    ps:
    欢迎评论留言哦~~

  • 相关阅读:
    【数据结构--排序】冒泡排序,选择排序,插入排序
    LIO-SAM总结笔记
    RabbitMQ
    LNMP架构
    如何在Linux系统安装Nginx
    【Unity】Unity坑的集锦之RenderTexture打包黑屏
    简单神经网络算法原理,最简单的神经网络算法
    初识vxlan
    首篇大模型压缩论文综述
    SpringBoot图片验证码
  • 原文地址:https://blog.csdn.net/weixin_46655027/article/details/128069195