0% found this document useful (0 votes)
5 views9 pages

5) Inheritance

The document discusses inheritance in object-oriented programming, detailing its properties, access control, and types such as single, multilevel, multiple, and hybrid inheritance. It provides code examples for various inheritance scenarios, including a Mark class for student grades and a Calculator class for arithmetic operations. The document also includes tasks that demonstrate creating classes with specific functionalities using inheritance.

Uploaded by

or12261618
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

5) Inheritance

The document discusses inheritance in object-oriented programming, detailing its properties, access control, and types such as single, multilevel, multiple, and hybrid inheritance. It provides code examples for various inheritance scenarios, including a Mark class for student grades and a Calculator class for arithmetic operations. The document also includes tasks that demonstrate creating classes with specific functionalities using inheritance.

Uploaded by

or12261618
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

----------inheritance ---------------

def: defining a new class in terms of another class

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

Access Control and Inheritance

Access public protected private


Same class yes yes yes
Derived classes yes yes no
Outside classes yes no no

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

---------- single --------------


#include <iostream.h>
#include<conio.h>
// Base class
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;
}
};

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;
}
};

class PaintCost : public Area{


public:
int cost;
void getCost(){
cost = area * 70;
cout << "Total cost: " <<cost << endl;
}
};
void main() {
Area Rect;
clrscr();
Rect.setWidth(5);
Rect.setHeight(7);

Rect.getArea();
Rect.getCost();
getch();
}

------------------- multiple ------------------------


#include<iostream>
#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;

void getCost(int c){


cost = c;
}
};
class Rectangle : public Area,public PaintCost
{
public:
int charge;
void getPaintingCost(){

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;

void getCost(int c){


cost = c;
}
};
class Rectangle : public Area,public PaintCost
{
public:
int charge;
void getPaintingCost(){
cout<<"\nEnter the Width=";
cin>>width;
cout<<"\nEnter the Height=";
cin>>height;
cout<<"\n Enter the Cost=";
cin>>cost;
getArea(width,height);
getCost(cost);
charge=area*cost;
cout<<"\n Painting Charge="<<charge;
}

};
int main() {
Rectangle Rect;
Rect.getPaintingCost();
return 0;
}

-------------- task ----------------


single:
define a Mark class as base class having variable
tamil,english,maths,chemistry,physics
define Student class as subclass for Mark having id,name,total,percentage and
methode getResult() uses the marks of Mark class to calculate total and percentage

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();
}

--------------- multilevel ------------------


define a Mark class as base class having variable
tamil,english,maths,chemistry,physics
define Cuttoff class as subclass from Mark having cutoffmark variable and method
getCutoff() to calculate cutoff based on maths/2,chemistry/2 and physics/2
define Student class as subclass for cutoff having id,name,total,percentage and
methode getPersonalDetail() ,getResult() uses the marks of Mark class to calculate
total and percentage

#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;

}
};

class Student : public cutoff {


public:
int id;
float total,percentage;
char name[100];
void getPersonalDetail(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;
s.getPersonalDetail(id,name);
s.getMarks(tamil,english,maths,chemistry,physics);
s.getResult();
s.getCutoff();
getch();
}

------------- multiple --------------------


create a addition class having a,b variable and method getAddition() metode for
addition a,b
create a Subtraction class having c,d variable and method getSubtraction() metode
for addition c,d
define Calculator class extends from both addition and subtraction class and use
getAddition(),getSubtraction() to calculations
#include<iostream.h>
#include<conio.h>
class Addition{
public:
int a,b;
void getAddition(){
cout<<"\n--------- Addition -----------";
cout<<"\nEnter the a value:";
cin>>a;
cout<<"\nEnter the b value:";
cin>>b;
cout<<a+b;
}
};
class Subtraction{
public:
int c,d;
void getSubtraction(){
cout<<"\n------------- Subtraction ------------";

cout<<"\nEnter the a value:";


cin>>c;
cout<<"\nEnter the b value:";
cin>>d;
cout<<(c-d);
}

};
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();

You might also like