0% found this document useful (0 votes)
27 views

Inheritance: Agnel Institute of Technology & Design

1. The document discusses different types of inheritance in C++ including single, multilevel, multiple, hierarchical, and hybrid inheritance. Code examples are provided to illustrate each type of inheritance. 2. Single inheritance is demonstrated by class B inheriting from class A. Multilevel inheritance is shown with class Result inheriting from class Test, which inherits from class Student. Multiple inheritance is exhibited by class C inheriting from both class A and B. 3. Hierarchical inheritance is portrayed with classes B and C both inheriting separately from class A. Hybrid inheritance is depicted with class Result inheriting from both class Test and Sports. The code examples output the values to demonstrate each inheritance concept.

Uploaded by

Sanat Sks
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Inheritance: Agnel Institute of Technology & Design

1. The document discusses different types of inheritance in C++ including single, multilevel, multiple, hierarchical, and hybrid inheritance. Code examples are provided to illustrate each type of inheritance. 2. Single inheritance is demonstrated by class B inheriting from class A. Multilevel inheritance is shown with class Result inheriting from class Test, which inherits from class Student. Multiple inheritance is exhibited by class C inheriting from both class A and B. 3. Hierarchical inheritance is portrayed with classes B and C both inheriting separately from class A. Hybrid inheritance is depicted with class Result inheriting from both class Test and Sports. The code examples output the values to demonstrate each inheritance concept.

Uploaded by

Sanat Sks
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

AGNEL INSTITUTE OF TECHNOLOGY & DESIGN

INHERITANCE
PROGRAMS:
1. Write a C++ program to illustrate single inheritance.
PROGRAM
#include<iostream>
using namespace std;
class A
{
public: int a;
void geta()
{
cout<<"Enter a:";
cin>>a;
}
};

class B: public A
{
public: int b;
void getb()
{
cout<<"Enter b:";
cin>>b;
}

void disp()
{
cout<<"a="<<a<<"\nb="<<b;
}
};

int main()
{
B b;
b.geta();
b.getb();
b.disp();
return 0;
}

OUTPUT:
Enter a: 15
Enter b: 25

OOPC++ Roll No: 18CO51


AGNEL INSTITUTE OF TECHNOLOGY & DESIGN

Value of a is 15
Value of a is b is 25

2. Write a C++ program to illustrate multilevel inheritance.


PROGRAM:
#include<iostream>
using namespace std;
class student
{
public: int rn;
void get_rollno()
{
cout<<"Enter roll no. of the student: ";
cin>>rn;
}
void disp_rollno()
{
cout<<"\nRoll No: "<<rn;
}
};
class test:public student
{
public: float sub1,sub2;
void get_mks()
{
cout<<"Enter marks of student is subject 1 & subject 2:";
cin>>sub1>>sub2;
}
void disp_mks()
{
cout<<"\nSubject 1: "<<sub1<<"\tSubject 2: "<<sub2;
}
};
class Result: public test
{
public: float total;
void disp()
{

OOPC++ Roll No: 18CO51


AGNEL INSTITUTE OF TECHNOLOGY & DESIGN

total=sub1+sub2;
cout<<"\nTotal: "<<total;
}
};
int main()
{
Result r;
r.get_rollno();
r.get_mks();
r.disp_rollno();
r.disp_mks();
r.disp();
return 0;
}
OUTPUT:
Enter roll no. of the student: 51
Enter marks of student is subject 1 & subject 2: 47 59
Roll No: 51
Subject 1: 47 Subject 2: 59
Total: 106

3. Write a C++ program to illustrate multiple inheritance.


PROGRAM:
#include<iostream>
using namespace std;
class A
{
public: int a, b;
void getab()
{
cout<<"Enter a: ";
cin>>a;
cout<<"Enter b: ";
cin>>b;
}
};

class B

OOPC++ Roll No: 18CO51


AGNEL INSTITUTE OF TECHNOLOGY & DESIGN

{
public: int c;
void getc()
{
cout<<"Enter c: ";
cin>>c;
}
};

class C: public A, public B


{
public: void mult()
{
cout<<"Value of a is "<<a<<endl;
cout<<"Value of a is "<<a<<endl;
cout<<"Value of a is "<<a<<endl;
}
};
int main()
{
C c;
c.getab();
c.getc();
c.mult();
return 0;
}

OUTPUT:
Enter a: 7
Enter b: 4
Enter c: 11
Value of a is 7
Value of b is 4
Value of c is 11
Product:308

4. Write a C++ program to illustrate hierarchical inheritance.


PROGRAM:
#include<iostream>
using namespace std;
class A

OOPC++ Roll No: 18CO51


AGNEL INSTITUTE OF TECHNOLOGY & DESIGN

{
public: int a, b;
void getdata()

{
cout<<"Enter the value of a & b: ";
cin>>a>>b;
}
};
class B: public A
{
public: void sum()
{
cout<<"Value of a is "<<a<<"\nValue of b is "<<b;
cout<<"\nSum: "<<a+b<<"\n\n";
}
};
class C: public A
{
public: void mult()
{
cout<<"Value of a is "<<a<<"\nValue of b is "<<b<<"\nProduct:"<<a*b;
}
};
int main()
{
C c;
B b;
b.getdata();
b.sum();
c.getdata();
c.mult();
}
OUTPUT:
Enter the value of a & b: 31 7
Value of a is 31
Value of b is 7
Sum: 38
Enter the value of a & b: 9 11

OOPC++ Roll No: 18CO51


AGNEL INSTITUTE OF TECHNOLOGY & DESIGN

Value of a is 9
Value of b is 11
Product: 99

5. Write a C++ program to illustrate hybrid inheritance.


PROGRAM:
#include<iostream>
using namespace std;
class student
{
public: int rollno;
void get_rollno()
{
cout<<"Enter roll no. of the student: ";
cin>>rollno;
}
void disp_rollno()
{
cout<<"\nRoll No: "<<rollno;
}
};
class test:public student
{
public: float sub1, sub2;
void get_mks()
{
cout<<"Enter the marks of the student in subject1 & subject 2: ";
cin>>sub1>>sub2;
}
void disp_mks()
{
cout<<"\nSubject 1: "<<sub1<<"\nSubject 2: "<<sub2;
}
};
class sports
{
public: float sports_mks;
void get_sports()
{
cout<<"Enter the sports marks of the student: ";
OOPC++ Roll No: 18CO51
AGNEL INSTITUTE OF TECHNOLOGY & DESIGN

cin>>sports_mks;
}
void disp_sports()
{
cout<<"\nSports Marks: "<<sports_mks;
}
};
class result:public test, public sports
{
public: float total;
void disp_total()
{
total=sub1+sub2+sports_mks;
cout<<"\nTotal: "<<total;
}
};

int main()
{
result r;
r.get_rollno();
r.get_mks();
r.get_sports();
r.disp_rollno();
r.disp_mks();
r.disp_sports();
r.disp_total();
return 0;
}
OUTPUT:
Enter roll no. of the student: 51
Enter the marks of the student in subject1 & subject 2: 63 42
Enter the sports marks of the student: 15
Roll No: 51
Subject 1: 63
Subject 2: 42
Sports Marks: 15
Total: 120

OOPC++ Roll No: 18CO51

You might also like