C++构造函数和析构函数的学习(一)
构造函数是类中特殊的成员函数。
创建类类型的新对象的,系统会自动调用构造函数。
构造函数的调用是为了保证每个数据成员都能被正确初始化。
构造函数的作用初始化。
通常情况下,构造函数应声明为公有函数,构造它不能像其他成员函数那样被显式的调用。
构造函数被声明为私有有特殊的用途。
构造函数可以有任意类型和任意个数的参数,一个类可以有多个构造函数。
如果程序未声明构造函数,默认会生成一个空的构造函数。
不带参数的构造函数称为默认构造函数。
如果有一个构造函数,系统不再生成默认构造函数
Test.h
//Test.h#ifndef_TEST_H_#define_TEST_H_classTest{public://如果程序未声明构造函数,默认会生成一个空的构造函数Test();private:intnum_;};#endif//_TEST_H_
Test.cpp
//Test.cpp#include"Test.h"#include<iostream>usingnamespacestd;Test::Test(){num_=0;cout<<"InitializingDefault"<<endl;}
main.cpp
#include<iostream>#include"Test.h"usingnamespacestd;intmain(void){//自动调用构造函数Testt;return0;}
运行结果:
// 如果有一个构造函数,系统不再生成默认构造函数
Test.h
//Test.h#ifndef_TEST_H_#define_TEST_H_classTest{public://如果程序未声明构造函数,默认会生成一个空的构造函数Test(int num);private:intnum_;};#endif//_TEST_H_
Test.cpp
//Test.cpp#include"Test.h"#include<iostream>usingnamespacestd;Test::Test(int num){num_=num;cout<<"Initializing" << num_ <<endl;}
main.cpp
#include<iostream>#include"Test.h"usingnamespacestd;intmain(void){//自动调用构造函数Testt(10);return0;}
运行结果:
构造函数重载的实例:
Test.h
//Test.h#ifndef_TEST_H_#define_TEST_H_classTest{public://如果程序未声明构造函数,默认会生成一个空的构造函数Test();Test(int num);private:intnum_;};#endif//_TEST_H_
Test.cpp
//Test.cpp#include"Test.h"#include<iostream>usingnamespacestd;Test::Test(){num_ = 0;cout<<"Initializingdefault " <<endl;}Test::Test(int num){num_=num;cout<<"Initializing" << num_ <<endl;}
main.cpp
#include<iostream>#include"Test.h"usingnamespacestd;intmain(void){//自动调用构造函数Test t1;Testt2(10);return0;}
运行结果:
构造函数与new运算符
//构造函数与new运算符#ifndef_TEST_H_#define_TEST_H_classTest{public:////可以显式的写一个默认构造函数,这样两个函数就成了重载Test();Test(intnum);//析构函数不能重载//析构函数不能带参数,如果带参数就可以重载~Test();voidDisplay();private:intnum_;};#endif
Test.cpp
#include"Test.h"#include<iostream>usingnamespacestd;voidTest::Display(){cout<<num_<<endl;}Test::Test(){num_=0;cout<<"InitializingDefault"<<endl;}Test::Test(intnum){num_=num;cout<<"Initializing"<<num_<<endl;}Test::~Test(){cout<<"Destory"<<num_<<endl;}
main.cpp
#include<iostream>#include"Test.h"usingnamespacestd;intmain(void){Testt;t.Display();Testt2(20);t2.Display();//不仅仅分配了内存,还调用了构造函数Test*t3=newTest(20);//newoperatort3->Display();//不仅仅释放了内存,也调用了析构函数deletet3;return0;}
运行结果:
//全局对象的构造先于main函数
#ifndef_TEST_H_#define_TEST_H_classTest{public:////可以显式的写一个默认构造函数,这样两个函数就成了重载Test();Test(intnum);//析构函数不能重载//析构函数不能带参数,如果带参数就可以重载~Test();voidDisplay();private:intnum_;};#endif
Test.cpp
#include"Test.h"#include<iostream>usingnamespacestd;voidTest::Display(){cout<<num_<<endl;}Test::Test(){num_=0;cout<<"InitializingDefault"<<endl;}Test::Test(intnum){num_=num;cout<<"Initializing"<<num_<<endl;}Test::~Test(){cout<<"Destory"<<num_<<endl;}
main.cpp
#include"Test.h"usingnamespacestd;//全局对象的构造先于main函数Testt(10);intmain(void){cout<<"Enteringmain..."<<endl;cout<<"Exitingmain..."<<endl;return0;}
运行结果:
默认析构函数是一个空函数
析构函数没有参数
析构函数不能被重载
析构函数与数组
Test.h
//Test.h#ifndef_TEST_H_#define_TEST_H_classTest{public:////可以显式的写一个默认构造函数,这样两个函数就成了重载Test();Test(intnum);//析构函数不能重载//析构函数不能带参数,如果带参数就可以重载~Test();voidDisplay();private:intnum_;};#endif//_TEST_H_
Test.cpp
//Test.cpp#include<iostream>#include"Test.h"usingnamespacestd;voidTest::Display(){cout<<num_<<endl;}Test::Test(){num_=0;cout<<"InitializingDeafule"<<endl;}Test::Test(intnum){num_=num;cout<<"Initializing"<<num_<<endl;}Test::~Test(){cout<<"Destory"<<num_<<endl;}
main.cpp
//main.cpp#include<iostream>#include"Test.h"usingnamespacestd;intmain(void){//定义对象数组Testt[2]={10,20};//动态对象Test*t2=newTest(2);//传递参数2deletet2;//没有传递任何参数,创建了两个对象Test*t3=newTest[2];delete[]t3;return0;}
运行结果:
析构函数不能重载
析构函数不能带参数,如果带参数就可以重载
析构函数可以显式调用,一般很少用到,会实现一些特殊的效果:
//main.cpp#include"Test.h"intmain(void){Testt;t.~Test();//析构函数被调用了两次//析构函数很少调用,可以显式调用return0;}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。