Uncategorized

Use Inheritance concepts to create a class

Use Inheritance and Polymorphism concepts to create a class named Animal with three derived classes. Each Animal has different characteristics and act different. Create a class that will show these differences by creating objects of type Animal, Dog, Cat and Bird.

  • Create a base class named Animal with three derived classes named
    • Gorilla,
    • Fish, and
    • Bird.
  • Create a derived class named Penguin whose base class is Bird.
  • Create constructors for each class.
  • Create destructors for each class.
  • All classes will have the following private instance fields, including Animal
    • legs that will initialize to 0
    • boolean swim that will initialize to false;
  • All classes will have the following polymorphic methods, including Animal
    • getLegs( ) – this returns the number of legs the animal has
    • makeSount( ) – this outputs the type of sound the animal makes
    • canSwim( ) – this will return true or false
  • makeSound( ) method will be a virtual function in the Animal class
  • Create an exception in the getLegs( ) method that makes sure the number of legs are valid
  • Create a main method to demonstrate the all functions including the following with appropriate output

Animal animal; animal.makeSound(); Dog dog dog.makeSound(); cout Animal badDog = Dog(); badDog.makeSound(); cout Animal* goodDog = new Dog(); goodDog->makeSound(); cout << goodDog->getLegs;

The post Use Inheritance concepts to create a class appeared first on My Assignment Online.

Leave a Reply

Your email address will not be published. Required fields are marked *