标签:
#include<iostream> #include<string> using namespace std; class Person{ string name; bool gender; int age; Person *lover; public: Person(const char *n="无名",bool g=true); void growup(int years); void show(); }; Person::Person(const char *n,bool g/*=true*/){ name=n; gender=g; age=0; lover=NULL; } void Person::growup(int years){ age+=years; } void Person::show(){ cout<<"大家好,我是"<<(gender?"帅哥":"美女")<<name<<",今年"<<age<<"岁。"<<endl; } int main(){ Person a("芙蓉",false); Person b("永浩",true); Person c("二狗"); Person d; Person e(); a.growup(18); b.growup(20); a.show(); b.show(); c.growup(5); c.show(); d.growup(25); d.show(); e().show(); } Person e() { string name; bool gender; int age; cout<<"请输入姓名,性别和年龄"; cin>>name>>gender>>age; Person input(name.c_str(),gender);//c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同. input.growup(age); return input; }
标签:
原文地址:http://blog.csdn.net/u012503639/article/details/45037783