【自用19.3】C++构造函数复盘

定义一个“人”类

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

using namespace std;

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

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

private:
	string name;
	int age;
	int 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  zhangshan;

	system("pause");
}

对象的使用

.的使用

使用结果

代码

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

using namespace std;

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

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

private:
	string name;
	int age;
	int 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; // 通过自定义的特殊数据类型“Human”类, 来创建一个“对象”

	// 合法使用
	h1.eat();
	h1.play();
	h1.sleep();

	// 非法使用
	//cout << "年龄" << h1.age << endl;  //直接访问私有成员,将无法通过编译

	//正确使用
	cout << "年龄" << h1.getAge() << endl; //暴露问题,年龄值是一个很大的负数

	system("pause");
}

说明:

.符号可以调用public中的成员函数,但是不能访问private中的信息

但是我们可以看到。获取到的年龄是一个负数,这是为什么呢?

->的使用

使用结果

代码如下

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

using namespace std;

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

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

private:
	string name;
	int age;
	int 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; // 通过自定义的特殊数据类型“Human”类, 来创建一个“对象”
	Human* p;

	p = &h1;

	// 合法使用
	p->eat();
	p->play();
	p->sleep();

	// 非法使用
	//cout << "年龄" << p->age << endl;  //直接访问私有成员,将无法通过编译

	//正确使用
	cout << "年龄" << p->getAge() << endl; //暴露问题,年龄值是一个很大的负数

	system("pause");
}

 在主函数中

首先自定义一个Human类类型的对象h1

然后定义一个Human类类型的指针

将h1的地址赋值给这个指针,相当于这个指针指向了h1这个对象

注意:Human是类名,Human是一个类,类中定义的叫做对象,比如h1。所以h1叫做Human类型的对象。

然后使用指针和->符号调用h1的成员函数。

最后调用的结果和第一种使用方式的结果是非常类似的。

但是我们可以知道,不同的对象,参数肯定也是不一样,那我们怎么去给不同的对象附上不同的参数呢?

构造函数

作用:

创建新的对象时,自动调用构造函数来进行初始化工作,这个初始化工作的内容是对对象内部的数据成员进行初始化。

特点:

  1. 自动调用(在创建新对象时,自动调用)
  2. 构造函数的函数名,和类名相同
  3. 构造函数没有返回类型
  4. 可以有多个构造函数(即函数重载形式)

种类:

默认构造函数

自定义的构造函数

拷贝构造函数

赋值构造函数

默认构造函数

没有参数的构造函数,称为默认构造函数

分类

合成的默认构造函数

手动定义的默认构造函数

合成的默认构造函数

 使用结果

代码

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

using namespace std;

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

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

private:
	string name;
	int age = 18;
	int 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;  // 使用合成的默认初始化构造函数
	cout << "年龄: " << h1.getAge() << endl;     //使用了类内初始值
	cout << "薪资:" << h1.getSalary() << endl;  //没有类内初始值

	system("pause");
	return 0;
}

 在上面这段代码中,在定义人类的使用,在private的年龄变量中初始化为了18。

后期没有手动编写构造函数,所以合成的默认构造函数使用了原本初始化的值,没有初始化的变量被赋给了一个很大的负数。

注意:

只要手动定义了任何一个构造函数,编译器就不会生成“合成的默认构造函数”

一般情况下,都应该定义自己的构造函数,不要使用“合成的默认构造函数”

【仅当数据成员全部使用了“类内初始值”,才宜使用“合成的默认构造函数”】

手动定义的默认构造函数

 使用结果

代码

#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;
	int age = 18;
	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;
}

 手动定义的默认构造函数简称默认构造函数

手动定义默认构造函数的话,需要在定义类的时候在public中声明这个默认构造函数,否则会出现问题。

根据代码的运行结果我们可以知道,如果某数据成员使用类内初始值,同时又在构造函数中进行了初始化,那么以构造函数中的初始化为准。相当于构造函数中的初始化,会覆盖对应的类内初始值。

 自定义的重载构造函数

使用结果

代码

#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;
	int age = 18;
	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;
}

 自定义的重载构造函数需要在定义类时,在public中进行函数重载的声明。

重载的函数与自定义的构造函数不同,重载的构造函数可以有传入的信息值,自定义的默认构造函数是没有参数的,因为默认构造的特点是没有参数。

在这里编写的自定义构造函数传入两个参数,年龄和工资,根据运行结果可以知道,当自定义的构造函数中信息的值与默认构造函数或者定义中的信息冲突的时候,要以自定义构造函数中传入的值为准。

拷贝构造函数

手动定义的拷贝构造函数 

使用结果

 

代码

#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;
	int age = 18;
	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 << "姓名:" << h1.getName() << endl;
	cout << "年龄: " << h1.getAge() << endl;
	cout << "薪资:" << h1.getSalary() << endl;

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


	system("pause");
	return 0;
}

 手动定义的拷贝函数函数的参数是一个对象,并且需要在类定义中的public中进行声明

使用拷贝构造函数之后,创建的新的对象的信息与所拷贝的对象的信息相同

合成的拷贝构造函数

使用结果

 

代码

#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;
	int age = 18;
	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;
} 

 不定义拷贝构造函数的话,编译器会自动生成合成拷贝构造函数

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

根据运行结果来看,由于h2拷贝了h1的信息。所以修改h1的信息的时候,h2的信息也会跟着改变。

但是需要注意的是两个对象的指针却是不一样的

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

使用结果

代码

#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;
	int age = 18;
	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;

	cout << "h1" << &h1 << endl;
	cout << "h2" << &h2 << endl;


	system("pause");
	return 0;
}

 “深拷贝”中,自定义的拷贝构造函数直接将所拷贝的对象h1的信息赋给新定义的对象h2,这样操作之后,被拷贝的对象h1的信息修改的时候,拷贝得到的对象h2的信息不会被修改。

那么

什么时候使用拷贝构造函数呢?

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;
	int age = 18;
	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;
}

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;
	int age = 18;
	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);
}

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, 64, man.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;
}

 综合来看,如果是创建对象并初始化,则调用拷贝构造函数,如果是直接赋值,则使用赋值构造函数。

以上构造函数的复盘结束~

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值