构造函数,拷贝构造函数,析构函数,赋值运算符重载

1、构造函数
定义:是一个特殊的成员函数,名字与类名相同,创建类类型对象时,由编译器自动调用,在对象的生命周期内有且只调用一次,以保证每个数据成员都有一个合适的初始值。

特性: 

1、函数名与类名相同。

2、没有返回值。
3、有初始化列表(可以不用)。
4、构造函数可以重载,实参决定了调用那个构造函数。
5、如果没有显示定义时,编译器会提供一个默认的构造函数。
6、无参构造函数和带有缺省值的构造函数都认为是缺省的构造函数,并且缺省构造函数只能有一个。
7、新对象被创建,由编译器自动调用,且在对象生命周期内仅调用一次。

8、构造函数不能用const来修饰.

初始化列表

定义:以一个冒号开始,接着是一个以逗号分隔的数据成员列表,每个数据成员后面跟一个放在圆括号中的初始化式。
#include<iostream>
using namespace std;
class Date
{
public:
//Date(int year,int month,int day)//不缺省的构造函数
Date(int year,int month = 10,int day = 1)//半缺省的构造函数,带缺省值的参数必须放在参数表的最后面(month和day)
//Date(int year = 2000,int month = 10,int day = 1)//全缺省的构造函数
//下面这三行是初始化列表
:Year(year)
,Month(month)
,Day(day)
{
cout<<"Year = "<<year<<" Month = "<<month<<" Day = "<<day<<endl;//输出要打印的结果
}
private:
int Year;
int Month;
int Day;
};
void FunTest()
{
//Date t(2016,10,10);
Date t0(2016);
//Date t1;
}
int main()
{
FunTest();
return 0;
}
2、拷贝构造函数
定义:只有单个形参,而且该形参是对本类类型对象的引用(常用const修饰),这样的构造函数成为拷贝构造函数。创建对象时使用已有的同类对象来进行初始化。

特征:    

1、它是构造函数的重载。

2、它的参数必须使用同类型对象引用传递。

3、如果没有显示定义,系统会自动合成一个默认的拷贝函数,默认的拷贝函数会依次拷贝类的数据成员完成初始化。

#include<iostream>
using namespace std;
class Date
{
public:
Date(int year = 2000,int month = 10,int day = 1)//全缺省的构造函数
:Year(year)
,Month(month)
,Day(day)
{
cout<<year<<"-"<<month<<"-"<<day<<endl;//输出要打印的结果
cout<<"Date: "<<this<<endl;//输出地址

}

Date(const Date& d)//拷贝构造函数:拷贝构造函数必须使用同类型对象引用传递
:Year(d.Year)
,Month(d.Month)
,Day(d.Day)
{
cout<<"CDate:"<<this<<endl;
}
private:
int Year;
int Month;
int Day;
};
void FunTest1(const Date d)//传值方式作为函数参数
{
// 输出什么?
}
Date FunTest2()//传值方式作为函数返回值
{
Date t;
return t;
}
void FunTest()
{
Date t;
Date t1(t);//拷贝构造函数
FunTest1(t);//传值方式作为函数参数
FunTest2();//传值方式作为函数返回值
}
int main()
{
FunTest();
return 0;

}

3、析构函数

定义:与构造函数功能相反,在对象被销毁时,由编译器自动调用,完成类的一些资源清理和汕尾工作

特性:   

1、在类名前加~

2、析构函数无参数无返回值。

3、一个类有且只有一个析构函数,若未显示定义,系统会自动生成缺省的析构函数。

4、析构函数体内并不是删除对象,而是做一些清理工作。

#include<iostream>
using namespace std;
class Date
{
public:
Date(int year = 2000,int month = 10,int day = 1)//全缺省的构造函数
:Year(year)
,Month(month)
,Day(day)
{
cout<<year<<"-"<<month<<"-"<<day<<endl;//输出要打印的结果
cout<<"Date: "<<this<<endl;//输出地址?谁的地址?
}
Date(const Date& d)//拷贝构造函数:拷贝构造函数必须使用同类型对象引用传递
:Year(d.Year)
,Month(d.Month)
,Day(d.Day)
{
cout<<"CDate:"<<this<<endl;
}
~Date()//析构函数
{
cout<<"~Date: "<<this<<endl;
}
private:
int Year;
int Month;
int Day;
};
void FunTest()
{
Date t;
Date t1(t);
}
int main()
{
FunTest();
return 0;
}

4、赋值运算符重载

定义:重载操作符是具有特殊函数名的函数,关键字operator后面接需要定义的操作符符号。

        操作符重载也是一个函数,具有返回值和形参表,它的形参数目与操作符数目相同。


#include<iostream>
using namespace std;
class Date
{
public:
Date(int year = 2000,int month = 10,int day = 1)//全缺省的构造函数
:Year(year)
,Month(month)
,Day(day)
{
cout<<"Date: "<<this<<endl;//输出要打印的结果
}
Date& operator = (const Date& d);//赋值运算符重载
private:
int Year;
int Month;
int Day;
};
Date& Date ::operator =(const Date& d)
{
Year = d.Year;
Month = d.Month;
Day = d.Day;
return *this;//必须要有返回值
}
void FunTest()
{
Date t;
Date t1 = t;//赋值运算符重载
}
int main()
{
FunTest();
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值