最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2022年10月26日 16:11:00   作者:劃水貓  
學習過C語言的小伙伴知道:C語言是面向過程的,關注的是過程,分析出求解問題的步驟,通過函數(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ù)種類

  • 默認構(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 未知 12

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

Student::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 未知 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ù)特點: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 女 14

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

Student::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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 基于QT5實現(xiàn)一個時鐘桌面

    基于QT5實現(xiàn)一個時鐘桌面

    這篇文章主要介紹了利用QT5實現(xiàn)的一個時鐘桌面,文中的示例代碼講解詳細,對我們學習或工作有一定的幫助,感興趣的小伙伴可以了解一下
    2022-01-01
  • C語言 全局變量和局部變量詳解及實例

    C語言 全局變量和局部變量詳解及實例

    這篇文章主要介紹了C語言 全局變量和局部變量詳解及實例的相關資料,需要的朋友可以參考下
    2017-01-01
  • C++ 淺談emplace_back及使用誤區(qū)

    C++ 淺談emplace_back及使用誤區(qū)

    這篇文章主要介紹了C++ 淺談emplace_back及使用誤區(qū),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • Qt實現(xiàn)給窗口繪制陰影的示例代碼

    Qt實現(xiàn)給窗口繪制陰影的示例代碼

    這篇文章主要為大家詳細介紹了Qt實現(xiàn)給窗口繪制陰影的方法,文中的示例代碼講解詳細,對我們學習Qt有一定的幫助,感興趣的可以了解一下
    2022-11-11
  • Vscode搭建遠程c開發(fā)環(huán)境的圖文教程

    Vscode搭建遠程c開發(fā)環(huán)境的圖文教程

    很久沒有寫C語言了,今天抽空學習下C語言知識,接下來通過本文給大家介紹Vscode搭建遠程c開發(fā)環(huán)境的詳細步驟,本文通過圖文實例代碼相結(jié)合給大家介紹的非常詳細,需要的朋友參考下吧
    2021-11-11
  • C++新特性詳細分析基于范圍的for循環(huán)

    C++新特性詳細分析基于范圍的for循環(huán)

    C++11這次的更新帶來了令很多C++程序員期待已久的for?range循環(huán),每次看到javascript,?lua里的for?range,心想要是C++能有多好,心里別提多酸了。這次C++11不負眾望,再也不用羨慕別家人的for?range了。下面看下C++11的for循環(huán)的新用法
    2022-04-04
  • OpenCV實現(xiàn)輪廓檢測與繪制

    OpenCV實現(xiàn)輪廓檢測與繪制

    這篇文章主要為大家詳細介紹了OpenCV實現(xiàn)輪廓檢測與繪制,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • C++利用Opencv實現(xiàn)多個圓形檢測

    C++利用Opencv實現(xiàn)多個圓形檢測

    霍夫圓檢測是opencv中用來檢測圓的重要算法,簡單的說,霍夫圓檢測就是對圖像中的弧線做切線,再在切點位置做切線的垂線,然后看這些垂線能交于一點的個數(shù),這個在方法中是自己設定的
    2022-08-08
  • C++構(gòu)造函數(shù)的一些注意事項總結(jié)

    C++構(gòu)造函數(shù)的一些注意事項總結(jié)

    構(gòu)造函數(shù)是創(chuàng)建類對象,并且在創(chuàng)建完成前,對類進行初始化的特殊函數(shù),下面這篇文章主要給大家介紹了關于C++構(gòu)造函數(shù)的一些注意事項,需要的朋友可以參考下
    2021-11-11
  • Qt掃盲篇之QRegularExpression正則匹配總結(jié)

    Qt掃盲篇之QRegularExpression正則匹配總結(jié)

    QRegularExpression是Qt5.0引進的,修復了很多bug,提高了效率,使用時建議使用QRegularExpression,下面這篇文章主要給大家介紹了關于Qt掃盲篇之QRegularExpression正則匹配的相關資料,需要的朋友可以參考下
    2023-03-03

最新評論

绵竹市| 宣威市| 开封市| 汨罗市| 贵德县| 渭南市| 霞浦县| 青冈县| 东乌珠穆沁旗| 临江市| 白朗县| 永安市| 静宁县| 资兴市| 阿尔山市| 北安市| 潼南县| 太湖县| 喜德县| 都安| 富阳市| 原阳县| 上饶市| 北流市| 湘乡市| 什邡市| 兴文县| 陆川县| 富宁县| 阳山县| 双流县| 陇川县| 高淳县| 吴忠市| 铁岭市| 和静县| 独山县| 洛宁县| 股票| 新津县| 东平县|