C++中的构造函数+拷贝构造函数+析构函数

来源:互联网 发布:高级数据安全工程师 编辑:程序博客网 时间:2024/05/21 15:45

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

(1)不带参数的构造函数

class Time{public :    Time()        :hour(0)        , minute(0)        , sec(0){}//初始化    void set_time();    void show_time();private:    int hour;    int minute;    int sec;};void Time::set_time(){    cin >> hour >> minute >> sec;}void Time::show_time(){    cout << hour << " " << minute << " " << sec << endl;}int main(){    Time t1;    t1.set_time();    t1.show_time();    Time t2;    t2.show_time();//000    system("pause\n");    return 0;    }

若没有显式给出构造函数,则系统默认给出一个构造函数,但这个构造函数不进行任何操作,也没有对数据初始化,打印出的数据为随机值。

(2)带参数的构造函数

class Box{public:    Box(int h, int w, int len)        :hight(h)//对参数进行初始化        , width(w)        , length(len){}    int volume();private:    int hight;    int width;    int length;};int Box::volume(){    return hight * width * length;}int main(){    Box b1(2, 3, 5);//定义一个类对象并调用构造函数    cout << "b1 volume is:" << b1.volume() << endl;    system("pause\n");    return 0;}

(3)带默认参数的构造函数

#include<string.h>class Student{public:    Student(int n, char s, char nam[])        :num(n)        , sex(s)    {        strcpy(name,nam);//数据成员中含有数组时,在{}内初始化    }    void display()    {        cout << num << " " << sex << " " << name << endl;    }private:    int num;    char sex;    char name[20];};int main(){    Student stu1(10101, 'n', "zhangsan");    stu1.display();    system("pause\n");    return 0;}

一般来说,在含有默认参数的构造函数时,不应定义重载函数,容易产生二义性。
(4)构造函数的重载

class Box{public:    Box()        :high(10)        , width(10)        , length(10){}    Box(int h = 10, int w = 10, int len = 10)        :high(h)        , width(w)        , length(len){}    int volume();private:    int high;    int width;    int length;};int Box::volume(){    return (length * high * width);}int main(){    Box box1;    cout << "box1 = " << box1.volume() << endl;//1000    Box box2(2, 5);    cout << "box2 = " << box2.volume() << endl;//100    Box box3(2, 5, 3);    cout << "box2 = " << box3.volume() << endl;//30    system("pause\n");    return 0;}

2,析构函数`

class Student{public:    Student(int a = 10, int b = 10, int c = 20)        :_a(a), _b(b), _c(c){}    ~Student()//析构函数    {        cout << "Destroy" << endl;    }private:    int _a;    int _b;    int _c;};int main(){    Student stu1(10, 20, 20);    Student stu2(20, 10, 20);    system("pause\n");    return 0;}

调用了两次析构函数,这里要注意的是,先清理stu2的空间,再清理stu1,遵循“先进后出”原则。
注意:不是所有情况下都使用先进后出
如果在函数中定义静态局部对象,则只在程序第一次调用此函数时调用构造函数一次,在调用函数结束时并不释放,因此也不用调用析构函数,只在main函数结束或调用exit函数结束程序时,才调用析构函数。

3,拷贝构造函数
只有单个形参,而且该形参是对本类类型对象的引用(常用const修饰),这样的构造函数称为拷贝构造函数。拷贝构造函数是特殊的构造函数,创建对象时使用已存在的同类对象来进行初始化,由编译器自动调用。

class CDate{public:    CDate()    {}    CDate(const int year, const int month, const int day)    {        _iYear = year;        _iMonth = month;        _iDay = day;    }    CDate(const CDate &date)    {        _iYear = date._iYear;        _iMonth = date._iMonth;        _iDay = date._iDay;    }private:    int _iYear;    int _iMonth;    int _iDay;};

【特征】
1,它是构造函数的重载
2,它的参数必须使用同类型对象的引用传递。
3,如果没有显式定义,系统会自动合成一个默认的拷贝构造函数,默认拷贝构造函数会依次拷贝类的数据成员完成初始化。

【使用场景】
1.对象实例化对象

CDate d1(1990,1,1);CDate d2(d1);

2,传值方式作为函数的参数

void FunTest(const CDate date){}

3,传值方式作为函数返回值

CDate FunTest(){  CDate date;  return date; }
3 0
原创粉丝点击