欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++面向?qū)ο笾袠?gòu)造函數(shù)使用詳解

 更新時間:2022年10月26日 16:11:00   作者:劃水貓  
學(xué)習(xí)過C語言的小伙伴知道:C語言是面向過程的,關(guān)注的是過程,分析出求解問題的步驟,通過函數(shù)調(diào)用逐步解決問題,這篇文章主要介紹了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 未知 12

Student::Student
自定義帶參構(gòu)造函數(shù)
wang 男 13

Student::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 未知 12

Student::Student
自定義帶參構(gòu)造函數(shù)
wang 男 13

Student::Student
自定義帶參構(gòu)造重載函數(shù)
gao 女 14

Student::Student
拷貝構(gòu)造函數(shù)
wang 男 13

Student::Student
拷貝構(gòu)造函數(shù)
gao 女 14

Student::Student
拷貝構(gòu)造函數(shù)
test1

test2

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 女 14

Student::Student
自定義默認(rèn)構(gòu)造函數(shù)
bian 未知 12

Student::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語言零基礎(chǔ)講解指針和數(shù)組

    C語言零基礎(chǔ)講解指針和數(shù)組

    由于數(shù)據(jù)的表現(xiàn)形式多種多樣,還有字符型和其它的數(shù)值類型,因此僅有基本數(shù)據(jù)類型是不夠的。是否可以通過基本數(shù)據(jù)類型的組合抽象構(gòu)造其它的數(shù)據(jù)類型呢?下面是小編為大家?guī)淼腃語言數(shù)組與指針詳解的知識
    2022-04-04
  • C++進(jìn)程共享數(shù)據(jù)封裝成類實(shí)例

    C++進(jìn)程共享數(shù)據(jù)封裝成類實(shí)例

    這篇文章主要介紹了C++進(jìn)程共享數(shù)據(jù)封裝成類的方法,以實(shí)例形式講述了其封裝代碼與具體用法,具有一定的實(shí)用價值,需要的朋友可以參考下
    2014-10-10
  • C++ OpenCV實(shí)現(xiàn)灰度圖蒙版GrayMask的示例代碼

    C++ OpenCV實(shí)現(xiàn)灰度圖蒙版GrayMask的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用C++和OpenCV實(shí)現(xiàn)灰度圖蒙版GrayMask,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定參考價值,需要的可以參考一下
    2022-05-05
  • C語言中常用的幾個頭文件及庫函數(shù)

    C語言中常用的幾個頭文件及庫函數(shù)

    這篇文章主要介紹了C語言中常用的幾個頭文件及庫函數(shù)的相關(guān)資料,需要的朋友可以參考下
    2017-09-09
  • C++存儲持續(xù)性生命周期原理解析

    C++存儲持續(xù)性生命周期原理解析

    這篇文章主要為大家介紹了C++存儲持續(xù)性生命周期原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • C++ 字符串去重排序?qū)嵗a

    C++ 字符串去重排序?qū)嵗a

    這篇文章主要介紹了C++ 字符串去重排序?qū)嵗a的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • C語言實(shí)現(xiàn)24點(diǎn)游戲計算器的示例代碼

    C語言實(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-08
  • C語言實(shí)現(xiàn)投票系統(tǒng)

    C語言實(shí)現(xiàn)投票系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)投票系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • C語言時間函數(shù)之strftime()詳解

    C語言時間函數(shù)之strftime()詳解

    這篇文章主要為大家詳細(xì)介紹了C語言時間函數(shù)之strftime(),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • C語言實(shí)現(xiàn)簡單通訊錄管理系統(tǒng)

    C語言實(shí)現(xiàn)簡單通訊錄管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡單通訊錄管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07

最新評論