Starting from:

$30

CS204-Homework 7 Inheritance and Polymorphism Solved

In this homework, you are asked to implement a simple program using inheritance and polymorphism as described in the lectures. In this homework, you will be writing a base class called Human which is inherited by two classes: Muggle, Wizardkind. And Wizardkind class is also inherited by two other classes called: Student, GrownUp. The derived classes inherit the functions of their bases classes and also add new member variables and functions. The functions and variables the classes have is illustrated in the following diagram.

 

 

Class Definitions
 

1-     Human

a.      Has two member variables: name and age.

b.      Has a function named: printInfo, which prints the name, age information of a human.

2-     Muggle

a.      Has a member variable: job.

b.      Has printInfo function implemented to print job alongside name and age.

3-     Wizardkind

a.      Has a member variable: wand.

b.      Has printInfo function implemented to print wand alongside name and age.

c.      Has another function, called doSpell which takes a string parameter which is the spell to cast.

4-     Student

a.      Has two more member variables: pet, houseName.

b.      Has printInfo function implemented to print pet and houseName alongside name, age and wand.

c.      Has another function called feedPet, which prints a corresponding message.  

i. Examle: “Harry Potter fed Hedwig.”

5-     GrownUp

a.      Has one additional member variable: job.

b.      Has printInfo function implemented to print job alongside name, age and wand.

 

 

 Sample Main
 

You can use the main function provided below to test your program and compare the output of yours’ with the given output.

 

int main(){ 

    Muggle vernon("Vernon Dursley", 50, "Director at Grunnings");     vernon.printInfo(); 

 

       cout << endl; 

       Student Harry("Harry Potter", 16, "Phoenix Feather", "Hedwig", "Gryffindor");             Harry.printInfo(); 

 

       cout << endl; 

       GrownUp Dumbledore("Albus Dumbledore", 110, "Elder Wand", "Headmaster"); 

       Dumbledore.printInfo(); 

 

       cout << endl; 

       Dumbledore.doSpell("Expecto Patronum"); 

 

       cout << endl; 

       Harry.doSpell("Expelliarmus"); 

 

       cout << endl;   Harry.feedPet(); 

 

       cout << endl; 

       Student Ginny("Ginny Weasley", 15, "Yew wood", "Arnold", "Gryffindor"); 

     GrownUp Snape("Severus Snape", 35, "Dragon Heartstring", "Potion Master"); 

       Student Hermione("Hermione Granger", 16, "Vine", "Crookshanks", "Gryffindor");  

       Human hArray[6]; 

 

       hArray[0] = vernon;   hArray[1] = Harry;   hArray[2] = Dumbledore;   hArray[3] = Ginny;   hArray[4] = Snape; 

       hArray[5] = Hermione; 

 

       Human * hPtr;   for (int i = 0; i < 6; i++) { 

              cout << endl;           hPtr = &hArray[i];     hPtr-printInfo(); 

       } 

 

       cout << endl; 

       system("pause"); 

 

    return 0; 

}

Output
 

Muggle Informations 

Name: Vernon Dursley 

Age: 50 

Job: Director at Grunnings 

 

Student Wizardkind Informations 

Name: Harry Potter 

Age: 16 

House: Gryffindor 

Wand: Phoenix Feather 

Pet: Hedwig 

 

Grownup Wizardkind Informations 

Name: Albus Dumbledore 

Age: 110 

Wand: Elder Wand 

Job: Headmaster 

 

Albus Dumbledore used spell: Expecto Patronum 

 

Harry Potter used spell: Expelliarmus 

 

Harry Potter fed Hedwig 

 

 

Human informations 

Name: Vernon Dursley 

Age: 50 

 

 

Human informations 

Name: Harry Potter 

Age: 16 

 

 

Human informations 

Name: Albus Dumbledore 

Age: 110 

 

 

Human informations 

Name: Ginny Weasley 

Age: 15 

 

 

Human informations 

Name: Severus Snape 

Age: 35 

 

 

Human informations 

Name: Hermione Granger 

Age: 16 

 

 

Press any key to continue . . . 

More products