C++面向?qū)ο笾袠?gòu)造函數(shù)使用詳解
構(gòu)造函數(shù)作用
構(gòu)造函數(shù)可以在創(chuàng)建對(duì)象的時(shí)候初始化成員數(shù)據(jù),或者利用現(xiàn)有對(duì)象修改現(xiàn)有對(duì)象數(shù)據(jù)(賦值拷貝構(gòu)造函數(shù))。
構(gòu)造函數(shù)特征
自動(dòng)調(diào)用,在創(chuàng)建對(duì)象的時(shí)候編譯器自動(dòng)調(diào)用 - 構(gòu)造函數(shù)名和類(lèi)名相同 - 構(gòu)造函數(shù)沒(méi)有返回值 - 可以有多個(gè)構(gòu)造函數(shù)(類(lèi)似函數(shù)重載)
構(gòu)造函數(shù)種類(lèi)
- 默認(rèn)構(gòu)造函數(shù)
- 自定義構(gòu)造函數(shù)
- 拷貝構(gòu)造函數(shù)
- 賦值構(gòu)造函數(shù)
默認(rèn)構(gòu)造函數(shù)
編譯器合成的默認(rèn)構(gòu)造函數(shù)
沒(méi)有手動(dòng)創(chuàng)建默認(rèn)構(gòu)造函數(shù)的時(shí)候,編譯器會(huì)去自動(dòng)合成構(gòu)造函數(shù)
- 合成默認(rèn)構(gòu)造函數(shù)使用類(lèi)內(nèi)初始化數(shù)據(jù)去初始化數(shù)據(jù)
- 如果沒(méi)有類(lèi)內(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:
// 類(lèi)內(nèi)初始化
// 創(chuàng)建對(duì)象的時(shí)候如果沒(méi)有構(gòu)造函數(shù)那邊編譯器會(huì)自己合成默認(rèn)構(gòu)造函數(shù)并且用這些數(shù)據(jù)來(lái)初始化對(duì)象
// 編譯器和合成的默認(rèn)構(gòu)造函數(shù)和手動(dòng)定義的默認(rèn)構(gòu)造函數(shù)區(qū)別是:
// 編譯器合成的只會(huì)拿這些類(lèi)內(nèi)初始化數(shù)據(jù)去初始化對(duì)象
// 手動(dòng)定義的默認(rèn)構(gòu)造函數(shù)如果有初始化數(shù)據(jù)的時(shí)候也可以用其他數(shù)據(jù)去覆蓋初始化數(shù)據(jù),也就是說(shuō)數(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)建對(duì)象調(diào)用默認(rèn)構(gòu)造函數(shù)
s1.describion();
system("pause");
return 0;
}結(jié)果:
bian 男 12
請(qǐng)按任意鍵繼續(xù). . .
手動(dòng)定義的默認(rèn)構(gòu)造函數(shù)
手動(dòng)定義的默認(rèn)構(gòu)造函數(shù)特點(diǎn):Student::Student()
手動(dòng)定義的默認(rèn)構(gòu)造函數(shù)和編譯器和成的默認(rèn)構(gòu)造函數(shù)沒(méi)太大區(qū)別。
唯一的區(qū)別:手動(dòng)默認(rèn)構(gòu)造函數(shù)可以使用類(lèi)內(nèi)初始化的值,也可以不使用類(lèi)內(nèi)初始化的值。
程序:
Student.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
Student();
void describion();
private:
// 類(lèi)內(nèi)初始化
int age = 12;
char name[20] = "bian";
string sex = "男";
};Student.cpp
#include "Student.h"
// 自定義默認(rèn)構(gòu)造函數(shù)
Student::Student() {
// 使用類(lèi)內(nèi)初始化數(shù)據(jù)來(lái)初始化
// 其實(shí)這種就是編譯器合成默認(rèn)構(gòu)造函數(shù)
this->age = age;
strcpy_s(this->name, 20, "bian");
this->sex = sex;
/*
// 使用其他數(shù)據(jù)來(lái)初始化對(duì)象,此做法會(huì)覆蓋類(lèi)內(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)建對(duì)象調(diào)用默認(rèn)構(gòu)造函數(shù)
s1.describion();
system("pause");
return 0;
}結(jié)果:
bian 男 12
請(qǐng)按任意鍵繼續(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:
// 類(lèi)內(nèi)初始化
int age = 12;
char name[20] = "bian";
string sex = "男";
};Student.cpp
#include "Student.h"
// 自定義默認(rèn)構(gòu)造函數(shù)
Student::Student() {
// 使用類(lèi)內(nèi)初始化數(shù)據(jù)來(lái)初始化
// 其實(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請(qǐng)按任意鍵繼續(xù). . .
為什么會(huì)出現(xiàn) wang 男 13,可以思考下這個(gè)男。答案在標(biāo)題下方。
拷貝構(gòu)造函數(shù)
拷貝構(gòu)造函數(shù)特點(diǎn):Student::Student(const Student& other)
深淺拷貝是針對(duì)在堆區(qū)開(kāi)辟內(nèi)存的數(shù)據(jù),深拷貝重新開(kāi)辟內(nèi)存存數(shù)據(jù),淺拷貝直接把原來(lái)的堆區(qū)拿過(guò)來(lái)用
合成拷貝構(gòu)造函數(shù)
合成拷貝構(gòu)造函數(shù)是編譯器自動(dòng)合成的屬于淺拷貝
自定義拷貝構(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:
// 類(lèi)內(nèi)初始化
int age = 12;
char* name;
string sex = "男";
};Student.cpp
#include "Student.h"
// 自定義默認(rèn)構(gòu)造函數(shù)
Student::Student() {
// 使用類(lèi)內(nèi)初始化數(shù)據(jù)來(lái)初始化
// 其實(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ù)就是這個(gè)
this->age = other.age;
this->name = other.name;
this->sex = other.sex;
// 深拷貝部分主要是堆區(qū)空間重新開(kāi)辟
this->age = other.age;
// 重新開(kāi)辟堆區(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í)機(jī)函數(shù)形參是值傳遞而不是引用
void test1(Student other) {
cout << __FUNCTION__ << endl;
cout << endl;
}
// 拷貝構(gòu)造函數(shù)調(diào)用第三種時(shí)機(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)用時(shí)機(jī)1、利用已有對(duì)象創(chuàng)建新對(duì)象
Student s4 = s2;
s4.describion();
Student s5(s3);
s5.describion();
// 拷貝構(gòu)造函數(shù):調(diào)用時(shí)機(jī)2、函數(shù)參數(shù)的值傳遞
test1(s5);
// 拷貝構(gòu)造函數(shù):調(diào)用時(shí)機(jī)3、函數(shù)返回值的值傳遞
test2(s5);
cout << endl;
// 拷貝構(gòu)造函數(shù):代用時(shí)機(jī)4、數(shù)組值時(shí)對(duì)象
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ù)
請(qǐng)按任意鍵繼續(xù). . .
結(jié)果解析:

拷貝構(gòu)造函數(shù)的調(diào)用時(shí)間
程序演示已經(jīng)在自定義拷貝構(gòu)造函數(shù)中寫(xiě)了。
- 使用已有對(duì)象創(chuàng)建新對(duì)象
- 函數(shù)參數(shù)是對(duì)象值傳遞
- 函數(shù)返回值是對(duì)象值傳遞
- 數(shù)組成員是對(duì)象
賦值構(gòu)造函數(shù)(operator=)
賦值構(gòu)造函數(shù)特點(diǎn):Student& operator=(const Student& other)
利用已有對(duì)象修改已有對(duì)象(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:
// 類(lèi)內(nèi)初始化
int age = 12;
char* name;
string sex = "男";
};Student.cpp
#include "Student.h"
// 自定義默認(rèn)構(gòu)造函數(shù)
Student::Student() {
// 使用類(lèi)內(nèi)初始化數(shù)據(jù)來(lái)初始化
// 其實(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ū)空間重新開(kāi)辟
this->age = other.age;
// 重新開(kāi)辟堆區(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ū)空間重新開(kāi)辟
this->age = other.age;
// 重新開(kāi)辟堆區(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請(qǐng)按任意鍵繼續(xù). . .
特別注意
1、當(dāng)存在類(lèi)內(nèi)初始值的時(shí)候,除了賦值拷貝構(gòu)造函數(shù)外,其他的構(gòu)造函數(shù)(默認(rèn)構(gòu)造函數(shù)、自定義參數(shù)構(gòu)造函數(shù)、拷貝構(gòu)造函數(shù))在執(zhí)行構(gòu)造函數(shù)前都會(huì)先執(zhí)行下數(shù)據(jù)初始值。

2、初始化列表只存在構(gòu)造函數(shù)中(成員數(shù)據(jù)、父類(lèi)對(duì)象可以使用初始化列表初始化)
到此這篇關(guān)于C++面向?qū)ο笾袠?gòu)造函數(shù)使用詳解的文章就介紹到這了,更多相關(guān)C++構(gòu)造函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于QT5實(shí)現(xiàn)一個(gè)時(shí)鐘桌面
這篇文章主要介紹了利用QT5實(shí)現(xiàn)的一個(gè)時(shí)鐘桌面,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定的幫助,感興趣的小伙伴可以了解一下2022-01-01
Vscode搭建遠(yuǎn)程c開(kāi)發(fā)環(huán)境的圖文教程
很久沒(méi)有寫(xiě)C語(yǔ)言了,今天抽空學(xué)習(xí)下C語(yǔ)言知識(shí),接下來(lái)通過(guò)本文給大家介紹Vscode搭建遠(yuǎn)程c開(kāi)發(fā)環(huán)境的詳細(xì)步驟,本文通過(guò)圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-11-11
C++新特性詳細(xì)分析基于范圍的for循環(huán)
C++11這次的更新帶來(lái)了令很多C++程序員期待已久的for?range循環(huán),每次看到j(luò)avascript,?lua里的for?range,心想要是C++能有多好,心里別提多酸了。這次C++11不負(fù)眾望,再也不用羨慕別家人的for?range了。下面看下C++11的for循環(huán)的新用法2022-04-04
OpenCV實(shí)現(xiàn)輪廓檢測(cè)與繪制
這篇文章主要為大家詳細(xì)介紹了OpenCV實(shí)現(xiàn)輪廓檢測(cè)與繪制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
C++利用Opencv實(shí)現(xiàn)多個(gè)圓形檢測(cè)
霍夫圓檢測(cè)是opencv中用來(lái)檢測(cè)圓的重要算法,簡(jiǎn)單的說(shuō),霍夫圓檢測(cè)就是對(duì)圖像中的弧線(xiàn)做切線(xiàn),再在切點(diǎn)位置做切線(xiàn)的垂線(xiàn),然后看這些垂線(xiàn)能交于一點(diǎn)的個(gè)數(shù),這個(gè)在方法中是自己設(shè)定的2022-08-08
C++構(gòu)造函數(shù)的一些注意事項(xiàng)總結(jié)
構(gòu)造函數(shù)是創(chuàng)建類(lèi)對(duì)象,并且在創(chuàng)建完成前,對(duì)類(lèi)進(jìn)行初始化的特殊函數(shù),下面這篇文章主要給大家介紹了關(guān)于C++構(gòu)造函數(shù)的一些注意事項(xiàng),需要的朋友可以參考下2021-11-11
Qt掃盲篇之QRegularExpression正則匹配總結(jié)
QRegularExpression是Qt5.0引進(jìn)的,修復(fù)了很多bug,提高了效率,使用時(shí)建議使用QRegularExpression,下面這篇文章主要給大家介紹了關(guān)于Qt掃盲篇之QRegularExpression正則匹配的相關(guān)資料,需要的朋友可以參考下2023-03-03

