Anda di halaman 1dari 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

Anda mungkin juga menyukai