You must Sign In to post a response.
  • Category: Programming

    Program to implement single/simple inheritance


    Have a question about writing an inheritance program? Want to know how to declare classes to implement single inheritance? On this Ask Expert page you can check out quality replies for your question.

    Following proramming question is asked in MSBTE Summer 2018 examination for Computer Engineering. This question carries 4 marks. Question is as below.
    Q)Write a program to implement single inheritance. Declare base class employee with Emp_No. and Emp_Name. Declare derived class fitness with height and weight. Accept and display data for one employee. [4 Marks]
    Looking forward for quality reply from our experts.
  • Answers

    3 Answers found.
  • You can use this code snippet to implement single/simple inheritance in C++ Program

    #include
    #include
    class employee
    {
    protected:
    int
    emp_no;
    char
    emp_name[25];
    void
    getdata()
    {
    cout<<"\n Enter employee no.";
    cin>>emp_no;
    cout<<"\n Enter emplyee name";
    cin>>emp_name;
    }
    void display()
    {
    cout<<"\n Employee no. is :"<cout<<"\n Employee name is:"<}
    };
    class
    fitness:public employee
    {
    protected:
    float
    height, weight;
    public:
    void
    getdata()
    {
    employee::getdata();
    cout<<"\n Enter height:";
    cin>>height;
    cout<<"\n Enter weight:";
    cin>>weight;
    }
    void display()
    {
    employee::display();
    cout<<"\n height is :"<cout<<"\n weight is :"<}
    };
    void main()
    {
    clrscr();
    fitness f;
    f.getdata();
    f.display();
    getch();
    }

    Please include and header files before class declaration.

    Phagu Mahato
    Success occurs when opportunity and preparation meet.

  • Inheritance is the most important feature provided by Object Oriented Programming. The main purpose of inheritance is the re-usability of the existing class. Using inheritance it is possible to derive one or more new classes from existing one or more classes. Here derived class have their own properties along with they can have properties of base(original) class/classes.
    There are different types inheritances available. They are
    1) Simple or Single inheritance. Here there is only one base class and only one derived class.
    2) Multiple inheritance. Here term multiple is used for base classes. So this type of inheritance have at least two base classes with one derived class.
    3) Multilevel inheritance. When derived class becomes base class for further inheritance, the concept is called as multiple inheritance.
    4) Hierarchical inheritance. When one base class has more than one derived classes then it is known as hierarchical inheritance.
    5) Hybrid inheritance. When more than one inheritances are used together then the term is known as hybrid inheritance.
    Syntax for declaring derived class from base class is as follows

    class derived class_name: visibility mode base class_name

    Syntax for declaring derived class in case of multiple inheritance is as below
    class derived class_name : visibility mode base class1, visibility mode base class2, .....

    Syntax for declaring derived class in case of multilevel inheritance is as follows
    class derived class1 : visibility mode base class
    class derived class 2:visibility mode class1

    Below is the program to implement single/simple inheritance.

    //PROGRAM TO IMPLEMENT SINGLE INHERITANCE
    #include
    #include
    class employee
    {
    private:
    int empno;
    char empname[25];
    public:
    void get(void)
    {
    cout<<"Enter employee number and employee name\n";
    cin>>empno>>empname;
    }
    void display(void)
    {
    cout<<"\nEmployee name is "< cout<<"\nEmployee number is "< }
    };

    class fitness:public employee
    {
    private:
    float height, weight,r;
    int temp;
    public:
    void input(void)
    {
    cout<<"\nEnter height and weight\n";
    cin>>height>>weight;
    temp=height;
    r=height-temp;
    }
    void output(void)
    {
    cout<<"\nEmployee height is "< cout<<"\nEmployee weight is "< }
    };

    void main(void)
    {
    class fitness f;
    clrscr();
    f.get();
    f.input();
    cout<<"\nDetails of employee are as follows\n";
    f.display();
    f.output();
    getch();
    }

    Please include and header files before class declaration.

  • Inheritance:- Inheritance is one of the most important pillar of Object-Oriented Programming. Inheritance simply means the reusability of code meaning you can inherit the data member and member function from one class to another class. There is two most common terminology that is:-
    (A) Parent / Super/ or Base class:- These are the classes, which properties are inherited by other classes.
    (B) Child / Sub/ or Drive class:- These are the classes that inherit the property from other classes. So whenever this class is trying to inherit the property it usage a keyword that is 'extends'.
    Below are the type of inheritance in java:-
    (A) Single or Simple Inheritance:- In this type of inheritance, the child/sub or drive class inherits the property from base class. This type of inheritance can have only one parent and one child class. Below is the syntax of the Single inheritance:-
    class Parent
    {
    //data and member function
    }
    class Child extends Parent
    {
    //Access the data member from the Parent class
    ......
    }

    (B) Multilevel Inheritance:- In this type of inheritance, the child class inherits the property of parent class and it acts as the parent class for the other child class so that other child class can inherit the property from them. Below is the syntax for the Multilevel Inheritance:-
    class Parent
    {
    //data and member function
    }
    class ChildButAsParent extends Parent //further this class act as the parent class so other classes inherit property from this class.
    {
    //Access the data member from the Parent class
    ......
    }
    class Child extends ChildButAsParent // here ChildButAsParent act as parent class
    {
    //Access the data member and member function from the ChildButAsParent class if needed.
    }

    (C) Hierarchical Inheritance:- In this type of inheritance, only one parent class is inherited by the more than one child class. Below is the syntax for the Hierarchical Inheritance:-
    class Parent
    {
    //data and member function
    }
    class FirstChild extends Parent
    {
    //Access the members from the Parent class
    ......
    }
    class SecondChild extends Parent
    {
    //Access the members from the Parent class
    ......
    }

    (D) Multiple Inheritance:- Java doesn't support Multiple Inheritance due to the diamond problem. But we can implement the multiple inheritances by using the Interface. In this type of inheritance one child class inherit the property from more than one parent class. Below is the syntax for the Multiple Inheritance:-
    Interface ParentOne
    {
    //members
    }
    Interface ParentTwo
    {
    //members
    }
    Class Child implements ParentOne, ParentTwo
    {
    //members of both above classes.
    }

    (E) Hybrid Inheritance:- It is the combination of two or more than two types of inheritances. We can implements also this one by using the Interface.


  • Sign In to post your comments