Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Animal.h // // Animal.h // test03 // // Created by Sildomar T. Monteiro on 4/9/1

ID: 660936 • Letter: A

Question

Animal.h

//
// Animal.h
// test03
//
// Created by Sildomar T. Monteiro on 4/9/15.
// Copyright (c) 2015 Sildomar T. Monteiro. All rights reserved.
//

#ifndef Animal_h
#define Animal_h

class Animal {
public:
virtual void soundThatAnimalMakes() = 0;
  
};
#endif

Given the base class Animal (source file provided), develop two classes, e.g. cats and birds, derived from the Animal class. Define two objects of your derived types, and use base-class pointers (i.e. polymorphism) pointing to your derived objects to output the sounds that the animals make

Explanation / Answer

#include <iostream>
#include<conio.h>
using namespace std;


class Animal
{
public:
Animal()
{
cout<<"constructor of Animal class class ";
}
virtual void sound()
   {
   cout<< "Birds sounds ";
        
   }
   
};

class Cat : public Animal
{
public:
   Cat()
   {
   cout<<"constructor of Cat class ";
   }
void sound()
   {
   cout<< "Cat sounds ";
   cout<< "miyaun! miyaun! ";
      
   }
     
};
class Bird : public Animal
{
public:
   Bird()
   {
   cout<<"constructor of birds class ";
   }
  
};


int main()
{
//   Animal* ptr;// = new Animal;
  
   Cat c;//= new Cat;
   Bird b;//=new Bird;
  
   c.sound();
   b.sound();

   Animal* ptr;
   ptr=&c;
   ptr->sound();
  
ptr=&b;
   ptr->sound();

  
   cout<<"Object created dynamically";
  
return 0;
}