C++ study log 34--deep copy and shallow copy


1. Deep copy and shallow copy

The core difference between deep copy and shallow copy:
Shallow copy: Copy the address of a variable, which is equivalent to pointing to the same storage space.
Deep copy: copying the contents of a variable is equivalent to creating a new one with the same contents.

#include<iostream>
#include<string>
#include"Date.h"
#include"Employee.h"
//任务1:构造Employee对象e1,拷贝构造e2
//任务2:调式模式观察e1和e2的biethday成员
//任务3:添加拷贝构造函数实现深拷贝
//任务4:调试模式观察e1和e2的birthday成员
int Employee::numberOfObjects = 0;
int main() 
{
    
    
	Employee e1{
    
     "Alex",Gender::male,{
    
    1998,5,1} };
	Employee e2{
    
     e1 };
	std::cout << e1.toString() << std::endl;
	std::cout << e2.toString() << std::endl;
	std::cin.get();
	return 0;

}

#pragma once
//任务1:定义Date类
#include<iostream>
#include<string>
class Date
{
    
    
private:
	int year = 2019, month = 1, day = 1;
public:
	int getYear() {
    
     return year; }
	int getMonth() {
    
     return month; }
	int getDay() {
    
     return day; }
	void setYear(int y) {
    
     year = y; }
	void setMonth(int m) {
    
     month = m; }
	int setDay(int d) {
    
     day = d; }
	Date() = default;
	Date(int y, int m, int d) :year{
    
     y }, month{
    
     m }, day{
    
     d }
	{
    
    
		std::cout << "Data" << toString() << std::endl;
	}
	std::string toString()
	{
    
    
		return (std::to_string(year) + "-" + std::to_string(month) + "-" + std::to_string(day));
	}//2019-1-1


};

#pragma once
//任务2:定义Gender枚举类型
//任务3:定义Employee类
#include<iostream>
#include<string>
#include"Date.h"
enum class Gender
{
    
    
	male,
	female,

};
class Employee
{
    
    
private:
	std::string name;
	Gender gender;
	Date* birthday;
	static int numberOfObjects;
public:
	void setName(std::string name) {
    
     this->name = name; }
	void setGender(Gender gender) {
    
     this->gender = gender; }
	void setBirthday(Date birthday) {
    
     *(this->birthday)= birthday; }
	std::string getName() {
    
     return name; }
	Gender getGender() {
    
     return gender; }
	Date getBirthday() {
    
     return *birthday; }
	std::string toString()
	{
    
    
		return (name + (gender == Gender::male ? std::string("male") : std::string("female")) + birthday->toString());
	}
	Employee(std::string name, Gender gender, Date birthday) :name{
    
     name }, gender{
    
     gender }
	{
    
    
		numberOfObjects++;
		this->birthday=new Date{
    
     birthday };
		std::cout << "Now there are: " << numberOfObjects << "employee" << std::endl;
	};
	Employee() :Employee("Alan", Gender::male, Date(2000, 4, 1)) {
    
    }

	Employee(const Employee& e)  //深拷贝函数  存在 则调用时为深拷贝
	{
    
    
		this->birthday = new Date{
    
     *(e.birthday) };
		numberOfObjects++;
	
	}
	~Employee()
	{
    
    
		numberOfObjects--;
		delete birthday;
		birthday = nullptr;
	}
};

insert image description here
The result is shown above.

Guess you like

Origin blog.csdn.net/taiyuezyh/article/details/124234064