类和对象(下)拷贝构造函数、赋值运算符重载、const成员、构造函数体赋值和初始化列表、static成员、友元、内部类

目录

 

8、拷贝构造函数

8.1、特征

8.2、浅拷贝构造无法解决类当中有堆空间的拷贝

9、赋值运算符重载

9.1、运算符重载

9.2、赋值运算符重载

10、const成员

1、构造函数体赋值和初始化列表

1.1、构造函数体赋值

1.2初始化列表

2、static成员

3、友元

3.1友元函数。

3.2友元类

4、内部类


8、拷贝构造函数

拷贝构造函数只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型对象

创建新对象时由编译器自动调用

8.1、特征

拷贝构造函数也是特殊的成员函数,其特征如下:

1. 拷贝构造函数是构造函数的一个重载形式

2. 拷贝构造函数的参数只有一个必须使用引用传参,使用传值方式会引发无穷递归调用

class Date
{
public:
 Date(int year = 1900, int month = 1, int day = 1)
 {
 _year = year;
 _month = month;
 _day = day;
 }
 Date(const Date& d)
 {
 _year = d._year;
 _month = d._month;
 _day = d._day;
 }
private:
 int _year;
 int _month;
 int _day;
};
int main()
{
 Date d1;
 Date d2(d1);
 return 0;
}

3. 若未显示定义,系统生成默认的拷贝构造函数。 默认的拷贝构造函数对象以内存存储按字节序完成拷 贝,这种拷贝我们叫做        浅拷贝,或者值拷贝。

8.2、浅拷贝构造无法解决类当中有堆空间的拷贝

#include <iostream>
#include <stdlib.h>
using namespace std;
class String
{
public:
	String(const char* str = "jack")
	{
		_str = (char*)malloc(strlen(str) + 1);
		strcpy(_str, str);
	}
	~String()
	{
		cout << "~String()" << endl;
		free(_str);
	}
private:
	char* _str;
};
int main()
{
	String s1("hello");
	String s2(s1);
	system("pause");
	return 0;
}

前面说过浅拷贝是按字节序拷贝的,以上程序在析构的时候会发生错误,调用s2的析构函数时free函数报错,因为s2对象根本没有开辟过空间只是对s1的值拷贝,所以无法执行free。以上程序就需要深拷贝。

9、赋值运算符重载

9.1、运算符重载

C++为了增强代码的可读性引入了运算符重载运算符重载是具有特殊函数名的函数,也具有其返回值类 型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。

函数名字为:关键字operator后面接需要重载的运算符符号

函数原型:返回值类型 operator操作符(参数列表)

注意:

  • 不能通过连接其他符号来创建新的操作符:比如operator@
  • 重载操作符必须有一个类类型或者枚举类型的操作数
  • 用于内置类型的操作符,其含义不能改变,例如:内置的整型+,不 能改变其含义
  • 作为类成员的重载函数时,其形参看起来比操作数数目少1的成员函数
  • 操作符有一个默认的形参this,限定为第一个形参
  • .* :: sizeof ?: . 注意以上5个运算符不能重载。这个经常在笔试选择题中出现。
 #include <iostream>
using namespace std;
// 全局的operator==
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	/* 左操作数省略的情况下,运算符重载应该在类的内部 */
	bool operator==(const Date& d2)/*左操作数是this,指向的调用函数的对象 */
	{
		return _year == d2._year&&_month == d2._month&&_day == d2._day;
	}
	//private:
	int _year;
	int _month;
	int _day;
};
// 这里会发现运算符重载成全局的就需要成员变量是共有的,那么问题来了,封装性如何保证?
// 这里其实可以用我们后面学习的友元解决,或者干脆重载成成员函数。
bool operator==(const Date& d1, const Date& d2)
{
	return d1._year == d2._year
		&& d1._month == d2._month
		&& d1._day == d2._day;
}

void Test()
{
	Date d1(2018, 9, 27);
	Date d2(2018, 9, 27);
	cout << (d1 == d2) << endl;
	cout << d1.operator==(d2);
}
int maind()
{
	Test();
	system("pause"); 
	return 0;
}

9.2、赋值运算符重载

赋值运算符重载主要有以下五个注意点

1. 参数类型:本类对象的引用

2. 返回值:本类对象的引用

3. 检测是否自己给自己赋值

4. 返回*this

5. 一个类如果没有显式定义赋值运算符重载,编译器也会生成一个,完成对象按字节序的值拷贝。

#include <iostream>
using namespace std;
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date& operator=(const Date& d)/* 运算符重载参数也是引用 */
	{
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1;
	Date d2(2018, 10,1);

	// 这里d1调用的编译器生成operator=完成拷贝,d2和d1的值也是一样的。
	d1 = d2;
	system("pause"); 
	return 0;
}

10、const成员

https://blog.csdn.net/weixin_43447989/article/details/100548283

至此类的六大默认函数全部总结完毕,接下来我们来看类的一些细节玩法。

1、构造函数体赋值和初始化列表

1.1、构造函数体赋值

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

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

顾名思义构造函数体赋值就是用参数和=的方式给类的变量赋初值的行为,和它类似的行为有初始化列表

1.2初始化列表

初始化列表:以一个冒号开始,接着是一个以逗号分隔的数据成员列表,每个"成员变量"后面跟一个放在括号中的初始值或表达式。

注意:

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

2. 类中包含以下成员,必须放在初始化列表位置进行初始化:

引用成员变量

const成员变量

类类型成员(该类没有默认构造函数)

 class A
{
public:
 A(int a)
 :_a(a)
 {}
private:
 int _a;
};
class B
{
public:
 B(int a, int ref)
 :_aobj(a)
 ,_ref(ref)
 ,_n(10)
 {}
private:
 A _aobj; // 没有默认构造函数
 int& _ref; // 引用
 const int _n; // const 
};

2、static成员

https://blog.csdn.net/weixin_43447989/article/details/100548859

3、友元

友元分为友元函数和友元类,友元提供了一种突破封装的方式,有时提供了便利。但是友元会增加耦合度,破坏了封装,所以友元不宜多用。除了约定俗成的方式下,一般不用,是普通的外部函数,和类没有关系。

3.1友元函数。

问题:现在我们尝试去重载operator<<,然后发现我们没办法将operator<<重载成成员函数。因为cout的输出流对象和隐含的this指针在抢占第一个参数的位置。this指针默认是第一个参数也就是左操作数了。但是实际使用中cout需要是第一个形参对象,才能正常使用。所以我们要将operator<<重载成全局函数。但是这样的话,又会导致类外没办法访问成员,那么这里就需要友元来解决。

class Date
{
friend ostream& operator<<(ostream& _cout, const Date& d);
friend istream& operator>>(istream& _cin, const Date& d);
public:
 Date(int year, int month, int day)
 : _year(year)
, _month(month)
 , _day(day)
 {}
 
prvate:
 int _year;
 int _month;
 int _day
};
ostream& operator<<(ostream& _cout, const Date& d)
{
 _cout<<d._year<<"-"<<d._month<<"-"<<d._day;
 
 return _cout;
}
istream& operator>>(istream& _cin, const Date& d)
{
 _cin>>d._year;
 _cin>>d._month;
 _cin>>d._day;
 
 return _cin;
}
int main()
{
 Date d;
 cin>>d;
 cout<<d<<endl;
 return 0;
}

在这里需要解释一下输出运算符重载,可以见到声明了一个ostream类类型的变量,这个类是专门用作输出操作的,里面内置了printf,重载<<,只是让其输出内容改变并没有改变它的实现方式。输入算符重载也是一样。

注意:

  • 友元函数可访问类的私有成员,但不是类的成员函数
  • 友元函数不能用const修饰
  • 友元函数可以在类定义的任何地方声明,不受类访问限定符限制
  • 一个函数可以是多个类的友元函数
  • 友元函数的调用与普通函数的调用和原理相同

3.2友元类

  • 一个外部类可以通过友元的方式来访问另一个类当中的成员函数和私有成员变量。
  • 友元关系是单向的,不具有交换性。

      比如上述Time类和Date类,在Time类中声明Date类为其友元类,那么可以在Date类中直接访问Time 类的私有成员变量,但       想在Time类中访问Date类中私有的成员变量则不行。

  • 友元关系不能传递

       如果BA的友元,CB的友元,则不能说明CA的友元。

 class Date; // 前置声明
class Time
{
 friend class Date; // 声明日期类为时间类的友元类,则在日期类中就直接访问Time类中的私有成员变
量
public:
 Time(int hour, int minute, int second)
 : _hour(hour)
 , _minute(minute)
 , _second(second)
 {}
 
private:
 int _hour;
 int _minute;
 int _second;
};
class Date
{
public:
 Date(int year = 1900, int month = 1, int day = 1)
 : _year(year)
 , _month(month)
 , _day(day)
 {}
 
 void SetTimeOfDate(int hour, int minute, int second)
 {
 // 直接访问时间类私有的成员变量
 _t._hour = hour;
 _t._minute = minute;
 _t.second = second;
 }
 
private:
 int _year;
 int _month;
 int _day;
 Time _t;
};

4、内部类

如果一个类定义在另一个类的内部,那么这个类就叫做这个类的内部类。外部类对内部类没有优越的访问权限,但是内部类却可以自由的访问外部类的成员函数和变量,也就是说外部类是内部类的友元。

注意:

内部类就是外部类的友元类。注意友元类的定义,内部类可以通过外部类的对象参数来访问外部类中
的所有成员。但是外部类不是内部类的友元。

sizeof(外部类)=外部类,和内部类没有任何关系。

class A
{
private:
 static int k;
 int h;
public:
 class B
 {
 public:
 void foo(const A& a)
 {
 cout << k << endl;//OK
 cout << a.h << endl;//OK
 }
 };
};
int A::k = 1;
int main()
{
 A::B b;
 b.foo(A());
 
 return 0;
}
发布了157 篇原创文章 · 获赞 98 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_43447989/article/details/100838445
今日推荐