Answer to Question #201575 in C++ for Asif

Question #201575

Write a program which uses the concept of Hierarchical Inheritance


1
Expert's answer
2021-06-01T06:47:49-0400
#include <iostream>


using namespace std;


// parent class Animal
class Animal {
public:
	void displayInfo() {
		cout << "I am an animal." << endl;
	}
	virtual void speak() const{
	
	}
};


// child class Dog
class Dog : public Animal {
public:
	void speak() const{
		cout << "I am a Dog." << endl;
	}
};


// child class Cat
class Cat : public Animal {
public:
	void speak() const {
		cout << "I am a Cat." << endl;
	}
};


int main() {
	Dog dog;
	dog.displayInfo();
	dog.speak();


	cout<<"\n";
	Cat cat;
	cat.displayInfo();
	cat.speak();


	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