#include <iostream>#include<string>using namespacestd;classpet{ Public: Pet (stringthename); voideat (); voidsleep (); Virtual voidplay ();protected: stringname;};classCat: Publicpet{ Public: Cat (stringthename); voidClimb (); voidplay ();};classDog: Publicpet{ Public: Dog (stringthename); voidbark (); voidplay ();}; Pet::P et (stringthename) {Name=thename;}voidpet::eat () {cout<< name <<"[is eating something]"<<Endl;}voidPet::sleep () {cout<< name <<"[is sleeping]"<<Endl;}voidPet::p Lay () {cout<< name <<"[Playing]"<<Endl;} Cat::cat (stringthename): Pet (thename) {}voidcat::climb () {cout<< name <<"[Climbing trees]"<<Endl;}voidCat::p Lay () {Pet::p lay (); cout<< name <<"[play yarn ball]"<<Endl;} Dog::D og ( string thename): Pet (thename) {}voidDog::bark () {cout<< name <<"[ Wang Wang Wang]"<<Endl;}voidDog::p Lay () {Pet::p lay (); cout<< name <<"[Chasing the cat]"<<Endl;}intMain () {int*p =New int; *p = -; cout<< *p <<Endl; Deletep; Pet*cat =NewCat ("Garfield"); Pet*dog =NewDog ("Odie"); Cat-sleep (); Cat-eat (); Cat-Play (); Dog-sleep (); Dog-eat (); Dog-Play (); DeleteDog; DeleteCat; return 0;}
C + + virtual methods