C++面向对象的程序设计4——赋值构造函数

赋值构造函数是四大构造函数中的一种,如果使用直接赋值的情况,默认也会调用赋值构造函数,但是使用默认的赋值构造函数如果在类中出现指针的情况下,会出现浅拷贝的情况,两个对象的指针指向相同的地址,为了解决这种问题,可以自定义赋值构造函数来解决。

赋值构造函数的使用场景:对象赋值的时候调用赋值构造函数。

赋值构造函数的使用方法:赋值构造函数的使用方法和自定义构造函数和拷贝构造函数不同,它是通过运算符重载的方式来实现的。

具体示例代码如下:

Human.h

#include <iostream>
#include <string>
#include <Windows.h>
#define ADDR_LEN 64

using namespace std;

class Human{
public:
	Human();                    // 默认构造函数
	Human(string, int, int);    // 自定义构造函数
	Human(const Human &);       // 拷贝构造函数
	Human &operator=(Human &);  // 赋值构造函数
	void eat();
	void sleep();
	void paly();
	void work();
	string getName();   // 获取名字
	int getAge();       // 获取年龄
	int getSalary();    // 获取薪水
	void setAddr(char *);  // 设置地址
	char *getAddr();    // 获取地址
	void desprition();  // 描述
	~Human();   // 析构函数

private:
	string name;
	int age;
	int salary;
	char *addr;
};

human.cpp

#include "Human.h"

Human::Human()
{
	cout << "调用默认构造函数" << this << endl;
	name = "无名";
	age = 18;
	salary = 10000;
}

Human::Human(string name, int age, int salary)
{
	cout << "调用自定义的构造函数" << this << endl;
	this->name = name;
	this->age = age;
	this->salary = salary;
	addr = new char[ADDR_LEN];
	strcpy_s(addr, ADDR_LEN, "China");
}

Human::Human(const Human &other)
{
	cout << "调用拷贝构造函数" << this << endl;
	name = other.name;
	age = other.age;
	salary = other.salary;
	addr = new char[ADDR_LEN];
	strcpy_s(addr, ADDR_LEN, other.addr);  // 深拷贝,另申请空间
}

Human &Human::operator=(Human &other)
{
	cout << "调用赋值构造函数" << endl;
	if(&other == this){
		return *this;
	}
	name = other.name;
	age = other.age;
	salary = other.salary;
	addr = new char[ADDR_LEN];
	strcpy_s(addr, ADDR_LEN, other.addr);  // 深拷贝,另申请空间
	return *this;   // 预防连等的情况
}

void Human::eat()
{
	cout << "我正在吃" << endl;
}

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

void Human::paly()
{
	cout << "我正在玩" << endl;
}

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

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

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

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

void Human::desprition()
{
	cout<< "name:" << name <<
		" age:" << age <<
		" salary:" << salary << 
		" addr:" << addr << endl;
}

void Human::setAddr(char *address)
{
	strcpy_s(addr, ADDR_LEN, address);
}

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

Human::~Human()
{
	cout << "调用析构函数" << this << endl;
	delete addr;
}

main.cpp

#include "Human.h"

int main(void)
{
	Human h1("张三", 25, 30000);
	Human h2(h1);
	h1.desprition();
	h2.desprition();
	cout << "------------" << endl;
	h2.setAddr("American");
	h1.desprition();
	h2.desprition();
	cout << "------------" << endl;
	Human h3;
	h3 = h1;   // 赋值的时候调用赋值构造函数
	h1.desprition();
	h3.desprition();
	cout << "------------" << endl;
	h3.setAddr("England");
	h1.desprition();
	h3.desprition();
	Human h4;
	h4 = h3;
	h4.desprition();
	system("pause");
	return 0;
}

运行结果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值