构造函数是类中特殊的成员函数。
创建类类型的新对象的,系统会自动调用构造函数。
构造函数的调用是为了保证每个数据成员都能被正确初始化。
构造函数的作用初始化。
通常情况下,构造函数应声明为公有函数,构造它不能像其他成员函数那样被显式的调用。
构造函数被声明为私有有特殊的用途。
构造函数可以有任意类型和任意个数的参数,一个类可以有多个构造函数。
如果程序未声明构造函数,默认会生成一个空的构造函数。
不带参数的构造函数称为默认构造函数。
如果有一个构造函数,系统不再生成默认构造函数
Test.h
//Test.h # ifndef _TEST_H_ # define _TEST_H_ class Test { public: //如果程序未声明构造函数,默认会生成一个空的构造函数 Test(); private: int num_; }; # endif //_TEST_H_
Test.cpp
//Test.cpp # include "Test.h" # include <iostream> using namespace std; Test::Test() { num_ = 0; cout << "Initializing Default " << endl; }
main.cpp
# include <iostream> # include "Test.h" using namespace std; int main(void) { //自动调用构造函数 Test t; return 0; }
运行结果:
// 如果有一个构造函数,系统不再生成默认构造函数
Test.h
//Test.h # ifndef _TEST_H_ # define _TEST_H_ class Test { public: //如果程序未声明构造函数,默认会生成一个空的构造函数 Test(int num); private: int num_; }; # endif //_TEST_H_
Test.cpp
//Test.cpp # include "Test.h" # include <iostream> using namespace std; Test::Test(int num) { num_ = num; cout << "Initializing " << num_ << endl; }
main.cpp
# include <iostream> # include "Test.h" using namespace std; int main(void) { //自动调用构造函数 Test t(10); return 0; }
运行结果:
构造函数重载的实例:
Test.h
//Test.h # ifndef _TEST_H_ # define _TEST_H_ class Test { public: //如果程序未声明构造函数,默认会生成一个空的构造函数Test(); Test(int num); private: int num_; }; # endif //_TEST_H_
Test.cpp
//Test.cpp # include "Test.h" # include <iostream> using namespace std; Test::Test(){num_ = 0; cout << "Initializing default " << endl; } Test::Test(int num) { num_ = num; cout << "Initializing " << num_ << endl; }
main.cpp
# include <iostream> # include "Test.h" using namespace std; int main(void) { //自动调用构造函数 Test t1;Test t2(10); return 0; }
运行结果:
构造函数与new运算符
//构造函数与new运算符 # ifndef _TEST_H_ # define _TEST_H_ class Test { public: ////可以显式的写一个默认构造函数,这样两个函数就成了重载 Test(); Test(int num); //析构函数不能重载 //析构函数不能带参数,如果带参数就可以重载 ~Test(); void Display(); private: int num_; }; # endif
Test.cpp
# include "Test.h" # include <iostream> using namespace std; void Test::Display() { cout << num_ << endl; } Test::Test() { num_ = 0; cout<<"Initializing Default" << endl; } Test::Test(int num) { num_ = num; cout<<"Initializing "<<num_ << endl; } Test::~Test() { cout << "Destory " << num_ << endl; }
main.cpp
# include <iostream> # include "Test.h" using namespace std; int main(void) { Test t; t.Display(); Test t2(20); t2.Display(); //不仅仅分配了内存,还调用了构造函数 Test * t3 = new Test(20); //new operator t3->Display(); //不仅仅释放了内存,也调用了析构函数 delete t3; return 0; }
运行结果:
//全局对象的构造先于main函数
# ifndef _TEST_H_ # define _TEST_H_ class Test { public: ////可以显式的写一个默认构造函数,这样两个函数就成了重载 Test(); Test(int num); //析构函数不能重载 //析构函数不能带参数,如果带参数就可以重载 ~Test(); void Display(); private: int num_; }; # endif
Test.cpp
# include "Test.h" # include <iostream> using namespace std; void Test::Display() { cout << num_ << endl; } Test::Test() { num_ = 0; cout<<"Initializing Default" << endl; } Test::Test(int num) { num_ = num; cout<<"Initializing "<<num_ << endl; } Test::~Test() { cout << "Destory " << num_ << endl; }
main.cpp
# include "Test.h" using namespace std; //全局对象的构造先于main函数 Test t(10); int main(void) { cout << "Entering main ..." << endl; cout << "Exiting main ..." << endl; return 0; }
运行结果:
默认析构函数是一个空函数
析构函数没有参数
析构函数不能被重载
析构函数与数组
Test.h
//Test.h # ifndef _TEST_H_ # define _TEST_H_ class Test { public: ////可以显式的写一个默认构造函数,这样两个函数就成了重载 Test(); Test(int num); //析构函数不能重载 //析构函数不能带参数,如果带参数就可以重载 ~Test(); void Display(); private: int num_; }; # endif //_TEST_H_
Test.cpp
//Test.cpp # include <iostream> # include "Test.h" using namespace std; void Test::Display() { cout << num_ << endl; } Test::Test() { num_ = 0; cout<<"Initializing Deafule " << endl; } Test::Test(int num) { num_ = num; cout<<"Initializing "<<num_ << endl; } Test::~Test() { cout << "Destory " << num_ << endl; }
main.cpp
//main.cpp # include <iostream> # include "Test.h" using namespace std; int main(void) { //定义对象数组 Test t[2] = {10,20}; //动态对象 Test* t2 = new Test(2); //传递参数2 delete t2; //没有传递任何参数,创建了两个对象 Test* t3 = new Test[2]; delete[] t3; return 0; }
运行结果:
析构函数不能重载
析构函数不能带参数,如果带参数就可以重载
析构函数可以显式调用,一般很少用到,会实现一些特殊的效果:
//main.cpp # include "Test.h" int main(void) { Test t; t.~Test(); //析构函数被调用了两次 //析构函数很少调用,可以显式调用 return 0; }
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。