Você está na página 1de 24

Programming in C++

CST-152
UNIT-2
Inheritance
Chapter- 5
Course Objectives
•To gain knowledge of Inheritance and its various types.

• To understand to concept of function overriding.

University Institute of Engineering


Contents
• Defining derived class
• Modes of inheritance
• Types of inheritance
• Ambiguity in inheritance
• Virtual base class
• Function overriding
• Order of execution of constructors
• Member Classes: Nesting of Classes

University Institute of Engineering


Inheritance
It is the Process of creating a New class from an existing class.
The Existing class is called Base or Parent class. The New class
is called as Child or Derived Class.
Advantages
• It permits code reusability. So, save time and increase the
program reliability.
•A programmer can use a class created by another person or
company without modifying it derive other classes from it that
are suited to particular situations
•Improve Program Reliability
•It Permits code sharing
University Institute of Engineering
Advantages
•It permits code reusability. So, save time and increase the program
reliability.
•A programmer can use a class created by another person or
company without modifying it derive other classes from it that are
suited to particular situations
•Improve Program Reliability
•It Permits code sharing
•The base class need not be changed but can be adapted to suit the
requirements in different applications.

University Institute of Engineering


Cont..
•Inheritance is the property that allows the reuse of an existing
class to build a new class

Base Class Derived class


Methods Base class methods
and +
Properties Additional methods

Class <derived classname> : public


<Base classname>
{
---
};

University Institute of Engineering


Types Of Inheritance

University Institute of Engineering


Single and Multiple
Inheritance
•A class can be derived from a single base class is called single
inheritance

Single Inheritance Multiple Inheritance

Base Class Class A Class B

sub Class
Class C
University Institute of Engineering
Example of Single
#include <iostream.h>
Inheritance
class Rectangle: public Shape
// Base class {
class Shape public:
{ int getArea()
public: {
return (width * height);
void setWidth(int w)
}
{ };
width = w;
} int main(void)
void setHeight(int h) {
{ Rectangle Rect;
height = h;
} Rect.setWidth(5);
Rect.setHeight(7);
protected:
int width; // Print the area of the object.
int height; cout << "Total area: " << Rect.getArea() <<
}; endl;

return 0;
University Institute of Engineering Output:
} Total area: 35
Multilevel and
Hierarchical Inheritance
Multilevel Inheritance Hierarchical Inheritance

Class A Base
Class

Class B
sub sub sub
Class 3 Class 1 Class 2
Class C
University Institute of Engineering
Example of Multilevel
#include<iostream.h>
#include<conio.h> Inheritance
class publications void getdata()
{ {
private : publications::getdata1();
int book_no; cout<<"Enter author name";
public : cin>>author_name;
void getdata1() }
{ void show()
cout<<"\n"<<"Enter Book Number:"; {
cin>>book_no; cout<<book_no<<anthor_name;
} }
void putdata() };
{ class deri:public author
cout<<book_no<<endl; {
} };
};
class author: public publications void main()
{ {
private : deri d;
char author_name[20]; d.getdata();
public : d.putdata();
getch();
University Institute of Engineering }
Example of Hierarchical
#include<iostream.h>
class employee
Inheritance
void getdata()
{ {
private : employee::getdata();
int empno; cout<<"Enter Basic Salary \n";
char empname[20]; cin>>basic;
public : }
void getdata(){
cout<<"\n Enter Employee No"; void putdata()
cin>>empno; {
cout<<"\n Enter Employee name "; da=0.30*basic;
cin>>empname;} hra=0.15*basic;
void putdata(){ cca=0.05*basic;
cout<<"\n Employee No"<<empno; pf=0.12*basic;
cout<<"\n Employee name "<<empname;} special_allowances=0.4*basic;
}; salary=basic+da+hra+cca+special_allowances-pf;
class manager :public employee employee::putdata();
{ cout<<endl<<salary;
private : }};
float basic; class supervisor : public employee
public : {
float salary; private :
float da; float basic;
float hra; public :
float cca; float salary;
float pf; float da;
float special_allowances; float hra;
float cca;
float pf;
University Institute of Engineering float special_allowances;
Cont..
void getdata()
void getdata()
{
{
employee::getdata();
employee::getdata();
cout<<"Enter Basic Salary \n";
cout<<"\n Enter wages & incentives";
cin>>basic;
cin>>wages>>incentive;
}
}
void putdata()
void putdata()
{
{
da=0.30*basic;
salary=wages+incentive;
hra=0.15*basic;
cca=0.05*basic;
employee::putdata();
pf=0.12*basic;
special_allowances=0.4*basic;
cout<<endl<<salary;
salary=basic+da+hra+cca+special_allowances-
}
pf;
};
employee::putdata();
void main()
cout<<endl<<salary;
{
}
manager m;
};
supervisor s;
class worker :public employee
worker w;
{
clrscr();
public :
m.getdata();
float salary;
s.getdata();
float wages;
w.getdata();
float incentive;
w.putdata();
University Institute of Engineering s.putdata(); }
Hybrid Inheritance
•Hybrid is nothing but the combination of Multilevel and
multiple Inheritance

Lecturer

Multi level
Inheritance
Marks Department

Student
Multiple
Inheritance

University Institute of Engineering


Example of Hybrid
#include<iostream.h>
class lecturer
Inheritance
void putdata()
{
{
private :
cout<<"Department name is "<<department_name;
char lecturer_name[20];
}
public :
};
void getdata()
class marks
{
{
cout<<"Enter Lecturer name";
public :
cin>>lecturer_name;
int mark1;
}
int mark2;
void putdata()
int mark3;
{
cout<<"lecturer name is "<<lecturer_name;
void getdata()
}
{
};
cout<<"Enter the marks for 3 subjects";
class department : public lecturer
cin>>mark1>>mark2>>mark3;
{
}
private :
void putdata()
char department_name[20];
{
public:
cout<<"The marks for 3 subjects are
void getdata()
"<<mark1<<"\t"<<mark2<<"\t"<<mark3;
{
}
cout<<"Enter Department name";
};
cin>>department_name;}

University Institute of Engineering


class student:public department,public marks
Cont..
{
private:
int roll_no;
char student_name[20];
public:
void getdata()
{
department::getdata();
cout<<"Enter the student name";
cin>>student_name;
cout<<"Enter the student Enrollment no";
cin>>roll_no;
marks::getdata();
}
void putdata()
{
department::putdata();
cout<<"The name & rollno is
"<<student_name<<"\t"<<roll_no;
marks::putdata();
}
};
void main()
{
student s;
clrscr();
s.getdata();
s.putdata();
getch();}

University Institute of Engineering


Multiple Inheritance

It is the process of creating new class from more than one base
classes.
Syntax :
class <derived class >:<access specifier>
base_class1,<access specifier> base_class2...
{
private :
// members;
protected :
// members;
public :
//members;};

University Institute of Engineering


Cont..
#include <iostream.h> class Rectangle: public Shape, public PaintCost {
class Shape { public:
public: int getArea() {
void setWidth(int w) return (width * height);
{ }
width = w; };
} int main(void)
{
void setHeight(int h)
Rectangle Rect;
{ int area;
height = h; Rect.setWidth(5);
} Rect.setHeight(7);
protected: area = Rect.getArea();
int width; // Print the area of the object.
int height; cout << "Total area: " << Rect.getArea() << endl;
};
// Print the total cost of painting
class PaintCost {
cout << "Total paint cost: $" <<
public: Rect.getCost(area) << endl;
int getCost(int area) {return area * return 0;
70; } }; }
Output:
University Institute of Engineering Total area: 35
Total paint cost: $2450
Ambiguity in Multiple
#include<iostream.h> Inheritance
class deri:public base1,public base2
class base1 {
{ };
public:
int i;
void main()
void disp()
{ {
cout<<"base1 value"<<i<<"\n"; deri d;
} d.i=10;
}; RESULT :
d.disp();
class base2 AMBIGUITY
{ clrscr();
ERROR
public: /*deri d;
int i; OCCURRED
d.base1::i=10;
void disp()
d.base2::i=20;
{
cout<<"base2 value"<<i; d.base1::disp();
} d.base2::disp();
}; getch();
}
University Institute of Engineering
Friend Function
• A friend function of a class is defined outside that class' scope but it has the right to
access all private and protected members of the class. Even though the prototypes for
friend functions appear in the class definition, friends are not member functions.
class Box {
double width;
public:
double length;
friend void printWidth( Box box );
void setWidth( double wid );
};
• To declare all member functions of class ClassTwo as friends of class ClassOne,
place a following declaration in the definition of class ClassOne:
friend class ClassTwo;

University Institute of Engineering


Cont..
#include <iostream.h>
class Box { void printWidth( Box box )
{
double width; /* Because printWidth() is a friend of Box, it
public: can
friend void printWidth( Box box ); directly access any member of this class */
cout << "Width of box : " << box.width
void setWidth( double wid ); <<endl;
}; }
// Member function definition int main( )
{
void Box::setWidth( double wid ) Box box;
{ // set box width with member function
box.setWidth(10.0);
width = wid;
// Use friend function to print the wdith.
} printWidth( box );
// printWidth() is not a member function of return 0;
any class. }

University Institute of Engineering


References

• https://www.cse.unr.edu/~bebis/CS308/PowerPoint/Inheritance.
ppt
• https://cs.wmich.edu/~rhardin/Spring04/cs112/lecturespecial.ppt
• www.cs.cityu.edu.hk/~lwang/ccs5253/lab3.ppt
• www.utdallas.edu/~john.cole/CS1Slides/CS1Lesson15-
Inheritance.pptx

University Institute of Engineering


Course Outcomes

•Understand the concept of inheritance.

• To understand the concept of function overriding .

University Institute of Engineering


Thank You!

University Institute of Engineering

Você também pode gostar