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...

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples