Answer to Question #189359 in C++ for Sami

Question #189359

Create two classes named Mammals and SeaAnimal. Create another class named Whale which inherits both the above classes. Now, create a function in each of these classes which prints "I am mammal" in class Mammals, "I am a marine animal” in class SeaAnimal and "I belong to both the categories: Mammals as well as Marine Animals" in class Whale. Now, create an object for each of the above class in main function and try calling.

1 - function of Mammals by the object of Mammal

2 - function of SeaAnimal by the object of SeaAnimal

3 - function of Whale by the object of Whale

4 - function of each of its parent by the object of Whale



1
Expert's answer
2021-05-05T04:52:58-0400
#include <iostream>




using namespace std;




//define class Mammals
class Mammals{
	public:
		void print1() {
			cout << "I am mammal" << endl;
		}
};




//define class SeaAnimal
class SeaAnimal {
	public:
		void print2() {
			cout << "I am a sea animal" << endl;
		}
};




//define class Whale
class Whale: public Mammals, public SeaAnimal {
	public:
		void print3() {
			cout << "I belong to both the categories: Mammals as well as Whale Animals" << endl;
		}
};




int main()
{
	Mammals mammals;  //object for class Mammals
	SeaAnimal seaAnimal; //object for class SeaAnimal
	Whale whale; //object for class Whale
	
	mammals.print1(); // 1 - function of Mammals by the object of Mammal
	seaAnimal.print2(); // 2 - function of SeaAnimal by the object of SeaAnimal
	whale.print3(); // 3 - function of Whale by the object of Whale
	//4 - function of each of its parent by the object of Whale
	whale.print1();
	whale.print2(); 




	system("pause");
	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