类与对象
1.类的6个默认成员函数
- 1.1 构造函数
- 1.2 拷贝函数构造
- 1.3 析构函数
- 1.4 赋值操作符重载
- 1.5 取地址操作符重载
- 1.6 const修饰的取地址操作符重载
2. 构造函数
2.1 概念:不是创建,某种意义上来说是初始化
- 对于以下的日期类:
class Date {};
{
public:
void SetDate(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
void Display()
{
cout <<_year<< "-" <<_month << "-"<< _day <<endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1,d2;
d1.SetDate(2018,5,1);
d1.Display();
Date d2;
d2.SetDate(2018,7,1);
d2.Display();
return 0;
}
对于Date类,可以通过SetDate公有的方法给对象设置内容,但是如果每次创建对象都调用该方法设置信息,未免有点麻烦,那能否在对象创建时,就将信息设置进去呢?
构造函数是一个特殊的成员函数,名字与类名相同,创建类类型对象时由编译器自动调用,保证每个数据成员都有 一个合适的初始值,并且在对象的生命周期内只调用一次构造函数是一个特殊的成员函数,名字与类名相同,创建类类型对象时由编译器自动调用,保证每个数据成员都有 一个合适的初始值,并且在对象的生命周期内只调用一次
2.2. 特征
构造函数是特殊的成员函数,需要注意的是,构造函数的虽然名称叫构造,但是需要注意的是构造函数的主要任务并不是开空间创建对象,而是初始化对象。
- 其特征如下:
- 2.2.1. 函数名与类名相同。
- 2.2.2. 无返回值。
- 2.2.3. 对象实例化时编译器自动调用对应的构造函数。
- 2.2.4. 构造函数可以重载。
- 2.2.5. 构造函数可以在类中定义,也可以在类外定义
#include<iostream>
#include<windows.h>
using namespace std;
class Date{
public:
Date(){ //构造函数,初始化了被它定义的对象
_year = 2019;
_month = 3;
_day = 10;
}
Date1(){}//类里面声明,在类外面定义
Date(int year, int month, int day){
//函数重载,函数名相同,参数不同
_year = year;
_month = month;
_day = day;
}
void Print(){
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
Date :: Date1(){ //构造函数可以在类外面定义
_year = 2019;
_month = 3;
_day = 10;
}
int main(){
Date d1;//期望d1一出来就被初始化
d1.Print();
Date d2(2019,3,20); //函数的重载
d2.Print();
system("pause");
return 0;
}
#include<iostream>
#include<windows.h>
using namespace std;
class Date{
public:
Date();//类里面声明,在类外面定义
Date(int year, int month, int day){
//函数重载,函数名相同,参数不同
_year = year;
_month = month;
_day = day;
}
void Print(){
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
Date :: Date(){ //构造函数可以在类外面定义
_year = 2019;
_month = 3;
_day = 10;
}
int main(){
Date d2(2019,3,20);
d2.Print();
Date d3; //构造函数可以在类中定义,也可以在类外定义
d3.Print();
system("pause");
return 0;
}
- 2.2.6 如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦用户显式定义编译器将不再生成
class Date
{
public:
/*
// 如果用户显式定义了构造函数,编译器将不再生成
Date (int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
*/
private:
int _year;
int _month;
int _day;
};
void Test()
{
// 没有定义构造函数,对象也可以创建成功,因此此处调用的是编译器生成的默认构造函数
Date d;
}
- 2.2.7.无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。注意:无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为
- 是默认成员函数。
// 默认构造函数
class Date
{
public:
Date(){
_year = 1900 ;
_month = 1 ;
_day = 1;
}
Date (int year = 1900, int month = 1, int day = 1){
_year = year;
_month = month;
_day = day;
}
private :
int _year ;
int _month ;
int _day ;
};
// 以下测试函数能通过编译吗?
void Test(){
Date d1;
}
- 2.2.8 关于编译器生成的默认成员函数,很多童鞋会有疑惑:在我们不实现构造函数的情况下,
- 编译器会生成
class Time
{
public:
Time()
{
cout << "Time()" << endl;
_hour = 0;
_minute = 0;
_second = 0;
}
private:
int _hour;
int _minute;
int _second;
};
class Date
{
private:
// 基本类型(内置类型)
int _year;
int _month;
int _day;
// 自定义类型
Time _t;
//默认生成的构造函数对于 1 不做处理,对于 2 默认调用无参构造初始化
//1.基本类型:int 、char ....
//2.自定义类型:class/struct定义的类型。。。
};
int main()
{
Date d;
return 0;
}
- 默认的构造函数。但是看起来默认构造函数又没什么用?d对象调用了编译器生成的默认构造函数,但是d对象year/month/_day,依旧是随机值。也就说在这里编译器生成的默认构造函数并没有什么卵用?
- 解答:C++把类型分成内置类型(基本类型)和自定义类型。内置类型就是语法已经定义好的类型:如int/char…,自定义类型就是我们使用class/struct/union自己定义的类型,看看下面的程序,就会发现编译器生成默认的构造函数会对自定类型成员_t调用的它的默认成员函数
3.析构函数
3.1 概念:
- 析构函数:与构造函数功能相反,析构函数不是完成对象的销毁,局部对象销毁工作是由编译器
- 完成的。而对象在销毁时会自动调用析构函数,完成类的一些资源清理工作
3.2. 特性
- 析构函数是特殊的成员函数。
- 其特征如下
3.2.1. 析构函数名是在类名前加上字符 ~ 。
3.2.2. 无参数无返回值。
3.2.3. 一个类有且只有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。
3.2.4. 对象生命周期结束时,C++编译系统系统自动调用析构函数.
- 析构函数建造时按定义的相反顺序建造函数
- 对于内置类型来说没有定义析构函数的话,编译器默认去掉用析构函数
typedef int DataType;
class SeqList
{
public :
SeqList (int capacity = 10)
{
_pData = (DataType*)malloc(capacity * sizeof(DataType));
assert(_pData);
_size = 0;
_capacity = capacity;
}
~SeqList()
{
if (_pData)
{
free(_pData ); // 释放堆上的空间
_pData = NULL; // 将指针置为空
_capacity = 0;
_size = 0;
}
}
private :
int* _pData ;
size_t _size;
size_t _capacity;
};
4.1 拷贝构造函数
- 构造函数:只是单个形参,该形参是对本类类型对象的引用(一般常用const 修饰),在已存在的类类型对象创建新对象时由编译器自动调用
#include<iostream>
#include<windows.h>
using namespace std;
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1){
_year = year;
_month = month;
_day = day;
}
void print(){
cout << _year << "-" << _month << "-" << _day << endl;
}
~Date(){
cout << "~Date" << endl;
}
private:
int _year;
int _month;
int _day;
};
// 以下测试函数能通过编译吗?
int main(){
Date d1;
d1.print();
Date d2(d1); //拷贝构造
d2.print();
system("pause");
return 0;
}
4.2 特征
4.2.1 拷贝构造函数是构造函数的一个重载形式
4.2.2 拷贝构造函数的参数只有一个且必须使用引用传参,使用传参方式会引发无穷递归调用
4.2.3 若未显示定义,系统生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝我们叫做浅拷贝,或者值拷贝
4.2.4 那么编译器生成的默认拷贝构造函数已经可以完成字节序的值拷贝了,我们还需要自己实现吗?当然像日期类这样的类是没必要的。
- 代码示例
#include<iostream>
#include<windows.h>
using namespace std;
class Date{
public:
Date(int year = 2019, int month = 3, int day = 10){
_year = year;
_month = month;
_day = day;
}
void Print(){
cout << _year << "-" << _month << "-" << _day << endl;
}
Date ( Date& d){ //拷贝构造出现问题
_year = d._year;
_month = d._month;
_day = d._day;
}
private:
int _year;
int _month;
int _day;
};
int main(){
Date d1;
d1.Print();
Date d2(2018, 6, 30);// 特征2
d2.Print();
Date d3(2019);
d3.Print();
Date d4(d2); //拷贝构造-->浅拷贝(按字节进行拷贝)
d4.Print();
system("pause");
return 0;
}
// 这里会发现下面的程序会崩溃掉?这里就需要深拷贝去解决。
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);
}
//程序奔溃的原因是在进行浅拷贝时,将s1和s2的指针指向了同一片空间,在释放时空间
//已经被s2释放了,所以s1再去释放这块空间就会出错
5.赋值运算符重载
5.1 运算符重载
C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。
函数名字为:关键字operator后面接需要重载的运算符符号。
函数原型:返回值类型 operator操作符(参数列表)
// 全局的operator==
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = 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, 26);
Date d2(2018, 9, 27);
cout<<(d1 == d2)<<endl;//可读性
}
//
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
// bool operator==(Date* this, const Date& d2) //增加可读性
// 这里需要注意的是,左操作数是this指向的调用函数的对象
bool operator==(const Date& d2)
{
return _year == d2._year
&& _month == d2._month
&& _day == d2._day;
}
private:
int _year;
int _month;
int _day;
};
void Test ()
{
Date d1(2018, 9, 26);
Date d2(2018, 9, 27);
cout<<(d1 == d2)<<endl;
cout<<operator==(d1,d2)<<endl;
}
注意:
-
5.1.1 不能通过连接其他符号来创建新的操作符:比如operator@
-
5.1.2 重载操作符必须有一个类类型或者枚举类型的操作数
-
5.1.3 用于内置类型的操作符,其含义不能改变,例如:内置的整型+,不能改变其含义
-
5.1.4 作为类成员的重载函数时,其形参看起来比操作数数目少1成员函数的操作符有一个默认的形参this,限定为第一个形参
-
5.1.5 < .* >< :: > < sizeof > < ?: >< . >注意以上5个运算符不能重载。这个经常在笔试选择题中出现
5.2. 赋值运算符重载
赋值运算符主要有四点:
- 5.2.1. 参数类型
- 5.2.2. 返回值
- 5.2.3. 检测是否自己给自己赋值
- 5.2.4. 返回*this
- 5.2.5. 一个类如果没有显式定义赋值运算符重载,编译器也会生成一个,完成对象按字节序的值拷贝
#include<iostream>
#include<windows.h>
using namespace std;
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
//d2=d1
//d2.operator(d1)
//d2.operator(&d2,d1)
void operator=(const Date& d){ //默认含有第一个参数的地址,
//已经被this指针所指代
if (this != &d){ //防止自己给自己传值
this->_year = d._year;
this->_month = d._month;
this->_day = d._day;
}
}
/*Date operator=(const Date& d){ //有返回值
if (this != &d){
this->_year = d._year;
this->_month = d._month;
this->_day = d._day;
}
return *this;
}*/
void print(){
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main(){
Date d1(2018,10, 20);
Date d2(2019,10, 10);
d1.print();
// 这里d1调用的编译器生成operator=完成拷贝,d2和d1的值也是一样的。
Date d3(d1); //拷贝构造
d1 = d2; //赋值运算符的用运
d3.print();
d1.print();
//用运这个表达式需要调用函数Date operator=(const Date& d)
//d3 = d2 = d1;//被转换成 d3=(d2.oprator=(&d2,d1))
//d3.print();
//Date d4 = d1; //拷贝构造,将已知的值赋给创建的变量。
//运算符重载
//函数重载 两者没有任何关系
system("pause");
return 0;
}
运行结果
不加
d3 = d2 = d1;
加上d3 = d2 = d1;
Date operator-(int day);//日期减去日期
int operator-(const Date& day); //返回天数
//两者而言即是函数重载,又是运算符重载
简单整理:
封装:管理
构造、析构:初始化、清理
拷贝构造、operator= : 复制
- 那么编译器生成的默认赋值重载函数已经可以完成字节序的值拷贝了,我们还需要自己实现吗?当然像日期类这样的类是没必要的。那么下面的类呢?验证一下试试?
// 这里会发现下面的程序会崩溃掉?这里就需要我们以后讲的深拷贝去解决。
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("world");
s1 = s2;
}
const 成员
将const修饰的类成员函数称为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改
#include<iostream>
using namespace std;
class Date{
public:
void Display(){
cout<<"Display()"<<endl;
cout<<"year:"<<++_year<<endl;
cout<<"month:"<<_month<<endl;
cout<<"day:"<<_day<<endl;
}
void Display()const{
cout<<"Display()"<<endl;
cout<<"year:"<<_year<<endl;
//其中的_year++不被允许
//const修饰的是类中的this指针,其不允许
//类中的成员变量被修改
cout<<"month:"<<_month<<endl;
cout<<"day:"<<_day<<endl;
}
private:
int _year=1997;
int _month=8;
int _day=1;
};
void Test(){
Date d1;
d1.Display();
const Date d2;
d2.Display();
}
int main(){
Test();
return 0;
}
- 总结
- const对象不可以调用非const成员函数
- 非const对象可以调用const成员函数
- const成员函数内不可以调用其他的const成员函数
- 非const成员函数内可以调用其他的const成员函数
取地址与const取地址操作符重载
这两个默认成员函数一般不用重新定义,编译器默认会生成
class Date{
public:
Date* operator&(){
return this;
}
const Date* operator&()const{
return this;
}
private:
int _year;
int _month;
int _day;
};
这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需要重载,比如想让别人获取到指定的内容!