Eat Sleep Code Example using Multilevel Inheritance in C++
This is a simple C++ program that demonstrates inheritance using classes.
- There are three classes defined: Human, Student, and Coder. The Human class has a member function called eat(). The Student class is derived from the Human class and has an additional member function called sleep(). The Coder class is derived from the Student class and has yet another member function called code().
- In the main() function, an object of the Coder class is created and its member functions are called: eat(), sleep(), and code(). When the program is run, it will output "Eating...", "Sleeping...", and "Coding..." on separate lines.
- The purpose of this program is to demonstrate how inheritance works in C++. The Student and Coder classes inherit all of the member variables and functions of the Human class, allowing them to use the eat() function without having to redefine it. Similarly, the Coder class inherits all of the member variables and functions of the Student class, allowing it to use both the eat() and sleep() functions.
Source Code
#include<iostream>
using namespace std;
class Human
{
public:
void eat()
{
cout<<"Eating..."<<endl;
}
};
class Student: public Human
{
public:
void sleep()
{
cout<<"Sleeping..."<<endl;
}
};
class Coder: public Student
{
public:
void code()
{
cout<<"Coding...";
}
};
int main(void)
{
Coder c1;
c1.eat();
c1.sleep();
c1.code();
return 0;
}
To download raw file
Click Here
Output
Eating...
Sleeping...
Coding...