C++的类与对象下

目录

1.初始化列表

2.隐式类型转换

1.单参数

2.多参数(C++11提供的新功能)

3.static成员

4.友元

5.内部类

6.匿名对象


1.初始化列表

C++祖师爷规定初始化列表成员变量定义与初始化的地方。

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


class Date
{
public:
	Date(int& x, int year = 1, int month = 1, int day = 1)
		:_year(year)
		, _month(month)
		, _day(day)
		, _t(12)
		, _ref(x)
		, _n(1)
	{
		// error : “Time”: 没有合适的默认构造函数可⽤ 
		// error : “Date::_ref” : 必须初始化引⽤ 
		// error : “Date::_n” : 必须初始化常量限定类型的对象 
	}
	void Print() const
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
	Time _t; // 没有默认构造 
	int& _ref; // 引⽤ 
	const int _n; // const 
};
int main()
{
	int i = 0;
	Date d1(i);
	d1.Print();
	return 0;
}

初始化列表初始化变量的顺序与类里声明变量的顺序是一致的

2.隐式类型转换

1.单参数

class Time1
{
public:
	Time1(int hour=0)
		:_hour(hour)
	{

	}
	Time1(Time1& t)
	{
		_hour = t._hour;
	}
private:
	int _hour;
};

int main()
{
	//第一点:3会隐式类型转换成Time1类的对象,其中调用的就是构造函数
	//第二点:构造完临时对象后,就会将这个临时对象拷贝构造给t对象
	Time1 t = 3;
	return 0;
}

2.多参数(C++11提供的新功能)

class Time1
{
public:
	Time1(int hour1=0,int hour2=0)
		:_hour1(hour1)
		,_hour2(hour2)
	{

	}
	Time1(Time1& t)
	{
		_hour1 = t._hour1;
		_hour2 = t._hour2;
	}
private:
	int _hour1;
	int _hour2;
};

int main()
{
	//第一点:{3,3}会隐式类型转换成Time1类的对象,其中调用的就是构造函数
	//第二点:构造完临时对象后,就会将这个临时对象拷贝构造给t对象
	Time1 t = { 3, 3 };
	return 0;
}

const Time1& t = { 3,3 };

其中引用的话必须是const引用,因为{3,3}构造的临时对象具有常性,不能被改变。

3.static成员

静态成员不存在对象里,所以不走初始化列表。

// 实现⼀个类,计算程序中创建出了多少个类对象? 
#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;
}

4.友元

class Date
{
	friend void Print(Date x);
public:
	Date(int hour, int minute)
	{
		_hour = hour;
		_minute = minute;
	}

private:
	int _hour;
	int _minute;
};

//Print函数需要访问Date类中的成员变量,所以需要将其设置为友元函数
void Print(Date x)
{
	cout << x._hour << '/' << x._minute << endl;
}
class Date
{
	friend class B;
public:
	Date(int hour, int minute)
	{
		_hour = hour;
		_minute = minute;
	}

private:
	int _hour;
	int _minute;
};


//如果一个类里面要频繁调用另一个类的私有成员变量,则可以将此类设置为另一个类的友元类
class B
{
public:
	void Print1(Date x)
	{
		cout << x._hour << endl;
	}
	void Print2(Date x)
	{
		cout << x._minute << endl;
	}
};

5.内部类

class B
{
private:
	int a;
	int b;

public:
	//A默认是B的友元类
	class A
	{
	public:
		void Print(B c)
		{
			cout << c.a << c.b << endl;
		}
	};
};

6.匿名对象

class Date
{
public:
	Date(int hour = 0)
	{
		_hour = hour;
	}
	void Print()
	{
		cout << _hour << endl;
	}


private:
	int _hour;
};



int main()
{
	//定义有名对象
	Date d;
	Date d1(1);


	//匿名对象
	Date();
	Date(1);
	//当我们只想要调用函数时,用匿名对象比较方便
	Date().Print();


	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值