5) Inheritance
5) Inheritance
properities:
it can access methods and variables of base class
have its own variables and methods
it implements is-a relationship
animal is base class. Dog is a Animal
use:
code sharing
easy maintanance
Public Inheritance − When deriving a class from a public base class, public members
of the base class become public members of the derived class and protected members
of the base class become protected members of the derived class. A base class's
private members are never accessible directly from a derived class, but can be
accessed through calls to the public and protected members of the base class.
Protected Inheritance − When deriving from a protected base class, public and
protected members of the base class become protected members of the derived class.
Private Inheritance − When deriving from a private base class, public and protected
members of the base class become private members of the derived class.
syntax:
class sub-classname : accessspecifier Baseclassname
type:
single => A -> B
multilevel => A -> B -> C
multiple => A,B -> C
hierarchy => Branches of parent class
Hybrid => all type mixed
void main() {
Area Rect;
clrscr();
Rect.setWidth(5);
Rect.setHeight(7);
Rect.getArea();
getch();
}
------------- multilevel ---------------
class Rectangle{
public:
int width;
int height;
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
};
// Derived class
class Area: public Rectangle{
public:
int area;
void getArea() {
area = width * height;
cout << "Total area: " <<area << endl;
}
};
Rect.getArea();
Rect.getCost();
getch();
}
// Derived class
class Area{
public:
int width;
int height;
int area;
void getArea(int w,int h){
width=w;
height=h;
area=width*height;
cout<<"\nArea of rectangle="<<area;
}
};
class PaintCost{
public:
int cost;
charge=area*cost;
cout<<"\n Painting Charge="<<charge;
}
};
int main() {
Rectangle Rect;
clrscr();
Rect.getArea(3,7);
Rect.getCost(30);
Rect.getPaintingCost();
getch();
return 0;
}
second method:
#include<iostream.h>
#include<conio.h>
// Derived class
class Area{
public:
int width;
int height;
int area;
void getArea(int w,int h){
width=w;
height=h;
area=width*height;
cout<<"\nArea of rectangle="<<area;
}
};
class PaintCost{
public:
int cost;
};
int main() {
Rectangle Rect;
Rect.getPaintingCost();
return 0;
}
ans:
#include<iostream.h>
#include<conio.h>
class Mark{
public:
float tamil,english,maths,chemistry,physics;
void getMarks(float t,float e,float m,float ch,float ph){
tamil=t;
english=e;
maths=m;
chemistry=ch;
physics=ph;
}
};
class Student : public Mark{
public:
int id;
float total,percentage;
char name[100];
Student(int i,char n[] ){
id=i;
*name=*n;
}
void getResult(){
total=tamil+english+maths+chemistry+physics;
percentage=(total/500)*100;
cout<<"\nID="<<id;
cout<<"\nName="<<name;
cout<<"\nTamil=" <<tamil;
cout<<"\nEnglish="<<english;
cout<<"\nMaths="<<maths;
cout<<"\nScience="<<chemistry;
cout<<"\nChemistry="<<physics;
cout<<"\nPhysics="<<total;
cout<<"\nPercentage="<<percentage;
}
};
void main(){
int id;
char name[100];
int tamil,english,maths,chemistry,physics;
clrscr();
cout<<"\nEnter the ID:";
cin>>id;
cout<<"\nEnter the Name:";
cin>>name;
cout<<"\nEnter the Tamil Mark:";
cin>>tamil;
cout<<"\nEnter the English Mark:";
cin>>english;
cout<<"\nEnter the Maths Mark:";
cin>>maths;
cout<<"\nEnter the Chemistry Mark:";
cin>>chemistry;
cout<<"\nEnter the Physics Mark:";
cin>>physics;
Student s(id,name);
s.getMarks(tamil,english,maths,chemistry,physics);
s.getResult();
getch();
}
#include<iostream.h>
#include<conio.h>
class Mark{
public:
float tamil,english,maths,chemistry,physics;
void getMarks(float t,float e,float m,float ch,float ph){
tamil=t;
english=e;
maths=m;
chemistry=ch;
physics=ph;
}
};
class cutoff : public Mark
{
public:
float cutoffmark;
void getCutoff(){
cutoffmark=(maths/2)+(chemistry/4)+(physics/4);
cout<<"\nCuttoff : "<<cutoffmark;
}
};
void main(){
int id;
char name[100];
int tamil,english,maths,chemistry,physics;
clrscr();
cout<<"\nEnter the ID:";
cin>>id;
cout<<"\nEnter the Name:";
cin>>name;
cout<<"\nEnter the Tamil Mark:";
cin>>tamil;
cout<<"\nEnter the English Mark:";
cin>>english;
cout<<"\nEnter the Maths Mark:";
cin>>maths;
cout<<"\nEnter the Chemistry Mark:";
cin>>chemistry;
cout<<"\nEnter the Physics Mark:";
cin>>physics;
Student s;
s.getPersonalDetail(id,name);
s.getMarks(tamil,english,maths,chemistry,physics);
s.getResult();
s.getCutoff();
getch();
}
};
class calculator : public Addition,public Subtraction{
};
void main()
{
calculator c;
clrscr();
c.getAddition();
c.getSubtraction();
getch();
}
-----------------------------------------------------------------------------------
-------------------------------------------------------
#include<iostream.h>
#include<conio.h>
class Salary{
public:
float attendance,perdaysalary,overtime,salary,overtimepay;
void getSalary(){
cout<<"\nEnter the attendance:";
cin>>attendance;
cout<<"\nEnter the per day Salary:";
cin>>perdaysalary;
cout<<"Enter the Overtime:";
cin>>overtime;
cout<<"Enter the Overtime pay:";
cin>>overtimepay;
salary=attendance*perdaysalary+overtime*overtimepay;
}
};
class Bonus : public Salary{
public:
float bonus;
void getBonus(){
cout<<"\nEnter the bonus per hour:";
cin>>bonus;
if(attendance>=28){
if(overtime>=50){
cout<<"\nEligible for Bonus!..";
cout<<"\nSalary without Bonus:"<<salary;
salary=salary+(overtime*bonus);
cout<<"\nAfter bonus salary: "<<salary;
}
else
{
cout<<"\nNot eligible bcz Overtime less than 50";
cout<<"\nyour Salary: "<<salary;
}
}
else
{
cout<<"\nNot eligible bcz attendance is less than 28";
cout<<"\nyour Salary: "<<salary;
}
}
};
class Account : public Bonus{
public:
void salaryCalculator(){
clrscr();
getSalary();
getBonus();
getch();
}
};
void main(){
Account jan;
jan.salaryCalculator();