【自用19.2】C++构造函数

手动定义的默认构造函数

常称为“默认构造函数”

#include <iostream>
#include <Windows.h>
#include <string>

using namespace std;

// 定义一个“人类”
class Human {
public:  //公有的,对外的
	Human(); //手动定义的“默认构造函数”
	void eat(); //方法, “成员函数”
	void sleep();
	void play();
	void work();

	string getName();
	int getAge();
	int getSalary();

private:
	string name = "Unknown";
	int age = 28;
	int salary;
};

Human::Human() {
	name = "无名氏";
	age = 18;
	salary = 30000;
}

void Human::eat() {
	cout << "吃炸鸡,喝啤酒!" << endl;
}

void Human::sleep() {
	cout << "我正在睡觉!" << endl;
}

void Human::play() {
	cout << "我在唱歌! " << endl;
}

void Human::work() {
	cout << "我在工作..." << endl;
}

string Human::getName() {
	return name;
}

int Human::getAge() {
	return age;
}

int Human::getSalary() {
	return salary;
}


int main(void) {
	Human  h1;  // 使用自定义的默认构造函数
	cout << "姓名:" << h1.getName() << endl;
	cout << "年龄: " << h1.getAge() << endl;    
	cout << "薪资:" << h1.getSalary() << endl; 

	system("pause");
	return 0;
}

说明:如果某数据成员使用类内初始值,同时又在构造函数中进行了初始化,

      那么以构造函数中的初始化为准。

      相当于构造函数中的初始化,会覆盖对应的类内初始值。

自定义的重载构造函数

#include <iostream>
#include <Windows.h>
#include <string>

using namespace std;

// 定义一个“人类”
class Human {
public:  
	Human();
	Human(int age, int salary);

	void eat(); 
	void sleep();
	void play();
	void work();

	string getName();
	int getAge();
	int getSalary();

private:
	string name = "Unknown";
	int age = 28;
	int salary;
};

Human::Human() {
	name = "无名氏";
	age = 18;
	salary = 30000;
}

Human::Human(int age, int salary) {
	cout << "调用自定义的构造函数" << endl; 
	this->age = age;      //this是一个特殊的指针,指向这个对象本身
	this->salary = salary;
	name = "无名";
}


void Human::eat() {
	cout << "吃炸鸡,喝啤酒!" << endl;
}

void Human::sleep() {
	cout << "我正在睡觉!" << endl;
}

void Human::play() {
	cout << "我在唱歌! " << endl;
}

void Human::work() {
	cout << "我在工作..." << endl;
}

string Human::getName() {
	return name;
}

int Human::getAge() {
	return age;
}

int Human::getSalary() {
	return salary;
}


int main(void) {
	Human  h1(25, 35000);  // 使用自定义的默认构造函数

	cout << "姓名:" << h1.getName() << endl;
	cout << "年龄: " << h1.getAge() << endl;    
	cout << "薪资:" << h1.getSalary() << endl; 

	system("pause");
	return 0;
}

拷贝构造函数

 手动定义的拷贝构造函数

#include <iostream>
#include <Windows.h>
#include <string>

using namespace std;

// 定义一个“人类”
class Human {
public:  
	Human();
	Human(int age, int salary);
	Human(const Human&);

	void eat(); 
	void sleep();
	void play();
	void work();

	string getName();
	int getAge();
	int getSalary();

private:
	string name = "Unknown";
	int age = 28;
	int salary;
};

Human::Human() {
	name = "无名氏";
	age = 18;
	salary = 30000;
}

Human::Human(int age, int salary) {
	cout << "调用自定义的构造函数" << endl; 
	this->age = age;      //this是一个特殊的指针,指向这个对象本身
	this->salary = salary;
	name = "无名";
}

Human::Human(const Human& man) {
	cout << "调用自定义的拷贝构造函数" << endl;
	name = man.name;
	age = man.age;
	salary = man.salary;
}


void Human::eat() {
	cout << "吃炸鸡,喝啤酒!" << endl;
}

void Human::sleep() {
	cout << "我正在睡觉!" << endl;
}

void Human::play() {
	cout << "我在唱歌! " << endl;
}

void Human::work() {
	cout << "我在工作..." << endl;
}

string Human::getName() {
	return name;
}

int Human::getAge() {
	return age;
}

int Human::getSalary() {
	return salary;
}


int main(void) {
	Human  h1(25, 35000);  // 使用自定义的默认构造函数
	Human  h2(h1);  // 使用自定义的拷贝构造函数

	cout << "姓名:" << h2.getName() << endl;
	cout << "年龄: " << h2.getAge() << endl;    
	cout << "薪资:" << h2.getSalary() << endl; 

	system("pause");
	return 0;
}

 合成的拷贝构造函数

#include <iostream>
#include <Windows.h>
#include <string>
#include <string.h>

using namespace std;

// 定义一个“人类”
class Human {
public:  
	Human();
	Human(int age, int salary);
	//Human(const Human&);  //不定义拷贝构造函数,编译器会生成“合成的拷贝构造函数”

	void eat(); 
	void sleep();
	void play();
	void work();

	string getName();
	int getAge();
	int getSalary();
	void setAddr(const char *newAddr);
	const char* getAddr();

private:
	string name = "Unknown";
	int age = 28;
	int salary;
	char *addr;
};

Human::Human() {
	name = "无名氏";
	age = 18;
	salary = 30000;
}

Human::Human(int age, int salary) {
	cout << "调用自定义的构造函数" << endl; 
	this->age = age;      //this是一个特殊的指针,指向这个对象本身
	this->salary = salary;
	name = "无名";

	addr = new char[64];
	strcpy_s(addr, 64, "China");
}

void Human::eat() {
	cout << "吃炸鸡,喝啤酒!" << endl;
}

void Human::sleep() {
	cout << "我正在睡觉!" << endl;
}

void Human::play() {
	cout << "我在唱歌! " << endl;
}

void Human::work() {
	cout << "我在工作..." << endl;
}

string Human::getName() {
	return name;
}

int Human::getAge() {
	return age;
}

int Human::getSalary() {
	return salary;
}

void Human::setAddr(const char *newAddr) {
	if (!newAddr) {
		return; 
	}

	strcpy_s(addr, 64,  newAddr);
}

const char* Human::getAddr() {
	return addr;
}





int main(void) {
	Human  h1(25, 35000);  // 使用自定义的默认构造函数
	Human  h2(h1);  // 使用自定义的拷贝构造函数

	cout << "h1 addr:" << h1.getAddr() << endl;
	cout << "h2 addr:" << h2.getAddr() << endl;
	
	h1.setAddr("长沙");

	cout << "h1 addr:" << h1.getAddr() << endl;
	cout << "h2 addr:" << h2.getAddr() << endl;

	system("pause");
	return 0;
}

说明:

合成的拷贝构造函数的缺点: 使用“浅拷贝

解决方案:在自定义的拷贝构造函数中,使用‘深拷贝

#include <iostream>
#include <Windows.h>
#include <string>
#include <string.h>

using namespace std;

// 定义一个“人类”
class Human {
public:
	Human();
	Human(int age, int salary);
	Human(const Human&);  //不定义拷贝构造函数,编译器会生成“合成的拷贝构造函数”

	void eat();
	void sleep();
	void play();
	void work();

	string getName();
	int getAge();
	int getSalary();
	void setAddr(const char *newAddr);
	const char* getAddr();

private:
	string name = "Unknown";
	int age = 28;
	int salary;
	char *addr;
};

Human::Human() {
	name = "无名氏";
	age = 18;
	salary = 30000;
}

Human::Human(int age, int salary) {
	cout << "调用自定义的构造函数" << endl;
	this->age = age;      //this是一个特殊的指针,指向这个对象本身
	this->salary = salary;
	name = "无名";

	addr = new char[64];
	strcpy_s(addr, 64, "China");
}

Human::Human(const Human &man) {
	cout << "调用自定义的拷贝构造函数" << endl;
	age = man.age;      //this是一个特殊的指针,指向这个对象本身
	salary = man.salary;
	name = man.name;
	// 深度拷贝
	addr = new char[64];
	strcpy_s(addr, 64, man.addr);
}

void Human::eat() {
	cout << "吃炸鸡,喝啤酒!" << endl;
}

void Human::sleep() {
	cout << "我正在睡觉!" << endl;
}

void Human::play() {
	cout << "我在唱歌! " << endl;
}

void Human::work() {
	cout << "我在工作..." << endl;
}

string Human::getName() {
	return name;
}

int Human::getAge() {
	return age;
}

int Human::getSalary() {
	return salary;
}

void Human::setAddr(const char *newAddr) {
	if (!newAddr) {
		return;
	}

	strcpy_s(addr, 64, newAddr);
}

const char* Human::getAddr() {
	return addr;
}

int main(void) {
	Human  h1(25, 35000);  // 使用自定义的默认构造函数
	Human  h2(h1);  // 使用自定义的拷贝构造函数

	cout << "h1 addr:" << h1.getAddr() << endl;
	cout << "h2 addr:" << h2.getAddr() << endl;

	h1.setAddr("长沙");

	cout << "h1 addr:" << h1.getAddr() << endl;
	cout << "h2 addr:" << h2.getAddr() << endl;

	system("pause");
	return 0;
}

什么时候调用拷贝构造函数

1.调用函数时,实参是对象,形参不是引用类型

   如果函数的形参是引用类型,就不会调用拷贝构造函数

2.函数的返回类型是类,而且不是引用类型

3.对象数组的初始化列表中,使用对象。

#include <iostream>
#include <Windows.h>
#include <string>
#include <string.h>

using namespace std;

// 定义一个“人类”
class Human {
public:
	Human();
	Human(int age, int salary);
	Human(const Human&);  //不定义拷贝构造函数,编译器会生成“合成的拷贝构造函数”

	void eat();
	void sleep();
	void play();
	void work();

	string getName();
	int getAge();
	int getSalary();
	void setAddr(const char *newAddr);
	const char* getAddr();

private:
	string name = "Unknown";
	int age = 28;
	int salary;
	char *addr;
};

Human::Human() {
	name = "无名氏";
	age = 18;
	salary = 30000;
}

Human::Human(int age, int salary) {
	cout << "调用自定义的构造函数" << endl;
	this->age = age;      //this是一个特殊的指针,指向这个对象本身
	this->salary = salary;
	name = "无名";

	addr = new char[64];
	strcpy_s(addr, 64, "China");
}

Human::Human(const Human &man) {
	cout << "调用自定义的拷贝构造函数" << "参数:" << &man
		 << " 本对象:" << this << endl;

	age = man.age;      //this是一个特殊的指针,指向这个对象本身
	salary = man.salary;
	name = man.name;
	// 深度拷贝
	addr = new char[64];
	strcpy_s(addr, 64, man.addr);
}

void Human::eat() {
	cout << "吃炸鸡,喝啤酒!" << endl;
}

void Human::sleep() {
	cout << "我正在睡觉!" << endl;
}

void Human::play() {
	cout << "我在唱歌! " << endl;
}

void Human::work() {
	cout << "我在工作..." << endl;
}

string Human::getName() {
	return name;
}

int Human::getAge() {
	return age;
}

int Human::getSalary() {
	return salary;
}

void Human::setAddr(const char *newAddr) {
	if (!newAddr) {
		return;
	}

	strcpy_s(addr, 64, newAddr);
}

const char* Human::getAddr() {
	return addr;
}


void test(Human man) {
	cout << man.getSalary() << endl;
}

void test2(Human &man) { //不会调用拷贝构造函数,此时没有没有构造新的对象
	cout << man.getSalary() << endl;
}

Human test3(Human &man) {
	return man;
}

Human& test4(Human &man) {
	return man;
}


int main(void) {
	Human h1(25, 35000);  // 调用默认构造函数
	Human h2(h1);         // 调用拷贝构造函数
	Human h3 = h1;		  // 调用拷贝构造函数

	test(h1);		      // 调用拷贝构造函数
	test2(h1);			  // 不会调用拷贝构造函数
	test3(h1);            // 创建一个临时对象,接收test3函数的返回值,调用1次拷贝构造函数
	Human h4 = test3(h1); // 仅调用1次拷贝构造函数,返回的值直接作为h4的拷贝构造函数的参数
	test4(h1);            // 因为返回的是引用类型,所以不会创建临时对象,不会调用拷贝构造函数
	
	Human men[] = { h1, h2, h3 }; //调用3次拷贝构造函数

	system("pause");
	return 0;
}

赋值构造函数

#include <iostream>
#include <Windows.h>
#include <string>
#include <string.h>

using namespace std;

// 定义一个“人类”
class Human {
public:
	Human();
	Human(int age, int salary);
	Human(const Human&);  //不定义拷贝构造函数,编译器会生成“合成的拷贝构造函数”
	Human& operator=(const Human &);

	void eat();
	void sleep();
	void play();
	void work();

	string getName();
	int getAge();
	int getSalary();
	void setAddr(const char *newAddr);
	const char* getAddr();

private:
	string name = "Unknown";
	int age = 28;
	int salary;
	char *addr;
};

Human::Human() {
	name = "无名氏";
	age = 18;
	salary = 30000;
}

Human::Human(int age, int salary) {
	cout << "调用自定义的构造函数" << endl;
	this->age = age;      //this是一个特殊的指针,指向这个对象本身
	this->salary = salary;
	name = "无名";

	addr = new char[64];
	strcpy_s(addr, 64, "China");
}

Human::Human(const Human &man) {
	cout << "调用自定义的拷贝构造函数" << "参数:" << &man
		 << " 本对象:" << this << endl;

	age = man.age;      //this是一个特殊的指针,指向这个对象本身
	salary = man.salary;
	name = man.name;
	// 深度拷贝
	addr = new char[64];
	strcpy_s(addr, 64, man.addr);
}


Human& Human::operator=(const Human &man) {
	cout << "调用" << __FUNCTION__ << endl;
	if (this == &man) {
		return *this; //检测是不是对自己赋值:比如 h1 = h1;
	}

	// 如果有必要,需要先释放自己的资源(动态内存)
	//delete addr;
	//addr = new char[ADDR_LEN];

	// 深拷贝
	strcpy_s(addr, ADDR_LEN, other.addr);

	// 处理其他数据成员
	name = man.name;
	age = man.age;
	salary = man.salary;

	// 返回该对象本身的引用, 以便做链式连续处理,比如 a = b = c;
	return *this;
}


void Human::eat() {
	cout << "吃炸鸡,喝啤酒!" << endl;
}

void Human::sleep() {
	cout << "我正在睡觉!" << endl;
}

void Human::play() {
	cout << "我在唱歌! " << endl;
}

void Human::work() {
	cout << "我在工作..." << endl;
}

string Human::getName() {
	return name;
}

int Human::getAge() {
	return age;
}

int Human::getSalary() {
	return salary;
}

void Human::setAddr(const char *newAddr) {
	if (!newAddr) {
		return;
	}

	strcpy_s(addr, 64, newAddr);
}

const char* Human::getAddr() {
	return addr;
}


void test(Human man) {
	cout << man.getSalary() << endl;
}

void test2(Human &man) { //不会调用拷贝构造函数,此时没有没有构造新的对象
	cout << man.getSalary() << endl;
}

Human test3(Human &man) {
	return man;
}

Human& test4(Human &man) {
	return man;
}


int main(void) {
	Human h1(25, 35000);  // 调用默认构造函数

      // 特别注意,此时是创建对象h2并进行初始化,调用的是拷贝构造函数,
	// 不会调用赋值构造函数
	Human h2 = h1;  

	h2 = h1; //调用赋值构造函数
	h2 = test3(h1); //调用赋值构造函数

	Human h3 = test3(h1); //调用拷贝构造函数

	system("pause");
	return 0;
}

如果没有定义赋值构造函数,编译器会自动定义“合成的赋值构造函数”,

与其他合成的构造函数,是“浅拷贝”(又称为“位拷贝”)。

  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值