C++ Program to enter Student details using Hierarchical Inheritance

By | 02.01.2017

Student details using Hierarchical Inheritance


Write a C++ Program to enter Student details using Hierarchical Inheritance. Here’s a Simple C++ Program to enter Student details using Hierarchical Inheritance in C++ Programming Language.


Hierarchical Inheritance


  • When two or more classes are derived from a single base  class, then Inheritance is called the hierarchical inheritance. The representation of the hierarchical inheritance is shown in the following Example:
  • Student is a base class, from which the three classes viz. arts, science and commerce have been derived. Now, let us write a program that illustrates the hierarchical inheritance, based on the above design.

 

Below is the source code for C++ Program to enter Student details using Hierarchical Inheritance which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C++ Program to enter Student details using Hierarchical Inheritance */

#include<iostream>
using namespace std;

const int len = 20 ;

class student  //  Base class
{
        public:
            char F_name[len];
        char L_name[len];
            int age;
        int roll_no ;
                void Enter_data(void)
                {
                        cout << "\n\t Enter the first name: " ; cin >> F_name ;
                        cout << "\t Enter the second name: "; cin >> L_name ;
                        cout << "\t Enter the age: " ; cin >> age ;
                        cout << "\t Enter the roll_no: " ; cin >> roll_no ;
                }
                void Display_data(void)
                {
                        cout << "\n\t First Name = " << F_name ;
                        cout << "\n\t Last Name = " << L_name ;
                        cout << "\n\t Age = " << age ;
                        cout << "\n\t Roll Number = " << roll_no ;
                }
};

class arts : public student
{
        private:
                char asub1[len] ;
                char asub2[len] ;
                char asub3[len] ;
        public:
                void Enter_data(void)
                {
                        student :: Enter_data( );
                        cout << "\t  Enter the subject1 of the arts student: "; cin >> asub1 ;
                        cout << "\t  Enter the subject2 of the arts student: "; cin >> asub2 ;
                        cout << "\t  Enter the subject3 of the arts student: "; cin >> asub3 ;
                }
                void Display_data(void)
                {
                        student :: Display_data( );
                        cout << "\n\t Subject1 of the arts student = " << asub1 ;
                        cout << "\n\t Subject2 of the arts student = " << asub2 ;
                        cout << "\n\t Subject3 of the arts student = " << asub3 ;
                }
};

class science : public student
{
        private:
                char ssub1[len] ;
                char ssub2[len] ;
                char ssub3[len] ;
        public:
                void Enter_data(void)
                {
                        student :: Enter_data( );
                        cout << "\t Enter the subject1 of the science student: "; cin >> ssub1;
                        cout << "\t Enter the subject2 of the science student: "; cin >> ssub2;
                        cout << "\t Enter the subject3 of the science student: "; cin >> ssub3;
                }
                void Display_data(void)
                {
                        student :: Display_data( );
                        cout << "\n\t Subject1 of the science student = " << ssub1 ;
                        cout << "\n\t Subject2 of the science student = " << ssub2 ;
                        cout << "\n\t Subject3 of the science student = " << ssub3 ;
                }
};

class commerce : public student
{
        private:   char csub1[len], csub2[len], csub3[len] ;
        public:
                void Enter_data(void)
                {
                        student :: Enter_data( );
                        cout << "\t Enter the subject1 of the commerce student: ";
                        cin >> csub1;
                        cout << "\t Enter the subject2 of the commerce student: ";
                        cin >> csub2 ;
                        cout << "\t Enter the subject3 of the commerce student: ";
                        cin >> csub3 ;
                }
                void Display_data(void)
                {
                        student :: Display_data( );
                        cout << "\n\t Subject1 of the commerce student = " << csub1 ;
                        cout << "\n\t Subject2 of the commerce student = " << csub2 ;
                        cout << "\n\t Subject3 of the commerce student = " << csub3 ;
                }
};

int main()
{
                arts a ;
                cout << "\n Entering details of the arts student\n" ;
                a.Enter_data( );
                cout << "\n Displaying the details of the arts student\n" ;
                a.Display_data( );
                science s ;
                cout << "\n\n Entering details of the science student\n" ;
                s.Enter_data( );
                cout << "\n Displaying the details of the science student\n" ;
                s.Display_data( );
                commerce c ;
                cout << "\n\n Entering details of the commerce student\n" ;
                c.Enter_data( );
                cout << "\n Displaying the details of the commerce student\n";
                c.Display_data( );
return 0;
}

OUTPUT : :


/*  C++ Program to enter Student details using Hierarchical Inheritance */

 Entering details of the arts student

         Enter the first name: John
         Enter the second name: Max
         Enter the age: 19
         Enter the roll_no: 123
          Enter the subject1 of the arts student: A
          Enter the subject2 of the arts student: B
          Enter the subject3 of the arts student: C

 Displaying the details of the arts student

         First Name = John
         Last Name = Max
         Age = 19
         Roll Number = 123
         Subject1 of the arts student = A
         Subject2 of the arts student = B
         Subject3 of the arts student = C

 Entering details of the science student

         Enter the first name: Codez
         Enter the second name: Club
         Enter the age: 20
         Enter the roll_no: 211
         Enter the subject1 of the science student: E
         Enter the subject2 of the science student: F
         Enter the subject3 of the science student: G

 Displaying the details of the science student

         First Name = Codez
         Last Name = Club
         Age = 20
         Roll Number = 211
         Subject1 of the science student = E
         Subject2 of the science student = F
         Subject3 of the science student = G

 Entering details of the commerce student

         Enter the first name: Sam
         Enter the second name: Payne
         Enter the age: 21
         Enter the roll_no: 234
         Enter the subject1 of the commerce student: H
         Enter the subject2 of the commerce student: I
         Enter the subject3 of the commerce student: J

 Displaying the details of the commerce student

         First Name = Sam
         Last Name = Payne
         Age = 21
         Roll Number = 234
         Subject1 of the commerce student = H
         Subject2 of the commerce student = I
         Subject3 of the commerce student = J

Process returned 0

Above is the source code and output for C++ Program to enter Student details using Hierarchical Inheritance which is successfully compiled and run on Windows System to produce desired output.

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post….

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments