#include<iostream>
#include<Windows.h>
#include<string>
using namespace std;
#define ADDR_LEN 64
class Cat
{
public:
Cat();
Cat(const Cat &other);
string getName();
int getAge();
void setAddr(char*addr);
void description();
private:
string name;
int age;
char*addr;
};
string Cat::getName()
{
return name;
}
int Cat::getAge()
{
return age;
}
void Cat::setAddr(char*addr)
{
if (!addr)
{
return;
}
strcpy_s(this->addr, ADDR_LEN, addr);
}
void Cat::description()
{
cout << "姓名:" << name << " 年龄: " << age << " 地址: " << addr << endl;
}
Cat::Cat()
{
cout << "调用手动默认构造函数" << endl;
name = "小狸花";
age = 14;
addr = new char[ADDR_LEN];
strcpy_s(addr, ADDR_LEN, "小狸花");
}
Cat::Cat(const Cat &other)
{
cout << "调用拷贝构造函数" << endl;
name = other.name;
age = other.age;
addr = new char[ADDR_LEN];
strcpy_s(addr, ADDR_LEN, other.addr);
}
int main()
{
Cat h1;
Cat h2 = h1;
h1.description();
h2.description();
cout << "---------------------------修改地址后" << endl;
h1.setAddr((char*)"小白");
h1.description();
h2.description();
system("pause");
return 0;
}