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ù)種類
- 默認構(gòu)造函數(shù)
- 自定義構(gòu)造函數(shù)
- 拷貝構(gòu)造函數(shù)
- 賦值構(gòu)造函數(shù)
默認構(gòu)造函數(shù)
編譯器合成的默認構(gòu)造函數(shù)
沒有手動創(chuàng)建默認構(gòu)造函數(shù)的時候,編譯器會去自動合成構(gòu)造函數(shù)
- 合成默認構(gòu)造函數(shù)使用類內(nèi)初始化數(shù)據(jù)去初始化數(shù)據(jù)
- 如果沒有類內(nèi)初始化數(shù)據(jù),那么合成構(gòu)造函數(shù)內(nèi)就是空的什么都不做
默認構(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ù)那邊編譯器會自己合成默認構(gòu)造函數(shù)并且用這些數(shù)據(jù)來初始化對象
// 編譯器和合成的默認構(gòu)造函數(shù)和手動定義的默認構(gòu)造函數(shù)區(qū)別是:
// 編譯器合成的只會拿這些類內(nèi)初始化數(shù)據(jù)去初始化對象
// 手動定義的默認構(gòu)造函數(shù)如果有初始化數(shù)據(jù)的時候也可以用其他數(shù)據(jù)去覆蓋初始化數(shù)據(jù),也就是說數(shù)據(jù)初始化的值以構(gòu)造函數(shù)內(nèi)為準
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)用默認構(gòu)造函數(shù)
s1.describion();
system("pause");
return 0;
}結(jié)果:
bian 男 12
請按任意鍵繼續(xù). . .
手動定義的默認構(gòu)造函數(shù)
手動定義的默認構(gòu)造函數(shù)特點:Student::Student()
手動定義的默認構(gòu)造函數(shù)和編譯器和成的默認構(gòu)造函數(shù)沒太大區(qū)別。
唯一的區(qū)別:手動默認構(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"
// 自定義默認構(gòu)造函數(shù)
Student::Student() {
// 使用類內(nèi)初始化數(shù)據(jù)來初始化
// 其實這種就是編譯器合成默認構(gòu)造函數(shù)
this->age = age;
strcpy_s(this->name, 20, "bian");
this->sex = sex;
/*
// 使用其他數(shù)據(jù)來初始化對象,此做法會覆蓋類內(nèi)初始化的設置值
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)用默認構(gòu)造函數(shù)
s1.describion();
system("pause");
return 0;
}結(jié)果:
bian 男 12
請按任意鍵繼續(xù). . .
自定義帶參數(shù)的構(gòu)造函數(shù)
自定義帶參數(shù)的構(gòu)造函數(shù)特點:Student::Student(int age, const char name)*
帶參數(shù),可以重載。
代碼:
Student.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
Student(); // 默認構(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"
// 自定義默認構(gòu)造函數(shù)
Student::Student() {
// 使用類內(nèi)初始化數(shù)據(jù)來初始化
// 其實這種就是編譯器合成默認構(gòu)造函數(shù)
cout << __FUNCTION__ << endl;
cout << "自定義默認構(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)用自定義默認構(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
自定義默認構(gòu)造函數(shù)
bian 未知 12Student::Student
自定義帶參構(gòu)造函數(shù)
wang 男 13Student::Student
自定義帶參構(gòu)造重載函數(shù)
gao 女 14請按任意鍵繼續(xù). . .
為什么會出現(xiàn) wang 男 13,可以思考下這個男。答案在標題下方。
拷貝構(gòu)造函數(shù)
拷貝構(gòu)造函數(shù)特點: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ù)可以實現(xiàn)深拷貝
Student.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
Student(); // 默認構(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"
// 自定義默認構(gòu)造函數(shù)
Student::Student() {
// 使用類內(nèi)初始化數(shù)據(jù)來初始化
// 其實這種就是編譯器合成默認構(gòu)造函數(shù)
cout << __FUNCTION__ << endl;
cout << "自定義默認構(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ū)地址還是以前的,其實編譯器合成的拷貝構(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)用第二種時機函數(shù)形參是值傳遞而不是引用
void test1(Student other) {
cout << __FUNCTION__ << endl;
cout << endl;
}
// 拷貝構(gòu)造函數(shù)調(diào)用第三種時機返回值是值傳遞
Student test2(const Student& other) {
cout << __FUNCTION__ << endl;
cout << endl;
return other;
}
int main() {
Student s1; // 調(diào)用自定義默認構(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)用時機1、利用已有對象創(chuàng)建新對象
Student s4 = s2;
s4.describion();
Student s5(s3);
s5.describion();
// 拷貝構(gòu)造函數(shù):調(diào)用時機2、函數(shù)參數(shù)的值傳遞
test1(s5);
// 拷貝構(gòu)造函數(shù):調(diào)用時機3、函數(shù)返回值的值傳遞
test2(s5);
cout << endl;
// 拷貝構(gòu)造函數(shù):代用時機4、數(shù)組值時對象
Student s6[2] = { s1, s2 };
system("pause");
return 0;
}結(jié)果:
Student::Student
自定義默認構(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ù)特點:Student& operator=(const Student& other)
利用已有對象修改已有對象(f2 = f1;)
重載=運算符
程序:
Student.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
Student(); // 默認構(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"
// 自定義默認構(gòu)造函數(shù)
Student::Student() {
// 使用類內(nèi)初始化數(shù)據(jù)來初始化
// 其實這種就是編譯器合成默認構(gòu)造函數(shù)
cout << __FUNCTION__ << endl;
cout << "自定義默認構(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
自定義默認構(gòu)造函數(shù)
bian 未知 12Student::operator =
賦值拷貝構(gòu)造函數(shù)
gao 女 14請按任意鍵繼續(xù). . .
特別注意
1、當存在類內(nèi)初始值的時候,除了賦值拷貝構(gòu)造函數(shù)外,其他的構(gòu)造函數(shù)(默認構(gòu)造函數(shù)、自定義參數(shù)構(gòu)造函數(shù)、拷貝構(gòu)造函數(shù))在執(zhí)行構(gòu)造函數(shù)前都會先執(zhí)行下數(shù)據(jù)初始值。

2、初始化列表只存在構(gòu)造函數(shù)中(成員數(shù)據(jù)、父類對象可以使用初始化列表初始化)
到此這篇關于C++面向?qū)ο笾袠?gòu)造函數(shù)使用詳解的文章就介紹到這了,更多相關C++構(gòu)造函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vscode搭建遠程c開發(fā)環(huán)境的圖文教程
很久沒有寫C語言了,今天抽空學習下C語言知識,接下來通過本文給大家介紹Vscode搭建遠程c開發(fā)環(huán)境的詳細步驟,本文通過圖文實例代碼相結(jié)合給大家介紹的非常詳細,需要的朋友參考下吧2021-11-11
C++構(gòu)造函數(shù)的一些注意事項總結(jié)
構(gòu)造函數(shù)是創(chuàng)建類對象,并且在創(chuàng)建完成前,對類進行初始化的特殊函數(shù),下面這篇文章主要給大家介紹了關于C++構(gòu)造函數(shù)的一些注意事項,需要的朋友可以參考下2021-11-11
Qt掃盲篇之QRegularExpression正則匹配總結(jié)
QRegularExpression是Qt5.0引進的,修復了很多bug,提高了效率,使用時建議使用QRegularExpression,下面這篇文章主要給大家介紹了關于Qt掃盲篇之QRegularExpression正則匹配的相關資料,需要的朋友可以參考下2023-03-03

