#include <iostream>
class Mammal
{
public:
Mammal():age(1) { std::cout << "Mammal constructor ...\n"; }
virtual ~Mammal() { std::cout << "Mammal destructor ...\n"; }
virtual void speak() const { std::cout << "Mammal speak!\n"; }
protected:
int age;
};
class Cat: public Mammal
{
public:
Cat() { std::cout << "Cat constructor ...\n"; }
~Cat() { std::cout << "Cat destructor ...\n"; }
void speak() const { std::cout << "Meow!\n"; }
void purr() const { std::cout << "Rrrrrrrrrrr!\n"; }
};
class Dog: public Mammal
{
public:
Dog() { std::cout << "Dog constructor ...\n"; }
~Dog() { std::cout << "Dog destructor ...\n"; }
void speak() const { std::cout << "Woof!\n"; }
};
int main()
{
const int numberMammals = 3;
Mammal* zoo[numberMammals];
Mammal* pMammal;
int choice, i;
for (i = 0; i < numberMammals; i++)
{
std::cout << "(1)Dog (2)Cat: ";
std::cin >> choice;
if (choice == 1)
pMammal = new Dog;
else
pMammal = new Cat;
zoo[i] = pMammal;
}
std::cout << "\n";
for (i = 0; i < numberMammals; i++)
{
zoo[i]->speak();
Cat *pRealCat = dynamic_cast<Cat *> (zoo[i]);
if (pRealCat)
pRealCat->purr();
else
std::cout << "Uh oh, not a cat!\n";
delete zoo[i];
std::cout << "\n";
}
return 0;
}
此处想要对cat单独定义一个函数,对dog不能使用,但是pCat是mammal指针(多态的本质),故使用运算符转换为Cat指针。
对于这个问题也可以使用纯虚函数解决。如下,在Mammal中定义一个纯虚函数,使其成为ADT(抽象数据类型),然后在dog和cat类中分别重写purr()函数。
#include <iostream>
class Mammal
{
public:
Mammal():age(1) { std::cout << "Mammal constructor ...\n"; }
virtual ~Mammal() { std::cout << "Mammal destructor ...\n"; }
virtual void speak() const { std::cout << "Mammal speak!\n"; }
virtual void purr() const=0; //ADT
protected:
int age;
};
class Cat: public Mammal
{
public:
Cat() { std::cout << "Cat constructor ...\n"; }
~Cat() { std::cout << "Cat destructor ...\n"; }
void speak() const { std::cout << "Meow!\n"; }
void purr() const { std::cout << "Rrrrrrrrrrr!\n"; }
};
class Dog: public Mammal
{
public:
Dog() { std::cout << "Dog constructor ...\n"; }
~Dog() { std::cout << "Dog destructor ...\n"; }
void speak() const { std::cout << "Woof!\n"; }
void purr() const {std::cout<<"Uh oh, not a cat!\n";}
};
int main()
{
const int numberMammals = 3;
Mammal* zoo[numberMammals];
Mammal* pMammal;
int choice, i;
for (i = 0; i < numberMammals; i++)
{
std::cout << "(1)Dog (2)Cat: ";
std::cin >> choice;
if (choice == 1)
pMammal = new Dog;
else
pMammal = new Cat;
zoo[i] = pMammal;
}
std::cout << "\n";
for (i = 0; i < numberMammals; i++)
{
zoo[i]->speak();
// Cat *pRealCat = dynamic_cast<Cat *> (zoo[i]);
// if (pRealCat)
// pRealCat->purr();
// else
// std::cout << "Uh oh, not a cat!\n";
zoo[i]->purr();
delete zoo[i];
std::cout << "\n";
}
return 0;
}