First make a Animal class as parent class it contain
-name and gender as data members
-eating as member function
Make 5 classes of Animal type and inherit from Animal class
A. Sheep
B. Goat
C. Cow
D. Buffalo
E. Zebra
-This class can override its parent class method and implements on its own way
-call its eating method in the base of polymorphism
#include<iostream>
using namespace std;
class Animal{
private:
string gender;
string name;
public:
Animal(string g, string n){
gender = g;
name = n;
}
void eating(){
cout<<"Must eat to live"<<
}
};
class Sheep: public Animal{
eating(){
cout<<"Eating "<<endl;
}
};
class Goat: public Animal{
void eating(){
cout<<"Eating thorns"<<endl;
}
};
class Cow: public Animal{
void eating(){
cout<<"Eating reads"<<endl;
}
};
class Buffalo: public Animal{
void eating(){
cout<<"Eating baobab"<<endl;
}
};
class Zebra: public Animal{
void eating(){
cout<<"Eating baobab"<<endl;
}
};
int main(){
}
Comments
Leave a comment