#include <Windows.h>
#include<iostream>
#include<string>
#define ADDR_LEN 128
using namespace std;
class Human {
public:
Human();
Human(string name, int salary, int age);
Human(const Human & );
Human& operator=( const Human &other);
int getAge() const;
string getName() const;
int getSalary() const;
void description() const;
void setAddr(const char* addr);
char* getAddr() const;
private:
int age;
string name;
int salary;
char* addr;
};
Human::Human() {
age = 20;
name = "lee";
salary = 60000;
}
Human::Human(string name, int age, int salary) {
cout << "正在调用自定义构造函数Human(string name, int age, int salary)" << endl;
this->name = name;
this->age = age;
this->salary = salary;
addr = new char[ADDR_LEN];
strcpy_s(addr, ADDR_LEN, "中国");
}
Human::Human(const Human &other) {
cout << "正在调用自定义的拷贝构造函数Human(const Human& other)" << endl;
name = other.name;
age = other.age;
salary = other.salary;
addr = new char[ADDR_LEN];
strcpy_s(this->addr, ADDR_LEN, other.addr);
}
Human& Human::operator=(const Human& other) {
if (this == &other) {
return *this;
}
name = other.name;
age = other.age;
salary = other.salary;
strcpy_s(addr, ADDR_LEN, other.addr);
return *this;
}
int Human::getAge() const{
return age;
}
string Human::getName() const{
return name;
}
int Human::getSalary() const{
return salary;
}
void Human::setAddr(const char* addr) {
if (!addr) {
return;
}
strcpy_s(this->addr, ADDR_LEN, addr);
}
char* Human::getAddr() const {
return addr;
}
void Human::description() const{
cout << "名字:" << getName() << " 年龄:" << getAge() << " 薪水:" << getSalary() <<" 居住地:"<<getAddr()<< endl;
}
void showMSG(const Human &h) {
cout << "自定义的消息函数:showMSG()" << endl;
cout << h.getName() << "的消息如下:" << endl;
h.description();
}
const Human& getBetterMan(const Human man1,const Human man2) {
if (man1.getSalary() >= man2.getSalary()) {
return man1;
}
else
return man2;
}
int main() {
Human h1("zhangsan", 20, 20000);
Human h2("lisi", 22, 25000);
getBetterMan(h1, h2);
system("pause");
return 0;
}