#include<iostream>
using namespace std;
#include<string>
class Person{
string name;
bool gender;
int age;
Person* lover;
public:
Person(const char* n,bool g = true);
void growup(int years);
void show();
void marry(Person &p);
};
void Person::marry(Person &p)
{
lover = &p;
p.lover = this;
cout<<"今天"<<p.name<<"和"<<name<<"结婚啦!"<<endl;
}
Person::Person(const char* name,bool gender/* = true*/)
{
//this指向的对象称为当前对象 往里穿哪个对象 this就指向哪个对象
(*this).name = name;//Person::name
this->gender = gender;
age = 0;
lover = NULL;
}
void Person::growup(int years)
{
age +=years;
}
void Person::show()
{
cout<<"我是"<<(gender?"男的":"女的")<<name<<",今年"<<age<<"岁"<<endl;
if(NULL == lover)
cout<<"目前没有对象"<<endl;
else
cout<<"结婚了,爱人是"<<lover->name<<endl;
}
Person e()
{
string name;
bool gender;
int age;
cout<<"请输入姓名性别和年龄:";
cin>>name>>gender>>age;
Person input(name.c_str(),gender);
input.growup(age);
return input;
}
int main()
{
Person a("了了",false);
Person b("都都",true);
Person e();//这是一个函数声明
a.growup(18);//编译器转换为growup(a,18);
b.growup(20);
a.show();
b.show();
Person c("二狗");
c.growup(5);
c.show();
//e.growup(23);不能把e当对象
e().show();
b.marry(a);
a.show();
b.show();
}
using namespace std;
#include<string>
class Person{
string name;
bool gender;
int age;
Person* lover;
public:
Person(const char* n,bool g = true);
void growup(int years);
void show();
void marry(Person &p);
};
void Person::marry(Person &p)
{
lover = &p;
p.lover = this;
cout<<"今天"<<p.name<<"和"<<name<<"结婚啦!"<<endl;
}
Person::Person(const char* name,bool gender/* = true*/)
{
//this指向的对象称为当前对象 往里穿哪个对象 this就指向哪个对象
(*this).name = name;//Person::name
this->gender = gender;
age = 0;
lover = NULL;
}
void Person::growup(int years)
{
age +=years;
}
void Person::show()
{
cout<<"我是"<<(gender?"男的":"女的")<<name<<",今年"<<age<<"岁"<<endl;
if(NULL == lover)
cout<<"目前没有对象"<<endl;
else
cout<<"结婚了,爱人是"<<lover->name<<endl;
}
Person e()
{
string name;
bool gender;
int age;
cout<<"请输入姓名性别和年龄:";
cin>>name>>gender>>age;
Person input(name.c_str(),gender);
input.growup(age);
return input;
}
int main()
{
Person a("了了",false);
Person b("都都",true);
Person e();//这是一个函数声明
a.growup(18);//编译器转换为growup(a,18);
b.growup(20);
a.show();
b.show();
Person c("二狗");
c.growup(5);
c.show();
//e.growup(23);不能把e当对象
e().show();
b.marry(a);
a.show();
b.show();
}