Answer to Question #341943 in C++ for khan

Question #341943

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();

1
Expert's answer
2022-05-17T16:05:55-0400


#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS