Tuesday 14 March 2017

C++ in Govindpuram Ghaziabad (inheritance)

4.2 Multilevel Inheritance
 In the Multilevel inheritance a class derived from another derived class. If a class B is derived form class A and class C derived from class B, like a figure 4.2 , then it is called multilevel inheritance. Because there are more than one levels. You can also derive class C , and then derive its derived and so on.


 


                                                                                                                                                               



Figure 4.2:Multilevel Inheritance

Here we have given some example to understand the concept of Multilevel inheritance.

Example 4.4 Multilevel inheritance

#include<iostream.h>

class A
{
            int a1, a2;
public:
            void getdata()
            {
                        cout<<”\nEnter the value of a1 and a2 :”<<endl;
                        cin>> a1 >> a2;
            }
            void putdata()
            {
                        cout<<”\nThe value of a1 is :”<< a1 <<  “and a2 :”<<a2;
            }
 };

class B : public A       
{
            int b1, b2;
public:
            void indata()
            {
                        cout<<”\nEnter the value of b1 and b2 :” <<endl;
                        cin>> b1 >> b2;
            }
            void outdata()
            {
                        cout<<”\nThe value of b1 is :”<< b1 <<  “and b2 :”<<b2;
            }
 };

class C : public B       
{
            int c1, c2;
public:
            void input()
            {
                        cout<<”\nEnter the value of c1 and c2 :” <<endl;
                        cin>> c1 >> c2;
            }
            void output()
            {
                        cout<<”\nThe value of c1 is :”<< c1 <<  “and c2 :”<<c2;
            }
 };

void main()
{

            C Obj;

            Obj.getdata();                // member function of class A
           
Obj.indata();                  // member function of class B

Obj.input();                   // member function of class C

Obj.putdata();                // member function of class A
Obj.outdata();                // member function of class B

Obj.output();                 // member function of class C

}

Output of Example 4.4 Multilevel inheritance

Enter the value of a1 and a2 :
2
3

Enter the value of b1 and b2 :
4
5

Enter the value of c1 and c2 :
6
7

The value of a1 is : 2  and a2 is : 3

The value of b1 is : 4  and b2 is : 5

The value of c1 is : 6  and c2 is : 7

Example 4.5 Multilevel inheritance

#include<iostream.h>

class First
{
protected:
            int a;                 // a is protected variable will inherit in derived classes
public:
            void getdata()
            {
                        cout<<”\nEnter the value of a :” <<endl;
                        cin>> a;
            }
            void putdata()
            {
                        cout<<”\nThe value of a is :”<< a;
            }
 };

class Second : public First       
{
protected:
int b;                 // b is protected variable will inherit in derived classes
public:
            void indata()
            {
                        cout<<”\nEnter the value of b :” <<endl;
                        cin>> b;
            }
void outdata()
            {
                        cout<<”\nThe value of b is :”<< b;
            }

};

class Third : public Second       
{
int c;                
public:
            void input()
            {
                        cout<<”\nEnter the value of c :” <<endl;
                        cin>> c;
            }
void output()
            {
                        cout<<”\nThe value of c is :”<< c;
            }

            void addition ()
            {
                        int d = a + b + c;
            cout<<”\n Addition of variables: ”<< d;
             }
 };

void main()
{

            Third T ;

            T.getdata();                   // member function of class First
           
T.indata();                     // member function of class Second

T.input()                        // member function of class Third

T.putdata();
T.outdata();
T.output();

T.addition();
}

Output of Example 4.5 Single inheritance

Enter the value of a :
2

Enter the value of b :
4

Enter the value of c :
6

The value of a is : 2 

The value of b is : 4

The value of b is : 6

 Addition of both variables : 12

Example 4.6 Multilevel inheritance

#include<iostream.h>
#include<string.h>

class Student
{
            char name[10];
            int rollno;
public:
            void getdata()
            {
                        cout<<”\nEnter the name :”<<endl;
                        cin>> name;
                        cout<< “\nEnter the Roll No. :”<<endl;
                        cin>>rollno;
            }
            void putdata()
            {
                        cout<<”\n Name  :”<< name;
                        cout<<”\n Roll No. :”<<rollno;
            }
 };

class Marks : public Student       
{
            int marks[4];
protected:
            int total;
public:
            void getmarks();
            void putmarks();
};

void Marks :: getmarks()
            {
                        for(int i = 0; i <= 4 ; i++)
{
cout<<”\nEnter the Marks of Subject ”<< i+1<< “: =” <<endl;
                        cin>> marks[i];
}
            }
void Marks::putmarks()
            {
                        total = 0;
                        for(int i = 0; i <= 4 ; i++)
{
cout<<”\nMarks of Subject ”<< i+1<< “: =” << marks[i];
total = total + marks[i]; 
}
                        cout<<”\n Total Marks :”<<total;
}

class Result : public Marks       
{
            int perc;
            boolean pass;                // it will store boolean value TRUE for pass and FALSE for fail
            char division[10];
public:
            void check();
            void display();
};

void Result :: ckeck()
{
            pass = TRUE;
            for(int i = 0; i <= 4 ; i++)
{
            if (marks[i]<30)
                                    pass = FALSE;
}
            perc = (total * 100) / 400;
}

void Result :: display()
{
            if (pass = = TRUE)
            {
                        if(perc >= 60)
                                    strcpy(division, “First”);
                        elseif(perc >= 45)
                                    strcpy(division, “Second”);
                        elseif(perc >= 30)
                                    strcpy(division, “Third”);
                        else
                                    strcpy(division, “Fail”);
            }
            else
            {
                        cout<<”\nStudent is Fail”;
                        break;
            }
                       
            cout<<” \n Percentage :”<<perc<< “%”;
            cout<<”\n Division :”<<division;
}
void main()
{
            Result S;
           
            S.getdata();
            S.getmarks();

            S.putdata();
S.putmarks()
           
            S.check();
            S.display();
}


Output of Example 4.6 Multilevel inheritance

Enter the Name :
Ravi

Enter the Roll No. :
1104

Enter the Marks of Subject 1
65

Enter the Marks of Subject 2
69

Enter the Marks of Subject 3
76

Enter the Marks of Subject 4
54

Name : Ravi

Roll No.: 1104

Marks of Subject 1
65

Marks of Subject 1
69

Marks of Subject 1
76

Marks of Subject 1
54

Total Marks :  264

Percentage : 66 %


Division : First

- CBSE
- CBSE Curriculum, CBSE study material, CBSE XI sample papers, CBSE XI previous year question papers
- CBSE textbooks , CBSE books download, CBSE XII sample papers, CBSE XII previous year question papers,
- CBSE IX sample papers , CBSE X sample papers , CBSE X previous year papers, CBSE syllabus, CBSE classes 
- CBSE Computer Sc Question papers, CBSE model papers, CBSE coaching in Govindpuram, Ghaziabad. 
- C++ Classes, JAVA Classes,  C#.NET classes
- C++ in Govindpuram, C++ Coaching in Govindpuram, C++ Tuition in Govindpuram, C++ Classes in Govindpuram
- JAVA in Govindpuram, JAVA Coaching in Govindpuram, JAVA Tuition in Govindpuram, JAVA Classes in Govindpuram
- C#.NET in Govindpuram, C#.NET Coaching in Govindpuram, C#.NET Tuition in Govindpuram, C#.NET Classes in Govindpuram
- English Speaking in Govindpuram, English Speaking Coaching in Govindpuram, English Speaking Tuition in Govindpuram, English Speaking Classes in Govindpuram
- Spoken English in Govindpuram, Spoken English Coaching in Govindpuram, Spoken English Tuition in Govindpuram, Spoken English Classes in Govindpuram
- C++ in Ghaziabad, C++ Coaching in Ghaziabad, C++  Tuition in Ghaziabad, C++ Classes in Ghaziabad
- JAVA in Ghaziabad, JAVA Coaching in Ghaziabad,  JAVA Tuition in Ghaziabad, JAVA Classes in Ghaziabad
- C#.NET in Ghaziabad, C#.NET Coaching in  Ghaziabad, C#.NET Tuition in Ghaziabad, C#.NET Classes in Ghaziabad
- English Speaking in Ghaziabad, English Speaking Coaching in Ghaziabad, English Speaking Tuition in Ghaziabad, English Speaking Classes in Ghaziabad
- Spoken English in Ghaziabad, Spoken English Coaching in Ghaziabad, Spoken English Tuition in Ghaziabad, Spoken English Classes in Ghaziabad
- C++ training in Govindpuram, C++ institute in Govindpuram, C++ jobs in Govindpuram, C++ center in Govindpuram
- C++ training in Ghaziabad, C++ institute in Ghaziabad, C++ jobs in Ghaziabad, C++ center in Ghaziabad
- JAVA training in Govindpuram, JAVA institute in Govindpuram, JAVA jobs in Govindpuram, JAVA center in Govindpuram
- JAVA training in Ghaziabad, JAVA institute in Ghaziabad, JAVA jobs in Ghaziabad, JAVA center in Ghaziabad
- C#.NET training in Govindpuram, C#.NET institute in Govindpuram, C#.NET jobs in Govindpuram, C#.NET center in Govindpuram
- C#.NET training in Ghaziabad, C#.NET institute in Ghaziabad, C#.NET jobs in Ghaziabad, C#.NET center in Ghaziabad
- english speaking course in ghaziabad, english speaking course in ghaziabad,english speaking course in ghaziabad, utter pradesh,english speaking course in ghaziabad,english speaking course in ghaziabad,

No comments:

Post a Comment