Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Animal{
- public:
- Animal() {
- age = 0;
- }
- virtual ~Animal(){}
- static Animal* factory(int type);
- int aliveSince(){
- return age;
- }
- virtual int fly(int m){} //error, needs a return value
- virtual int swim(int m){} //error, needs a return value
- private:
- int age;
- };
- class Bird: public Animal{
- public:
- Bird(){
- totalFlight = 0;
- }
- int fly(int m){
- cout<<"Bird flew "<<m<<" metresn";
- return totalFlight = totalFlight+m;
- }
- private:
- int totalFlight;
- };
- class Fish: public Animal{
- public:
- Fish(){
- totalSwim = 0;
- }
- int swim(int m){
- cout<<"Fish swam "<<m<<" metres undergroundn";
- return totalSwim = totalSwim + m;
- }
- private:
- int totalSwim;
- };
- Animal* Animal::factory(int type){
- if (type) return new Bird();
- else return new Fish();
- }
- Animal *a = Animal::factory(1); //Bird
- Animal *b = Animal::factory(0); //Fish
- a->aliveSince();
- b->aliveSince();
- a->fly(5);
- b->swim(5);
- virtual int fly(int m){ return 0; }
- virtual int swim(int m){ return 0; }
- class Animal{
- public:
- Animal()
- {
- totalMoved = 0;
- }
- virtual ~Animal(){}
- virtual int move( int m )
- {
- totalMoved += m;
- cout<<"Total distance " << totalMoved << std::endl;
- return totalMoved ;
- }
- protected:
- int totalMoved;
- };
- class Bird: public Animal{
- public:
- int move(int m)
- {
- cout<<"Bird flew "<<m<<" metresn";
- return Animal::move( m );
- }
- };
- class Fish: public Animal{
- public:
- int swim(int m){
- cout<<"Fish swam "<<m<<" metres undergroundn";
- return Animal::move( m );
- }
- };
Add Comment
Please, Sign In to add comment