#include <iostream>
using namespace std;
class Person{
private:
char name[20];
bool gender;
int age;
public:
Person(char* name, bool gender):gender(gender), age(0)
{
if (strlen(name) > 19)
{
cout << "名字太长" << endl;
name[19] = '\0';
}
strcpy(this->name, name);
}
void set_age(int age)
{
if (age < 0)
{
cout << "你能回到过去" << endl;
}
else if (age > 200)
{
cout << "您老太高寿了啊!" << endl;
}
else
{
this->age= age;
}
}
void show()
{
cout << "我是" << (gender ? "帅哥":"美女") << name << "今年" << age << "岁了!" << endl;
}
};
int main()
{
Person p1("魏军", true);
Person p2("小V", false);
p1.set_age(20);
p2.set_age(18);
p1.show();
p2.show();
return 0;
}
构造函数的使用
最新推荐文章于 2022-03-13 16:41:22 发布