c++中类的六个默认成员函数 : 构造函数, 拷贝构造函数, 析构函数, 赋值操作符重载,取地址操作符重载,const修饰的取地址操作符重载。
下面介绍的是类的六个默认成员函数:
一,构造函数:
1,定义:对成员变量进行初始化,有且仅在定义对象时自动执行一次的成员函数。
2,特征: a,函数名与类名相同;
b,无返回值;
c,对象实例化时,系统自动调用相应的构造函数;
d,构造函数可以重载;
e,可在类中定义,也可在类外定义(::);
f,如果类定义中未给出构造函数,则编译器自动产生一个缺省的构造函数,反之则不生成;
g,无参构造函数和全缺省函数都认为是缺省构造函数,并且缺省构造函数只能有一个。
3,成员变量的初始化: a,初始化列表(更高效);
b,函数类赋值。
4,必须放在初始化列表里的成员变量: a,常量成员变量(常量创建时必须初始化)
b,引用类型成员变量(引用创建时必须初始化)
c,没有缺省构造函数的类成员变量。
注:若缺省参数声明与定义分离,则可以在声明或定义中给默认参数。
二,拷贝构造函数
1,定义:创建对象时使用同类对象来进行初始化。
2,特征:a,本质是一个构造函数的重载;
b,参数必须使用引用传参;(使用值传参会引起无穷递归调用)
c,若未显示定义,系统会默认缺省的拷贝构造函数,缺省的拷贝构造函数会依次拷贝类成员进行初始化。
三,析构函数
1,定义:当一个对象的生命周期结束时,c++编译系统会自动调用成员函数析构函数。
2,特征: a,函数名是类名前加上字符~;
b,无参无返回值;
c,一个类有且只有一个析构函数;
d,对象生命周期结束时,c++编译系统自动调用;
e,并不是删除对象,而是做一些清理工作。
四,赋值操作符重载
1,特征: a,“operator+合法的运算符”构成函数名;
b,不改变运算符的优先级等;
2,与拷贝构造函数相比:拷贝构造函数是创建新对象,使用已有对象来初始化新对象;而赋值运算符重载是对一个已存在的对象进行拷贝赋值。
Data d2 = d1; //调用拷贝构造函数
Data d3 ;
Data d3 = d1; //调用赋值操作符重载
注:五个不能重载的运算符:.* sizeof :: ?: .
五,取地址操作符重载
class Date
{
public:
Date* operator&()
{
return this;
}
};
六,const修饰的取地址操作符重载
class Date
{
public:
const Date* operator&() const
{
return this;
}
};
下面所写代码主要是对以上所述问题的验证:
#include<iostream>
using namespace std;
class plural
{
private:
double _real;
double _imaginary;
public:
plural(double real,double imaginary) //构造函数
:_real(real),_imaginary(imaginary)
{
// cout << "plural(double real,double imaginaray)" << endl;
}
~plural() //析构函数
{
// cout << "~plural()" << endl;
}
//plural(const plural& d) //拷贝构造函数
//{
// _real =d._real;
// _imaginary = d._imaginary;
// cout << "plural(const plural& d)"<< endl;
//}
plural(const plural& d) //拷贝构造函数(初始化列表)
:_real(d._real), _imaginary(d._imaginary)
{
// cout << "plural(const plural& d)" << endl;
}
plural& operator= (const plural& d) //赋值操作符重载
{
if (this != &d)
{
this->_real =d._real;
this->_imaginary = d._imaginary;
// cout << "operator=(const plural& d)"<< endl;
}
return *this;
}
plural& operator+ (const plural& d) //加法重载
{
this->_real = _real + d._real;
this->_imaginary = _imaginary + d._imaginary;
// cout << "operator+(const plural& d)" << endl;
return *this;
}
plural& operator- (const plural& d) //减法重载
{
this->_real = _real - d._real;
this->_imaginary = _imaginary - d._imaginary;
// cout << "operator-(const plural& d)" << endl;
return *this;
}
plural operator* (const plural& d) //乘法重载
{
plural tmp(0,0);
tmp._real = (_real * d._real) - (_imaginary * d._imaginary);
tmp._imaginary = (_real * d._imaginary) + (d._real * _imaginary);
// cout << "operator*(const plural& d)" << endl;
return tmp;
}
plural operator/ (const plural& d) //除法重载
{
plural tmp(0, 0);
tmp._real = ((_real*_imaginary) + (d._real*d._imaginary)) /
(_imaginary*_imaginary + d._imaginary*d._imaginary);
tmp._imaginary = ((d._real*_imaginary) - (_real*d._imaginary)) /
(_imaginary*_imaginary + d._imaginary*d._imaginary);
// cout << "operator/(const plural& d)" << endl;
return tmp;
}
plural& operator++() //前置++
{
_real++;
_imaginary++;
return *this;
}
plural operator++(int) //后置++
{
plural tmp(*this);
_real++;
_imaginary++;
return tmp;
}
void Display() const
{
cout << _real<<"+"<<_imaginary<<"i"<< endl;
}
};
void Test()
{
plural d1(5.0,6.0); //调用构造函数
d1.Display();
plural d2 = d1; //调用拷贝构造函数
d2.Display();
plural d3(d1); //调用拷贝构造函数
d3.Display();
plural d4(1.0,2.0);
d4 = d1; //调用赋值操作符重载
d4.Display();
plural d5(5.0, 9.0),d6(1.0,3.0),d7(0.0,0.0); //加法
d7 = d5 + d6 ;
d7.Display();
plural d8(5.0, 9.0), d9(1.0, 3.0), d10(0.0, 0.0); //减法
d10 = d8 - d9;
d10.Display();
plural d11(5.0, 9.0), d12(1.0, 3.0), d13(0.0, 0.0); //乘法
d13 = d11 * d12;
d13.Display();
plural d14(5.0, 9.0), d15(1.0, 3.0), d16(0.0, 0.0); //除法
d16 = d14 / d15;
d16.Display();
plural d17(5.0, 9.0),d19(0.0,0.0); //前置++
d19 = ++d17;
d19.Display();
plural d18(5.0, 9.0),d20(0.0,0.0); //后置++
d20 = d18++;
d20.Display();
}
int main()
{
Test();
system("pause");
return 0;
}