C++面向?qū)ο笾袠?gòu)造函數(shù)使用詳解
構(gòu)造函數(shù)作用
構(gòu)造函數(shù)可以在創(chuàng)建對象的時候初始化成員數(shù)據(jù),或者利用現(xiàn)有對象修改現(xiàn)有對象數(shù)據(jù)(賦值拷貝構(gòu)造函數(shù))。
構(gòu)造函數(shù)特征
自動調(diào)用,在創(chuàng)建對象的時候編譯器自動調(diào)用 - 構(gòu)造函數(shù)名和類名相同 - 構(gòu)造函數(shù)沒有返回值 - 可以有多個構(gòu)造函數(shù)(類似函數(shù)重載)
構(gòu)造函數(shù)種類
- 默認(rèn)構(gòu)造函數(shù)
- 自定義構(gòu)造函數(shù)
- 拷貝構(gòu)造函數(shù)
- 賦值構(gòu)造函數(shù)
默認(rèn)構(gòu)造函數(shù)
編譯器合成的默認(rèn)構(gòu)造函數(shù)
沒有手動創(chuàng)建默認(rèn)構(gòu)造函數(shù)的時候,編譯器會去自動合成構(gòu)造函數(shù)
- 合成默認(rèn)構(gòu)造函數(shù)使用類內(nèi)初始化數(shù)據(jù)去初始化數(shù)據(jù)
- 如果沒有類內(nèi)初始化數(shù)據(jù),那么合成構(gòu)造函數(shù)內(nèi)就是空的什么都不做
默認(rèn)構(gòu)造函數(shù)
程序:
Student.h
#pragma once #include<iostream> #include<string> using namespace std; class Student { public: void describion(); private: // 類內(nèi)初始化 // 創(chuàng)建對象的時候如果沒有構(gòu)造函數(shù)那邊編譯器會自己合成默認(rèn)構(gòu)造函數(shù)并且用這些數(shù)據(jù)來初始化對象 // 編譯器和合成的默認(rèn)構(gòu)造函數(shù)和手動定義的默認(rèn)構(gòu)造函數(shù)區(qū)別是: // 編譯器合成的只會拿這些類內(nèi)初始化數(shù)據(jù)去初始化對象 // 手動定義的默認(rèn)構(gòu)造函數(shù)如果有初始化數(shù)據(jù)的時候也可以用其他數(shù)據(jù)去覆蓋初始化數(shù)據(jù),也就是說數(shù)據(jù)初始化的值以構(gòu)造函數(shù)內(nèi)為準(zhǔn) int age = 12; char name[20] = "bian"; string sex = "男"; };
Student.cpp
#include "Student.h" void Student::describion() { cout << this->name << " " << this->sex << " " << this->age << endl; }
main.cpp
#include "Student.h" using namespace std; int main() { Student s1; // 創(chuàng)建對象調(diào)用默認(rèn)構(gòu)造函數(shù) s1.describion(); system("pause"); return 0; }
結(jié)果:
bian 男 12
請按任意鍵繼續(xù). . .
手動定義的默認(rèn)構(gòu)造函數(shù)
手動定義的默認(rèn)構(gòu)造函數(shù)特點(diǎn):Student::Student()
手動定義的默認(rèn)構(gòu)造函數(shù)和編譯器和成的默認(rèn)構(gòu)造函數(shù)沒太大區(qū)別。
唯一的區(qū)別:手動默認(rèn)構(gòu)造函數(shù)可以使用類內(nèi)初始化的值,也可以不使用類內(nèi)初始化的值。
程序:
Student.h
#pragma once #include<iostream> #include<string> using namespace std; class Student { public: Student(); void describion(); private: // 類內(nèi)初始化 int age = 12; char name[20] = "bian"; string sex = "男"; };
Student.cpp
#include "Student.h" // 自定義默認(rèn)構(gòu)造函數(shù) Student::Student() { // 使用類內(nèi)初始化數(shù)據(jù)來初始化 // 其實(shí)這種就是編譯器合成默認(rèn)構(gòu)造函數(shù) this->age = age; strcpy_s(this->name, 20, "bian"); this->sex = sex; /* // 使用其他數(shù)據(jù)來初始化對象,此做法會覆蓋類內(nèi)初始化的設(shè)置值 this->age = 14; strcpy_s(this->name, 20, "wang"); this->sex = "女"; */ } void Student::describion() { cout << this->name << " " << this->sex << " " << this->age << endl; }
main.cpp
#include "Student.h" using namespace std; int main() { Student s1; // 創(chuàng)建對象調(diào)用默認(rèn)構(gòu)造函數(shù) s1.describion(); system("pause"); return 0; }
結(jié)果:
bian 男 12
請按任意鍵繼續(xù). . .
自定義帶參數(shù)的構(gòu)造函數(shù)
自定義帶參數(shù)的構(gòu)造函數(shù)特點(diǎn):Student::Student(int age, const char name)*
帶參數(shù),可以重載。
代碼:
Student.h
#pragma once #include<iostream> #include<string> using namespace std; class Student { public: Student(); // 默認(rèn)構(gòu)造函數(shù) Student(int age, const char* name); // 自定義帶參構(gòu)造函數(shù) Student(int age, const char* name, string sex); // 自定義帶參構(gòu)造重載函數(shù) void describion(); private: // 類內(nèi)初始化 int age = 12; char name[20] = "bian"; string sex = "男"; };
Student.cpp
#include "Student.h" // 自定義默認(rèn)構(gòu)造函數(shù) Student::Student() { // 使用類內(nèi)初始化數(shù)據(jù)來初始化 // 其實(shí)這種就是編譯器合成默認(rèn)構(gòu)造函數(shù) cout << __FUNCTION__ << endl; cout << "自定義默認(rèn)構(gòu)造函數(shù)" << endl; this->age = age; strcpy_s(this->name, 20, "bian"); this->sex = "未知"; } // 自定義帶參構(gòu)造函數(shù) Student::Student(int age, const char* name) { cout << __FUNCTION__ << endl; cout << "自定義帶參構(gòu)造函數(shù)" << endl; this->age = age; strcpy_s(this->name, 20, name); } // 自定義帶參構(gòu)造重載函數(shù) Student::Student(int age, const char* name, string sex) { cout << __FUNCTION__ << endl; cout << "自定義帶參構(gòu)造重載函數(shù)" << endl; this->age = age; strcpy_s(this->name, 20, name); this->sex = sex; } void Student::describion() { cout << this->name << " " << this->sex << " " << this->age << endl; cout << endl; }
main.cpp
#include "Student.h" using namespace std; int main() { Student s1; // 調(diào)用自定義默認(rèn)構(gòu)造函數(shù) s1.describion(); Student s2(13, "wang"); // 調(diào)用自定義帶參構(gòu)造函數(shù) s2.describion(); Student s3(14, "gao", "女"); // 調(diào)用自定義帶參構(gòu)造函數(shù)(重載) s3.describion(); system("pause"); return 0; }
結(jié)果:
Student::Student
自定義默認(rèn)構(gòu)造函數(shù)
bian 未知 12Student::Student
自定義帶參構(gòu)造函數(shù)
wang 男 13Student::Student
自定義帶參構(gòu)造重載函數(shù)
gao 女 14請按任意鍵繼續(xù). . .
為什么會出現(xiàn) wang 男 13,可以思考下這個男。答案在標(biāo)題下方。
拷貝構(gòu)造函數(shù)
拷貝構(gòu)造函數(shù)特點(diǎn):Student::Student(const Student& other)
深淺拷貝是針對在堆區(qū)開辟內(nèi)存的數(shù)據(jù),深拷貝重新開辟內(nèi)存存數(shù)據(jù),淺拷貝直接把原來的堆區(qū)拿過來用
合成拷貝構(gòu)造函數(shù)
合成拷貝構(gòu)造函數(shù)是編譯器自動合成的屬于淺拷貝
自定義拷貝構(gòu)造函數(shù)
自定義拷貝構(gòu)造函數(shù)可以實(shí)現(xiàn)深拷貝
Student.h
#pragma once #include<iostream> #include<string> using namespace std; class Student { public: Student(); // 默認(rèn)構(gòu)造函數(shù) Student(int age, const char* name); // 自定義帶參構(gòu)造函數(shù) Student(int age, const char* name, string sex); // 自定義帶參構(gòu)造重載函數(shù) Student(const Student& other); // 拷貝構(gòu)造函數(shù) void describion(); private: // 類內(nèi)初始化 int age = 12; char* name; string sex = "男"; };
Student.cpp
#include "Student.h" // 自定義默認(rèn)構(gòu)造函數(shù) Student::Student() { // 使用類內(nèi)初始化數(shù)據(jù)來初始化 // 其實(shí)這種就是編譯器合成默認(rèn)構(gòu)造函數(shù) cout << __FUNCTION__ << endl; cout << "自定義默認(rèn)構(gòu)造函數(shù)" << endl; this->age = age; this->name = new char[20]; strcpy_s(this->name, 20, "bian"); this->sex = "未知"; } // 自定義帶參構(gòu)造函數(shù) Student::Student(int age, const char* name) { cout << __FUNCTION__ << endl; cout << "自定義帶參構(gòu)造函數(shù)" << endl; this->age = age; this->name = new char[20]; strcpy_s(this->name, 20, name); } // 自定義帶參構(gòu)造重載函數(shù) Student::Student(int age, const char* name, string sex) { cout << __FUNCTION__ << endl; cout << "自定義帶參構(gòu)造重載函數(shù)" << endl; this->age = age; this->name = new char[20]; strcpy_s(this->name, 20, name); this->sex = sex; } // 拷貝構(gòu)造函數(shù) Student::Student(const Student& other) { cout << __FUNCTION__ << endl; cout << "拷貝構(gòu)造函數(shù)" << endl; // 淺拷貝,堆區(qū)地址還是以前的,其實(shí)編譯器合成的拷貝構(gòu)造函數(shù)就是這個 this->age = other.age; this->name = other.name; this->sex = other.sex; // 深拷貝部分主要是堆區(qū)空間重新開辟 this->age = other.age; // 重新開辟堆區(qū) this->name = new char[20]; strcpy_s(this->name, 20, other.name); this->sex = other.sex; } void Student::describion() { cout << this->name << " " << this->sex << " " << this->age << endl; cout << endl; }
main.cpp
#include "Student.h" using namespace std; // 拷貝構(gòu)造函數(shù)調(diào)用第二種時機(jī)函數(shù)形參是值傳遞而不是引用 void test1(Student other) { cout << __FUNCTION__ << endl; cout << endl; } // 拷貝構(gòu)造函數(shù)調(diào)用第三種時機(jī)返回值是值傳遞 Student test2(const Student& other) { cout << __FUNCTION__ << endl; cout << endl; return other; } int main() { Student s1; // 調(diào)用自定義默認(rèn)構(gòu)造函數(shù) s1.describion(); Student s2(13, "wang"); // 調(diào)用自定義帶參構(gòu)造函數(shù) s2.describion(); Student s3(14, "gao", "女"); // 調(diào)用自定義帶參構(gòu)造函數(shù)(重載) s3.describion(); // 拷貝構(gòu)造函數(shù):調(diào)用時機(jī)1、利用已有對象創(chuàng)建新對象 Student s4 = s2; s4.describion(); Student s5(s3); s5.describion(); // 拷貝構(gòu)造函數(shù):調(diào)用時機(jī)2、函數(shù)參數(shù)的值傳遞 test1(s5); // 拷貝構(gòu)造函數(shù):調(diào)用時機(jī)3、函數(shù)返回值的值傳遞 test2(s5); cout << endl; // 拷貝構(gòu)造函數(shù):代用時機(jī)4、數(shù)組值時對象 Student s6[2] = { s1, s2 }; system("pause"); return 0; }
結(jié)果:
Student::Student
自定義默認(rèn)構(gòu)造函數(shù)
bian 未知 12Student::Student
自定義帶參構(gòu)造函數(shù)
wang 男 13Student::Student
自定義帶參構(gòu)造重載函數(shù)
gao 女 14Student::Student
拷貝構(gòu)造函數(shù)
wang 男 13Student::Student
拷貝構(gòu)造函數(shù)
gao 女 14Student::Student
拷貝構(gòu)造函數(shù)
test1test2
Student::Student
拷貝構(gòu)造函數(shù)Student::Student
拷貝構(gòu)造函數(shù)
Student::Student
拷貝構(gòu)造函數(shù)
請按任意鍵繼續(xù). . .
結(jié)果解析:
拷貝構(gòu)造函數(shù)的調(diào)用時間
程序演示已經(jīng)在自定義拷貝構(gòu)造函數(shù)中寫了。
- 使用已有對象創(chuàng)建新對象
- 函數(shù)參數(shù)是對象值傳遞
- 函數(shù)返回值是對象值傳遞
- 數(shù)組成員是對象
賦值構(gòu)造函數(shù)(operator=)
賦值構(gòu)造函數(shù)特點(diǎn):Student& operator=(const Student& other)
利用已有對象修改已有對象(f2 = f1;)
重載=運(yùn)算符
程序:
Student.h
#pragma once #include<iostream> #include<string> using namespace std; class Student { public: Student(); // 默認(rèn)構(gòu)造函數(shù) Student(int age, const char* name); // 自定義帶參構(gòu)造函數(shù) Student(int age, const char* name, string sex); // 自定義帶參構(gòu)造重載函數(shù) Student(const Student& other); // 拷貝構(gòu)造函數(shù) Student& operator=(const Student& other); // 賦值拷貝構(gòu)造函數(shù) void describion(); private: // 類內(nèi)初始化 int age = 12; char* name; string sex = "男"; };
Student.cpp
#include "Student.h" // 自定義默認(rèn)構(gòu)造函數(shù) Student::Student() { // 使用類內(nèi)初始化數(shù)據(jù)來初始化 // 其實(shí)這種就是編譯器合成默認(rèn)構(gòu)造函數(shù) cout << __FUNCTION__ << endl; cout << "自定義默認(rèn)構(gòu)造函數(shù)" << endl; this->age = age; this->name = new char[20]; strcpy_s(this->name, 20, "bian"); this->sex = "未知"; } // 自定義帶參構(gòu)造函數(shù) Student::Student(int age, const char* name) { cout << __FUNCTION__ << endl; cout << "自定義帶參構(gòu)造函數(shù)" << endl; this->age = age; this->name = new char[20]; strcpy_s(this->name, 20, name); } // 自定義帶參構(gòu)造重載函數(shù) Student::Student(int age, const char* name, string sex) { cout << __FUNCTION__ << endl; cout << "自定義帶參構(gòu)造重載函數(shù)" << endl; this->age = age; this->name = new char[20]; strcpy_s(this->name, 20, name); this->sex = sex; } // 拷貝構(gòu)造函數(shù) Student::Student(const Student& other) { cout << __FUNCTION__ << endl; cout << "拷貝構(gòu)造函數(shù)" << endl; // 淺拷貝,堆區(qū)地址還是以前的 //this->age = other.age; //this->name = other.name; //this->sex = other.sex; // 深拷貝部分主要是堆區(qū)空間重新開辟 this->age = other.age; // 重新開辟堆區(qū) this->name = new char[20]; strcpy_s(this->name, 20, other.name); this->sex = other.sex; } // 賦值拷貝構(gòu)造函數(shù) Student& Student::operator=(const Student& other) { cout << __FUNCTION__ << endl; cout << "賦值拷貝構(gòu)造函數(shù)" << endl; if (this == &other) { return *this; // 防止出現(xiàn)f1=f1 } // 淺拷貝,堆區(qū)地址還是以前的 //this->age = other.age; //this->name = other.name; //this->sex = other.sex; // 深拷貝部分主要是堆區(qū)空間重新開辟 this->age = other.age; // 重新開辟堆區(qū) this->name = new char[20]; strcpy_s(this->name, 20, other.name); this->sex = other.sex; return *this; } void Student::describion() { cout << this->name << " " << this->sex << " " << this->age << endl; cout << endl; }
main.cpp
#include "Student.h" using namespace std; int main() { Student s1(14, "gao", "女"); // 調(diào)用自定義帶參構(gòu)造函數(shù)(重載) s1.describion(); // 調(diào)用賦值拷貝構(gòu)造函數(shù) Student s2; s2.describion(); s2 = s1; s2.describion(); system("pause"); return 0; }
結(jié)果:
Student::Student
自定義帶參構(gòu)造重載函數(shù)
gao 女 14Student::Student
自定義默認(rèn)構(gòu)造函數(shù)
bian 未知 12Student::operator =
賦值拷貝構(gòu)造函數(shù)
gao 女 14請按任意鍵繼續(xù). . .
特別注意
1、當(dāng)存在類內(nèi)初始值的時候,除了賦值拷貝構(gòu)造函數(shù)外,其他的構(gòu)造函數(shù)(默認(rèn)構(gòu)造函數(shù)、自定義參數(shù)構(gòu)造函數(shù)、拷貝構(gòu)造函數(shù))在執(zhí)行構(gòu)造函數(shù)前都會先執(zhí)行下數(shù)據(jù)初始值。
2、初始化列表只存在構(gòu)造函數(shù)中(成員數(shù)據(jù)、父類對象可以使用初始化列表初始化)
到此這篇關(guān)于C++面向?qū)ο笾袠?gòu)造函數(shù)使用詳解的文章就介紹到這了,更多相關(guān)C++構(gòu)造函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++進(jìn)程共享數(shù)據(jù)封裝成類實(shí)例
這篇文章主要介紹了C++進(jìn)程共享數(shù)據(jù)封裝成類的方法,以實(shí)例形式講述了其封裝代碼與具體用法,具有一定的實(shí)用價值,需要的朋友可以參考下2014-10-10C++ OpenCV實(shí)現(xiàn)灰度圖蒙版GrayMask的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C++和OpenCV實(shí)現(xiàn)灰度圖蒙版GrayMask,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定參考價值,需要的可以參考一下2022-05-05C語言實(shí)現(xiàn)24點(diǎn)游戲計算器的示例代碼
24點(diǎn)是一種益智游戲,24點(diǎn)是把4個整數(shù)(一般是正整數(shù))通過加減乘除以及括號運(yùn)算,使最后的計算結(jié)果是24的一個數(shù)學(xué)游戲,24點(diǎn)可以考驗(yàn)人的智力和數(shù)學(xué)敏感性,它能在游戲中提高人們的心算能力。本文將用C語言實(shí)現(xiàn)這一游戲,感興趣的可以了解一下2022-08-08C語言實(shí)現(xiàn)簡單通訊錄管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡單通訊錄管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07