标签:des style blog http io color os ar 使用
无
输出数据共有3行,本题目要求输出内容必须与示例中的相同。
How does a pet speak ? I am a cat,My name is Tom My sound is miao!miao! I am a dog,My name is Snoppy My sound is wang!wang!
#include <iostream> using namespace std; class Pet { public : virtual void speak()//定义虚函数 { cout<<"How does a pet speak ?"<<endl; } }; class Cat: public Pet { public : void speak () { cout <<"I am a cat,My name is Tom My sound is miao!miao!"<<endl; } }; class Dog : public Pet { public : void speak() { cout<<"I am a dog,My name is Snoppy My sound is wang!wang!"<<endl; } }; int main() { Pet p, *t;//定义Pet类对象的指针t Cat c; Dog d; t = &p;//t指针指向p t->speak();//调用p.speak函数 t = &c;//t指针指向c t->speak();//调用c.speak函数 t = &d;//t指针指向d t->speak();//调用d.speak函数 return 0; }
标签:des style blog http io color os ar 使用
原文地址:http://blog.csdn.net/u013634961/article/details/40593387