代码如下
一下是自动生成的拷贝函数代码,将//Human(const Human &);打开是自己手动写的拷贝函数
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
#define addr_len 64
//定义一个"人类"
class Human{
public:
Human();
//Human(const Human &);
void description();
string getName();
int getAge();
int getSalary();
void setAddr(char*addr);
private:
int age = 22;
int salary = 25000;
string name = "无名氏";
char *addr="china";
};
Human::Human() {
name = "无名";
age = 22;
salary = 18000;
addr = new char[addr_len];
strcpy_s(addr, addr_len, "china");
}
//Human::Human(const Human &other) {
// name = other.name;
// age = other.age;
// salary = other.salary;
//}
string Human::getName(){
return name;
}
int Human::getAge(){
return age;
}
int Human::getSalary(){
return salary;
}
void Human::description() {
cout << "name:"<<name<< "age:" << age << "salary:" << salary<<"addr"<<addr<<endl;
}
void Human::setAddr(char*addr) {
if (!addr) {
return;
}
strcpy_s(this->addr, addr_len, addr);
}
int main(void){
Human h;
Human h2=h;
h.setAddr("美国");
h.description();
h2.description();
system("pause");
}
自动拷贝结果
手动拷贝结果