- IS-A. , , " , . . " ". (, , ) .
- . ( , ). , Human . - :
class Human
{
protected:
std::string name;
virtual std::string getGenderString() const = 0;
public:
virtual ~Human() {}
void getInfo(std::string hName)
{ name = hName; }
void showInfo() const
{
std::cout << "Your name is: " << name << '\n';
std::cout << "And you are a " << getGenderString() << std::endl;
}
};
class Man : public Human
{
protected:
virtual std::string getGenderString() const
{ return "MAN"; }
};
class Woman : public Human
{
protected:
virtual std::string getGenderString() const
{ return "WOMAN"; }
};
int main(){
std::string choice;
std::string tName;
Human *person = NULL;
std::cout << "Please enter you name: ";
std::cin >> tName;
std::cout << "Are you a [boy/girl]: ";
std::cin >> choice;
if (choice == "boy"){
person = new Man();
}else if(choice == "girl"){
person = new Woman();
}
person->getInfo(tName);
person->showInfo();
system("pause");
delete person;
return 0;
}
( ) . , .
, , , . , ++ 11, . () - ++.