Inheritance: Agnel Institute of Technology & Design
Inheritance: 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
Value of a is 15
Value of a is b is 25
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
class B
{
public: int c;
void getc()
{
cout<<"Enter c: ";
cin>>c;
}
};
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
{
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
Value of a is 9
Value of b is 11
Product: 99
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