In this task, we implement multiple inheritance. We create two classes, a ‘Mammal’ and a
‘Bird’ class. Then we create another class which inherits both the classes, we name the third
class ‘Organism’. Consider including the following attributes and functionalities to your
classes.
class Mammal
void setMammalName(string);
void showMammal();
class Bird
void setBirdName(string);
void showBird();
class Organism
void setOrganismName(string);
void showOrganism();
char* getOrganismName();
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
using namespace std;
class Mammal
{
private:
string name;//Name
public:
Mammal()
{
}
Mammal(string name)
{
this->name=name;
}
void setMammalName(string& s)
{
this->name=s;
}
void showMammal()
{
cout<<"Mammal name: "<<this->name<<endl;
}
};
class Bird
{
protected:
int age;//Birth's age
string name;//name
public:
Bird()
{
this->age=15;
}
Bird(string nm,int age)
{
this->name=nm;
this->age=age;
}
void setBirdName(string&s)
{
this->name=s;
}
void showBird()
{
cout<<this->name<<" | "<<this->age<<endl;
}
};
class Organism:public Mammal,Bird
{
private:
char nm[100];
public:
Organism():Mammal(),Bird()
{
}
Organism(string s):Mammal(s),Bird(s,5)
{
strcpy(nm,s.c_str());
}
void setOrganismName(string &s)
{
this->setMammalName(s);
this->setBirdName(s);
}
char* getOrganismName()
{
return this->nm;
}
void showOrganism()
{
this->showBird();
this->showMammal();
}
};
int main()
{
Organism r("Organizm");
r.showOrganism();
return 0;
}
Comments
Leave a comment