【c++】类和对象(下)

构造函数(初始化列表)

之前我们实现构造函数时,初始化成员变量都是在函数体内赋值,构造函数中初始化变量还有一种方法———初始化列表。初始化列表以冒号开始将要初始化的变量用逗号隔开。

#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>

using namespace std;

class Date
{
public:
	Date(int& x, int year = 1, int month = 1, int day = 1)
		:_year(2024)
		,_month(5)
		,_day(10)
	{

	}

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

private:
	int _year;
	int _month;
	int _day;

};

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

在使用初始化列表时,要注意:

每个成员在初始化列表只能出现一次,在语法理解上初始化列表是每一个成员变量定义初始化的地方。
引用成员变量,const成员变量,没有默认构造的类类型变量,必须放在初始化列表进行初始化,不然编译会报错。

const类型成员必须在定义的时候就初始化,定义了之后不能改变

注意这是错误演示 ,const 类型成员要在初始化列表中初始化。

//注意这是错误演示

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
class Date
{
public:
	Date(int& x, int year = 1, int month = 1, int day = 1)
		:_year(2024)
		,_month(5)
		,_day(10)
	{
		_n = 1;//const类型成员必须在定义的时候就初始化,定义了之后不能改变
	}

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

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

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

 在函数体中对const类型对象赋值会报错,所以const类型成员只能由初始化列表进行初始化。

定义了就要初始化的还有谁呢?那就是引用类型对象。

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
class Date
{
public:
	Date(int& x, int year = 1, int month = 1, int day = 1)
		:_year(2024)
		, _month(5)
		, _day(10)
		, _n(5)
		,xx(x)  //c++中要求对引用类型成员定义的时候就初始化,是强制的,所以引用类型必须在初始化列表中初始化
	{
		//_n = 1;//const类型成员必须在定义的时候就初始化,定义了之后不能改变
	}

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

private:
	int _year;
	int _month;
	int _day;
	const int _n;

	int& xx;
};

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

没有默认构造的自定义类型也必须在初始化列表中进行初始化。

 如果自定义类型无默认构造也没有在初始化列表中进行初始化,编译就不能通过。

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
class Time
{
public:
	Time(int hour,int  minute,int second)
	{
		cout << "time()" << endl;
	}
private:
	int _hour;
	int _minute;
	int _second;
};

class Date
{
public:
	Date(int& x, int year = 1, int month = 1, int day = 1)
		:_year(2024)
		, _month(5)
		, _day(10)
		, _n(5)
		, xx(x)  //c++中要求对引用类型成员定义的时候就初始化,是强制的,所以引用类型必须在初始化列表中初始化
	{
		//_n = 1;//const类型成员必须在定义的时候就初始化,定义了之后不能改变
	}

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

private:
	int _year;
	int _month;
	int _day;
	const int _n;

	int& xx;
	Time _t;
};

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

c++11支持在声明成员变量时给缺省值,这个缺失值是给没有在初始化列表中显示初始化的成员使用的 。

尽量使用初始化列表对成员变量进行初始化,那些你不在初始化列表初始化的成员也会走初始化列表,如果这个成员在声明位置给了缺省值,初始化列表会用这个缺省值初始化。如果你没有给缺省值,对于没有显示在初始化列表初始化的内置类型成员是否初始化取决于编译器,C++并没有规定。对于没有显示在初始化列表初始化的自定义类型成员会调用这个成员类型的默认构造函数,如果没有默认构造会编译错误。

总结:每个成员都要走初始化列表

1.在初始化列表初始化的成员

2.没有在初始化列表的成员

   a.声明的地方有缺省值用缺省值

   b.没有缺省值

            x.内置类型。不确定,看编译器,大概率是随机值

            y.自定义类型,调用默认构造,没有默认构造就编译报错

3.引用,const,没有默认构造的自定义类型 必须初始化列表初始化

初始化列表中按照成员变量在类中声明顺序进行初始化,跟成员在初始化列表出现的的先后顺序无关。建议声明顺序和初始化列表顺序保持⼀致。

观察下列程序并列出执行结果:

_a2比_a1先声明,所以_a2先初始化,A的构造函数中_a2用_a1的值初始化,这时候_a1没初始化,是随机值,所以用_a1的值初始化的_a2是随机值,之后用a的值初始化_a1,_a1为1。

类型转化

C++支持内置类型隐式类型转换为类类型对象,需要有相关内置类型为参数的构造函数。
构造函数前面加explicit就不再支持隐式类型转换。
#include<iostream>
using namespace std;
class A
{
public:
	// 构造函数explicit就不再支持隐式类型转换
	// explicit A(int a1)
	
		A(int a1)
		:_a1(a1)
	{}
	//explicit A(int a1, int a2)
	A(int a1, int a2)
		:_a1(a1)
		, _a2(a2)
	{}
	void Print()
	{
		cout << _a1 << " " << _a2 << endl;
	}
private:
	int _a1 = 1;
	int _a2 = 2;
};
int main()
{
	// 1构造⼀个A的临时对象,再用这个临时对象拷⻉构造aa3
	// 编译器遇到连续构造+拷⻉构造->优化为直接构造
	A aa1 = 1;
	aa1.Print();
	const A& aa2 = 1;
	// C++11之后才支持多参数转化
	A aa3 = { 2,2 };
	return 0;
}

static成员

用static修饰的成员变量,称之为静态成员变量,静态成员变量⼀定要在类外进行初始化。
静态成员变量为所有类对象所共享,不属于某个具体的对象,不存在对象中,存放在静态区。
用static修饰的成员函数,称之为静态成员函数,静态成员函数没有this指针。
静态成员函数中可以访问其他的静态成员,但是不能访问非静态的,因为没有this指针。
非静态的成员函数,可以访问任意的静态成员变量和静态成员函数。
突破类域就可以访问静态成员,可以通过类名::(作用域操作符)静态成员 或者对象.静态成员 来访问静态成员变量和静态成员函数。
静态成员也是类的成员,受public、protected、private 访问限定符的限制。
静态成员变量不能在声明位置给缺省值初始化,因为缺省值是个构造函数初始化列表的,静态成员变量不属于某个对象,不走构造函数初始化列表。
#define _CRT_SECURE_NO_WARNINGS 1

// 实现⼀个类,计算程序中创建出了多少个类对象?
#include<iostream>
using namespace std;
class A
{
public:
	A()
	{
		++_scount;
	}
	A(const A& t)
	{
		++_scount;
	}
	~A()
	{
		--_scount;
	}
	static int GetACount()
	{
		return _scount;
	}
private:
	// 类里面声明
	static int _scount;
};
// 类外面初始化
int A::_scount = 0;


int main()
{
	cout << A::GetACount() << endl;
	A a1, a2;
	A a3(a1);
	cout << A::GetACount() << endl;
	cout << a1.GetACount() << endl;

	// 编译报错:error C2248: “A::_scount”: ⽆法访问 private 成员(在“A”类中声明)
//cout << A::_scount << endl;
	return 0;
}

通过类名::和对象 . 都可以找到GeyACount()函数

下面看一个题:

 局部的全局变量在第一次运行到这个位置才会初始化,main之外的全局变量在main之前初始化。所以第一个选E;

成员变量后定义的先析构,所以先调用B的析构函数才调用A的析构函数;局部成员变量析构完之后才会析构全局变量,d在局部域,先调用D的析构函数。所以第二个选B。

友元

友元提供了⼀种突破类访问限定符封装的方式,友元分为:友元函数和友元类,在函数声明或者类声明的前面加friend,并且把友元声明放到⼀个类的里面。
外部友元函数可访问类的私有和保护成员,友元函数仅仅是⼀种声明,他不是类的成员函数。
友元函数可以在类定义的任何地方声明,不受类访问限定符限制。
⼀个函数可以是多个类的友元函数。
友元类中的成员函数都可以是另⼀个类的友元函数,都可以访问另⼀个类中的私有和保护成员。
友元类的关系是单向的,不具有交换性,比如A类是B类的友元,但是B类不是A类的友元。
友元类关系不能传递,如果A是B的友元, B是C的友元,但是A不是C的友元。
有时提供了便利。但是友元会增加耦合度,破坏了封装,所以友元不宜多用。
一个函数可以是多个类的友元函数
#include <iostream>

using namespace std;

class id;
//先声明id这个类,这样做是为了允许在 ID 类中声明 id 作为友元函数的参数类型,尽管 id 的完整定义在 //ID 的定义之后才出现。前向声明对于处理类之间的相互依赖很有用。
class ID
{
public:
	friend void Func(const ID& a, const id& b);
	ID(int I = 1, int W = 1, int H = 1)
		:
		_idnumber(I)
		, _weight(W)
		, _height(H)
	{

	}
	void Print()
	{
		cout << _idnumber << _weight << _height << endl;
	}

private:

	int _idnumber;
	int _weight;
	int _height;
};

class id
{
public:
	friend void Func(const ID& a, const id& b);
	id(int I = 1, int W = 1, int H = 1)
		:
		_idnumber(I)
		, _weight(W)
		, _height(H)
	{

	}
	void Print()
	{
		cout << _idnumber << _weight << _height << endl;
	}

private:

	int _idnumber;
	int _weight;
	int _height;
};

void Func(const ID &a,const id &b)
{
	cout << a._height << endl;
	cout << b._height << endl;
}

int main()
{
	ID a1;
	id a2;
	Func(a1,a2);

	return 0;
}

 在定义ID类之前先声明id,这样做是为了允许在 ID 类中声明 id 作为友元函数的参数类型,尽管 id 的完整定义在 ID 的定义之后才出现。前向声明对于处理类之间的相互依赖很有用。

把一个类定义为另一个类的友元类之后可以单向访问另一个对象的私有成员。


#include <iostream>

using namespace std;

class id;
class ID
{
public:
	friend class id;
	
	friend void Func(const ID& a, const id& b);
	ID(int I = 1, int W = 1, int H = 1)
		:
		_idnumber(I)
		, _weight(W)
		, _height(H)
	{

	}
	void Print()
	{
		cout << _idnumber << _weight << _height << endl;
	}

private:

	int _idnumber;
	int _weight;
	int _height;
};

class id
{
public:
	friend void Func(const ID& a, const id& b);
	id(int I = 1, int W = 1, int H = 1)
		:
		_idnumber(I)
		, _weight(W)
		, _height(H)
	{

	}
	void Print()
	{
		cout << _idnumber << _weight << _height << endl;
	}
	void Func2(const ID& ss)
	{
		cout << ss._idnumber << endl;//id为ID的友元类可以直接访问私有成员
	}
private:

	int _idnumber;
	int _weight;
	int _height;
};

void Func(const ID& a, const id& b)
{
	cout << a._height << endl;
	cout << b._height << endl;
}

int main()
{
	ID a1;
	id a2;
	a2.Func2(a1);
	
	return 0;
}




内部类 

如果⼀个类定义在另⼀个类的内部,这个内部类就叫做内部类。内部类是⼀个独⽴的类,跟定义在全局相比,他只是受外部类类域限制和访问限定符限制,所以外部类定义的对象中不包含内部类。
内部类默认是外部类的友元类。
内部类本质也是⼀种封装,当A类跟B类紧密关联,A类实现出来主要就是给B类使用,那么可以考虑把A类设计为B的内部类,如果放到private/protected位置,那么A类就是B类的专属内部类,其
他地方都用不了。
将B定义在A的public区域:
#include <iostream>

using namespace std;

class ID
{
public:
    ID(int I = 1, int W = 1, int H = 1)
        :
        _idnumber(I)
        , _weight(W)
        , _height(H)
    {
    }
    void Print()
    {
        cout << _idnumber << _weight << _height << endl;
    }
    class id
    {
    public:
        id(int I = 1, int W = 1, int H = 1)
            :
            _idnumber(I)
            , _weight(W)
            , _height(H)
        {
        }
        void Print()
        {
            cout << _idnumber << _weight << _height << endl;
        }
        void Func(ID& id_obj)
        {
            cout << id_obj._weight << endl;
        }
    private:
        int _idnumber;
        int _weight = 5;
        int _height;
    };

private:
    int _idnumber;
    int _weight;
    int _height;
};

int main()
{
    ID a1;
    ID::id a2;
    a2.Func(a1);  // 通过对象 a1 访问 ID 类的私有成员
    return 0;
}

将B定义在A的private里;B成为A的专属内部类其他地方无法使用

#include <iostream>

using namespace std;

class ID
{
public:
    ID(int I = 1, int W = 1, int H = 1)
        :
        _idnumber(I)
        , _weight(W)
        , _height(H)
    {
    }
    void Print()
    {
        cout << _idnumber << _weight << _height << endl;
    }
   

private:
    int _idnumber;
    int _weight;
    int _height;
    class id
    {
    public:
        id(int I = 1, int W = 1, int H = 1)
            :
            _idnumber(I)
            , _weight(W)
            , _height(H)
        {
        }
        void Print()
        {
            cout << _idnumber << _weight << _height << endl;
        }
        void Func(ID& id_obj)
        {
            cout << id_obj._weight << endl;
        }
    private:
        int _idnumber;
        int _weight = 5;
        int _height;
    };
};

int main()
{
    ID a1;
    ID::id a2;    //B放在了private里,成为A的专属内部类,其他地方无法访问
    a2.Func(a1);  // 通过对象 a1 访问 ID 类的私有成员
    return 0;
}

匿名对象 

用类型(实参) 定义出来的对象叫做匿名对象,相比之前我们定义的类型对象名(实参) 定义出来的
叫有名对象
匿名对象生命周期只在当前一行,⼀般临时定义⼀个对象当前用⼀下即可,就可以定义匿名对象。
匿名对象的使用有时候可以使我们在操作的时候更简便:
#include <iostream>
using namespace std;


class Whatever
{
public:
	int Func()
	{
		//......
	}
	void Print()
	{
		cout << _a << "/" << _b << endl;
	}
private:
	int _a = 2;
	int _b = 1;
};

int main()
{
	Whatever a;//有名对象
	cout << a.Func() << endl;//用有名对象调用

	//用匿名对象调用
	cout << Whatever().Func() << endl;

}

评论 31
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值