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.
  • #153102
    The status of this response is Pending and require some modification by the author.

    Please find below the program in C++ language. Enjoy coding!
    **************************************************************************************************************************
    #include
    #include
    using namespace std;

    class employee {
    public:
    int Emp_No;
    char Emp_Name[20];

    void get() {
    cout << "Enter the Employee number:";
    cin>>Emp_No;
    cout << "Enter the Employee name:";
    cin>>Emp_Name;
    }
    };

    class fitness: public employee {
    float height,weight;
    public:

    void get1() {
    cout << "Enter the Height:";
    cin>>height;
    cout << "Enter the Weight:";
    cin>>weight;

    }

    void display() {
    cout << Emp_No << "\t" << Emp_Name << "\t" << height << "\t" << weight <<"\n";
    }
    };

    void main() {
    int i, n;
    char ch;
    fitness s[10];
    clrscr();
    cout << "Enter the number of employee:";
    cin>>n;
    for (i = 0; i < n; i++) {
    s[i].get();
    s[i].get1();

    }
    cout << "\nEmployee Number \t Employee Name\t Height of Employee \t Weight of Employee \n";
    for (i = 0; i < n; i++) {
    s[i].display();
    }
    getch();
    }

  • #153103
    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.

  • #153104
    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.


  • Sign In to post your comments