C++类和对象(一)

一.类的引入

在C语言阶段,struct关键字用来构造用户自定义数据类型,只能用来定义变量,C++把struct升级成了类,既能定义变量,又能定义函数,同时兼容C语言struct结构体的所有用法,类由两部分组成,一部分是成员变量(属性),另一部分是成员函数(用来对成员变量进行操作)

C++中struct和class都能定义类,它们的区别是什么呢?

C++的struct是兼容C语言的,所以C++的struct可以当作结构体去使用,另外struct还可以用来定义类,和class定义类是一样的,区别是struct定义类默认访问权限是公有的,class定义类默认访问权限是私有的

typedef int STDataType;
struct Stack
{
    
    
	// 定义成员函数
	void StackInit(int initcapacity = 4)
	{
    
    
		_a = (int*)malloc(sizeof(int) * initcapacity);
		_size = 0;
		_capacity = initcapacity;
	}
	void StackPush(STDataType x)
	{
    
    
		_a[_size++] = x;
	}
	// 定义成员变量
	STDataType* _a;
	int _size;
	int _capacity;
};
int main()
{
    
    
	Stack st;
	st.StackInit(10);
	st.StackPush(1);
	st.StackPush(2);
	st.StackPush(3);
}

二.类的定义

class 类名
{
    
    
	类体 // 定义成员函数和成员变量
};

类的两种定义方式 :

一.声明和定义全部放在类内

class Stack
{
    
    
public:
	void StackInit(int capacity = 4)
	{
    
    
		_a = (int*)malloc(sizeof(int) * capacity);
		_size = 0;
		_capacity = capacity;
	}
private:
	int* _a;
	int _size;
	int _capacity;
};

二.声明和定义分开,声明在类内,定义在类外,在类外定义的时候记得加上域作用限定符

class Stack
{
    
    
public:
	void StackInit(int capacity = 4);
private:
	int* _a;
	int _size;
	int _capacity;
};
void Stack::StackInit(int capacity)
{
    
    
	_a = (int*)malloc(sizeof(int) * capacity);
	_size = 0;
	_capacity = capacity;
}

三.类的访问限定符和封装

封装 :
在C语言阶段,数据和方法是分离开来的,使用方法可以去修改数据,但这样会破坏原有的数据,因此C++引入了封装
封装本质上是一种管理,封装把数据和方法封装到一起,隐藏对象的属性和实现细节,仅提供对外接口来实现和对象的交互

访问限定符 : public,protected,private

(1). public修饰的成员在类外可以直接被访问
(2). protected和private修饰的成员在类外不能直接被访问(此处protected和private是类似的)
(3). 访问权限作用域从该访问限定符出现的位置开始直到下一个访问限定符出现时为止
(4). class的默认访问权限为private,struct为public(因为struct要兼容C)

四.类的实例化和类对象模型

用类类型创建对象的过程,称为类的实例化
(1). 类只是一个模型一样的东西,限定了类有哪些成员,定义出一个类并没有分配实际的内存空间来存储它
(2). 一个类可以实例化出多个对象,实例化出的对象占用实际的物理空间,存储类成员变量,类实例化出对象就像现实中使用建筑设计图建造出房子,类就像是设计图,只设计出需要什么东西,但是并没有实体的建筑存在,同样类也只是一个设计,实例化出的对象才能实际存储数据,占用物理空间

类对象的存储方式 :

对象保存成员变量,成员函数存放在公共代码段

计算类对象的大小

方法 : 计算类对象的大小只需要计算成员变量的大小,因为成员函数没有存放在对象里,计算成员变量的大小时注意内存对齐,内存对齐在之前C语言阶段已经讲过,遗忘的可以去回顾复习一下

// 类中既有成员变量,又有成员函数
class A1
{
    
    
public:
 	void f1()
 	{
    
    }
private:
 	int _a;
};
// sizeof(A1) = 4

// 类中仅有成员函数
class A2
{
    
    
public:
 	void f2()
 	{
    
    }
};
// sizeof(A2) = 1

// 类中什么都没有---空类
class A3
{
    
    };
// sizeof(A3) = 1

注意当类中无成员函数时,类的大小不是0,而是1,因为我们用类去实例化多个对象出来时,为了区分这些对象,需要给一个字节去唯一标识对象

五.this指针

class Date
{
    
    
public:
	void Init(int year = 0,int month = 1,int day = 1)
	{
    
    
		_year = year;
		_month = month;
		_day = day;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
    
    
	Date d1;
	d1.Init(2021,5,29);
	
	Date d2;
	d2.Init(2021,5,30);
}

对于上面的类,有这样一个问题 :

d1和d2调用的函数都是同一个位于公共代码区的函数Init(),但Init()函数怎么知道是哪个对象调用的它呢?

C++通过引入了this指针来解决这个问题,C++编译器给每个“非静态的成员函数“增加了一个隐藏的指针参数,让该指针指向当前对象(函数运行时调用该函数的对象),在函数体中所有成员变量的操作,都是通过该指针去访问。只不过所有的操作对用户是透明的,即用户不需要来传递,编译器自动完成。

因此上面的函数调用编译器会处理为如下

d1.Init(&d1,2021,5,29);
d2.Init(&d2,2021,5,30);

void Init(Date* this,int year = 0,int month = 1,int day = 1)
{
    
    
		this->_year = year;
		this->_month = month;
		this->_day = day;
}

this指针的特性 :
(1). this指针的类型:类类型* const
(2). 只能在“成员函数”的内部使用
(3). this指针本质上其实是一个成员函数的形参,是对象调用成员函数时,将对象地址作为实参传递给this形参。所以对象中不存储this指针,this指针存储在栈中
(4). this指针是成员函数第一个隐含的指针形参,一般情况由编译器通过ecx寄存器自动传递,不需要用户传递.

了解了this指针之后,我们来看一道题

// p->PrintA()结果怎么样
// 程序崩溃
// p->Show()结果怎么样
// 正常运行
class A
{
    
     
public:
	void PrintA() 
 	{
    
    
 		cout<<_a<<endl;
 	}
 	void Show()
 	{
    
    
 		cout<<"Show()"<<endl;
 	}
private:
 	int _a;
};
int main()
{
    
    
 	A* p = nullptr;
 	p->PrintA();
 	p->Show();
 }

上文提到,成员函数存放到代码段,供所有对象进行调用,所以p->PrintA(),p->Show()实际上并没有对p指针解引用,编译器处理后

p->PrintA(p);
void PrintA(A* this)
{
    
    
	cout<<this->_a<<endl;
	// this为空指针,对空指针进行解引用操作,崩溃
}

p->Show(p);
void Show(A* this)
{
    
    
	cout<<"Show()"<<endl;
	// 并没有对this指针进行解引用,不会引发崩溃
}

六.类的6个默认成员函数

类里面什么成员函数都没有时,编译器会自动生成6个默认成员函数

(1).构造函数

构造函数是一个特殊的成员函数
构造函数并不是开空间创建对象,而是初始化对象

特点:
(1). 函数名与类名相同。
(2). 无返回值。
(3). 对象实例化时编译器自动调用对应的构造函数。
(4). 构造函数可以重载。
(5). 如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦用户自己定义,编译器不再提供构造函数
(6).无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。注意:无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为是默认成员函数。

class Date
{
    
    
public:
	Date()
	{
    
    
		_year = 0;
		_month = 1;
		_day = 1;
	}
	// 重载
	Date(int year = 0,int month = 0,int day = 1)
	{
    
    
		_year = year;
		_month = month;
		_day = day;
	}
	// 这两个构造函数只能写一个
private:
	int _year;
	int _month;
	int _day;
};

(7).编译器生成的默认构造函数,对内置类型不初始化,对自定义类型调用自定义类型自己的默认构造函数

内置类型不进行处理是C++语法设计的小缺陷,所以C++11给出了补充语法 : 声明缺省值,来补足缺陷

class A
{
    
    
public:
	A(int a = 0)
	{
    
    
		_a = a;
	}
private:
	int _a;
};
class Date
{
    
    
private:
	int _year;
	int _month;
	int _day;
	A aa;
};
int main()
{
    
    
	Date d;
	// d对象的_year,_month,_day为随机值,aa对象的_a值为0
}
class B
{
    
    
public:
	B(int b = 0)
	:_b(b)
	{
    
    }
private:
	int _b;
};
class C
{
    
    
public:
	C(int c1 = 0,int c2 = 0)
		:_c1(c1)
		,_c2(c2)
	{
    
    }
private:
	int _c1;
	int _c2;
};
class A
{
    
    
private:
	int _a1 = 1;
	int _a2 = 1;
	// B _b = B(1);
	// B _b = 1;
	// C _c = C(1, 1);
	// C _c = { 1,1 };
};

再来看一个问题,D的构造函数中在哪里调用的B的默认构造函数呢?

在D的初始化列表里,成员变量在初始化列表进行定义初始化,无论显示给出初始化列表还是隐式给出,在进入D的构造函数之前,都会走一遍初始化列表

class B
{
    
    
public:
	B(int b = 0)
	:_b(b)
	{
    
    }
private:
	int _b;
};
class D
{
    
    
public:
	D()
	{
    
    }
private:
	int _d = 0;
	B _b;
};

(2).析构函数

析构函数是一个特殊的成员函数
析构函数并不是完成对象的销毁,而是完成类的资源清理工作

特点 :
(1). 析构函数名是在类名前加上字符 ~。
(2). 无参数无返回值。
(3). 一个类有且只有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。
(4). 对象生命周期结束时,C++编译系统系统自动调用析构函数

class Stack
{
    
    
public:
	Stack(int capacity = 4)
	{
    
    
		_a = (int*)malloc(sizeof(int) * capacity);
		_size = 0;
		_capacity = capacity;
	}
	~Stack()
	{
    
    
		free(_a);
		_a = NULL;
		_size = _capacity = 0;
	}
private:
	int* _a;
	int _size;
	int _capacity;
};

(5).编译器生成的默认析构函数,对内置类型不处理,对自定义类型调用自定义类型自己的析构函数

class A
{
    
    
public:
	A(int a = 0)
	{
    
    
		_a = a;
	}
	~A()
	{
    
    
		cout<<"~A()"<<endl;
	}
private:
	int _a;
};
class Stack
{
    
    
public:
	Stack(int capacity = 4)
	{
    
    
		_a = (int*)malloc(sizeof(int) * capacity);
		_size = 0;
		_capacity = capacity;
	}
private:
	int* _a;
	int _size;
	int _capacity;
	A aa;
};
int main()
{
    
    
	Stack st;
}

(3).拷贝构造函数

拷贝构造函数是一个特殊的成员函数
拷贝构造函数:只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型对象创建新对象时由编译器自动调用

特点 :
(1). 拷贝构造函数是构造函数的一个重载形式。
(2). 拷贝构造函数的参数只有一个且必须使用引用传参,使用传值方式会引发无穷递归调用。

class Date
{
    
    
public:
	Date(int year = 0, int month = 0, 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(2021,5,29);
	Date d2(d1);
}

(3).若未显示定义,系统生成默认的拷贝构造函数。 默认的拷贝构造函数对内置类型按内存存储按字节序完成拷贝,这种拷贝我们叫做浅拷贝,或者值拷贝,但编译器生成的默认拷贝构造函数对于以下的程序是有问题的

对自定义类型调用自定义类型的拷贝构造函数

// 程序会崩溃掉
class Stack
{
    
    
public:
	Stack(int capacity = 4)
	{
    
    
		_a = (int*)malloc(sizeof(int) * capacity);
		_size = 0;
		_capacity = capacity;
	}
	~Stack()
	{
    
    
		free(_a);
		_a = nullptr;
	}
private:
	int* _a;
	int _size;
	int _capacity;
};
int main()
{
    
    
	Stack st;
	Stack copy(st);
}

这就产生了浅拷贝问题,一段malloc出来的空间被释放了两次,导致程序崩溃,需要我们自己去实现深拷贝

(4).赋值运算符重载

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

函数名字为:operator + 后面接需要重载的运算符符号。
函数原型:返回值类型 operator操作符(参数列表)

注意:
(1).不能通过连接其他符号来创建新的操作符:比如operator@
(2).重载操作符必须有一个类类型或者枚举类型的操作数
(3).用于内置类型的操作符,其含义不能改变,例如:内置的整型+,不 能改变其含义
(4).作为类成员的重载函数时,其形参比操作数数目少1,操作符有一个默认的形参this,限定为第一个形参
(5) .* 、:: 、sizeof 、?: 、. 注意以上5个运算符不能重载.

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

	cout << (d1 == d2) << endl;
}

赋值运算符重载

赋值运算符重载也是拷贝行为,但和拷贝构造不同的是,拷贝构造函数是创建一个对象时,拿同类对象对其初始化,而赋值运算符重载是两个对象已经存在(都完成了初始化),把一个对象拷贝给另一个对象

class Date
{
    
     
public :
 	Date(int year = 0, 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;
 	}
 	// 赋值运算符重载
	 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;
};

编译器默认生成的赋值运算符重载,对内置类型会完成浅拷贝,对自定义类型会去调用自定义类型自己的赋值运算符重载

(5).取地址操作符重载和const修饰的取地址操作符重载

这两个默认成员函数一般不用重新定义 ,编译器默认会生成。

class Date
{
    
    
public :
	// 取地址操作符重载
 	Date* operator&()
 	{
    
    
 		return this ;
 	}
 	// const修饰的取地址操作符重载
 	const Date* operator&()const
 	{
    
    
 		return this ;
 	}
private :
 	int _year ; // 年
 	int _month ; // 月
 	int _day ; // 日
};
int main()
{
    
    
	Date d1(2021,5,30);
	const Date d2(2021,5,31);
	&d1;
	&d2;
}

七.const成员

const修饰类的成员函数

将const修饰的类成员函数称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。

class Date
{
    
     
public :
 	Date(int year = 0, int month = 1, int day = 1)
 	{
    
    
 		_year = year;
		_month = month;
 		_day = day;
 	}
 	// 编译器处理为 bool operator==(const Date* this,const Date& d2)
 	bool operator==(const Date& d2)const
	{
    
    
		return _year == d2._year
			&& _month == d2._month
			&& _day == d2._day;
	}
private:
 	int _year;
 	int _month;
 	int _day;
};

思考以下问题 :

(1). const对象可以调用非const成员函数吗?

// 编译器处理为 bool operator==(Date* this,const Date& d2)
 bool operator==(const Date& d2)
{
    
    
	return _year == d2._year
		&& _month == d2._month
		&& _day == d2._day;
}
int main()
{
    
    
	const Date d1(2021,5,30);
	Date d2(2021,5,31);
	d1 == d2; // 错误
	// 编译器处理为 d1.operator==(&d1,d2);
	// &d1类型为 const Date*    this指针类型为 Date* ,属于权限放大,不可以
}

(2). 非const对象可以调用const成员函数吗?

// 编译器处理为 bool operator==(const Date* this,const Date& d2)
 bool operator==(const Date& d2)const
{
    
    
	return _year == d2._year
		&& _month == d2._month
		&& _day == d2._day;
}
int main()
{
    
    
	Date d1(2021,5,30);
	Date d2(2021,5,31);
	d1 == d2; // 正确
	// 编译器处理为 d1.operator==(&d1,d2);
	// &d1类型为 Date*    this指针类型为 const Date* ,属于权限缩小,可以
}

(3). const成员函数内可以调用其它的非const成员函数吗?

void func1()const
{
    
    
	// 编译器处理为 Print(func1);
	Print();
}
// 编译器处理为 void Print(Date* this)
void Print()
{
    
    }
// func1类型为 const Date*   this指针为Date*
// 属于权限的放大,不可以

(4). 非const成员函数内可以调用其它的const成员函数吗?

void func1()
{
    
    
	// 编译器处理为 Print(func1);
	Print();
}
// 编译器处理为 void Print(const Date* this)
void Print()const
{
    
    }
// func1类型为 Date*   this指针为const Date*
// 属于权限的缩小,可以

猜你喜欢

转载自blog.csdn.net/DR5200/article/details/117258256
今日推荐