#include <iostream>
using namespace std;
class Animal
{
private:
string name;
public:
Animal()
{}
Animal(string name):name(name)
{
}
virtual void perform()
{
cout << name << endl;
}
};
class Person:public Animal
{
private:
Animal animal;
public:
Person(string Pname,Animal animal):Animal(Pname),animal(animal)
{
}
void perform()
{
cout << "I'm:";
Animal::perform();
cout << "Look,this is :";
animal.perform();
}
};
class Lion:public Animal
{
public:
Lion(string name):Animal(name)
{
}
void perform()
{
cout << "Lion is eating" << endl;
}
};
class Elephant:public Animal
{
public:
Elephant(string name):Animal(name)
{
}
void perform()
{
cout << "Elephant is eating" << endl;
}
};
int main()
{
Lion lion("狮子");
Person p1("luxi",lion);
Animal *animal;
animal = &p1;
animal->perform();
return 0;
}
C++_day6
最新推荐文章于 2024-08-19 18:41:12 发布