#include "iostream" #include<string> using namespace std; class Animal { public: virtual void cry() {cout<<"不知哪种动物,让我如何学叫?"<<endl;} }; class Mouse:public Animal { public: void cry() { cout<<"我叫"<<name<<",是一只老鼠,我的叫声是:吱吱吱!"<<endl;} Mouse(string nam):name(nam){} private: string name; }; class Cat:public Animal { public: void cry() { cout<<"我叫"<<name<<",是一只猫,我的叫声是:喵喵喵!"<<endl;} Cat(string nam):name(nam){} private: string name; }; class Dog:public Animal { public: void cry() { cout<<"我叫"<<name<<",是一条狗,我的叫声是:汪汪汪!"<<endl;} Dog(string nam):name(nam){} private: string name; }; class Giraffe:public Animal { public: void cry () {cout<<"我叫"<<name<<",是一条狗,我的叫声是:汪汪汪!"<<endl;} Giraffe(string nam):name(nam){} private: string name; }; int main( ) { Animal *p; p = new Animal(); p->cry(); //输出: 不知哪种动物,让我如何学叫?(问题出自此处) Mouse m("Jerry"); p=&m; p->cry(); //输出: 我叫Jerry,是一只老鼠,我的叫声是:吱吱吱! Cat c("Tom"); p=&c; p->cry(); //输出: 我叫Tom,是一只猫,我的叫声是:喵喵喵! Dog d("Droopy"); p=&d; p->cry(); //输出: 我叫Droopy,是一条狗,我的叫声是:汪汪汪! Giraffe g("Gill"); p=&g; p->cry(); //输出: 我叫Droopy,是一条狗,我的叫声是:汪汪汪! system("pause"); return 0; }
#include "iostream" #include<string> using namespace std; class Animal { public: virtual void cry() const = 0; }; class Mouse:public Animal { public: void cry() const { cout<<"我叫"<<name<<",是一只老鼠,我的叫声是:吱吱吱!"<<endl;} Mouse(string nam):name(nam){} private: string name; }; class Cat:public Animal { public: void cry() const { cout<<"我叫"<<name<<",是一只猫,我的叫声是:喵喵喵!"<<endl;} Cat(string nam):name(nam){} private: string name; }; class Dog:public Animal { public: void cry() const { cout<<"我叫"<<name<<",是一条狗,我的叫声是:汪汪汪!"<<endl;} Dog(string nam):name(nam){} private: string name; }; class Giraffe:public Animal { public: void cry () const {cout<<"我叫"<<name<<",是一条狗,我的叫声是:汪汪汪!"<<endl;} Giraffe(string nam):name(nam){} private: string name; }; int main( ) { Animal *p; //p = new Animal(); p->cry(); 输出: 不知哪种动物,让我如何学叫?(问题出自此处) Mouse m("Jerry"); p=&m; p->cry(); //输出: 我叫Jerry,是一只老鼠,我的叫声是:吱吱吱! Cat c("Tom"); p=&c; p->cry(); //输出: 我叫Tom,是一只猫,我的叫声是:喵喵喵! Dog d("Droopy"); p=&d; p->cry(); //输出: 我叫Droopy,是一条狗,我的叫声是:汪汪汪! Giraffe g("Gill"); p=&g; p->cry(); //输出: 我叫Droopy,是一条狗,我的叫声是:汪汪汪! system("pause"); return 0; }
#include "iostream" #include<string> using namespace std; class Animal { public: virtual void cry() const = 0; Animal(string nam) { name=nam; } protected: string name; }; class Mouse:public Animal { public: void cry() const { cout<<"我叫"<<name<<",是一只老鼠,我的叫声是:吱吱吱!"<<endl;} Mouse(string nam):Animal(nam){} /*private: string name;*/ }; class Cat:public Animal { public: void cry() const { cout<<"我叫"<<name<<",是一只猫,我的叫声是:喵喵喵!"<<endl;} Cat(string nam):Animal(nam){} /*private: string name;*/ }; class Dog:public Animal { public: void cry() const { cout<<"我叫"<<name<<",是一条狗,我的叫声是:汪汪汪!"<<endl;} Dog(string nam):Animal(nam){} /*private: string name;*/ }; class Giraffe:public Animal { public: void cry () const {cout<<"我叫"<<name<<",是一条狗,我的叫声是:汪汪汪!"<<endl;} Giraffe(string nam):Animal(nam){} /*private: string name;*/ }; int main( ) { Animal *p; //p = new Animal(); p->cry(); 输出: 不知哪种动物,让我如何学叫?(问题出自此处) Mouse m("Jerry"); p=&m; p->cry(); //输出: 我叫Jerry,是一只老鼠,我的叫声是:吱吱吱! Cat c("Tom"); p=&c; p->cry(); //输出: 我叫Tom,是一只猫,我的叫声是:喵喵喵! Dog d("Droopy"); p=&d; p->cry(); //输出: 我叫Droopy,是一条狗,我的叫声是:汪汪汪! Giraffe g("Gill"); p=&g; p->cry(); //输出: 我叫Droopy,是一条狗,我的叫声是:汪汪汪! system("pause"); return 0; }
抽象类是专门方便与派生类的使用而存在的。
第十三周任务(二)
最新推荐文章于 2022-06-22 17:19:17 发布