NU-Lec 9 Inheritance
NU-Lec 9 Inheritance
Lecture No. 9
Introduction to Inheritance
Introduction
• Probably most powerful feature of OOP
• Concept similar to inheritance in real life
• New classes are created from existing classes
Introduction
• New classes absorb all features of existing classes
including their data and functions. Also enhance
them by adding their own new features in form of
new data members and new member functions
Introduction
• Existing classes are called base classes
• New classes are called derived classes
• Objects of derived classes are more specialized as
compared to objects of their base classes
Classification Animal
Mammal Reptiles
Human
Dog
.....
man woman
Inheritance
John Mary
Inheritance Examples
Student GraduateStudent
UndergraduateStudent
Shape Circle
Triangle
Rectangle
Loan CarLoan
HomeImprovementLoan
MortgageLoan
Employee FacultyMember
StaffMember
Account CheckingAccount
SavingsAccount
Another example: University’s community member’s
hierarchy
CommunityMember
Single
Employee Student Alumnus inheritance
Single
Faculty Staff inheritance
Multiple
AdministratorTeacher inheritance
Types of inheritance
• public
• private
• protected
Public Inheritance
• With public inheritance, public and protected
members of the base class become respectively
public and protected members of the derived class.
Protected Inheritance
• Public and protected members of the base class
become protected members of the derived class.
Private Inheritance
• With private inheritance, public and protected
members of the base class become private
members of the derived class.
Inheritance Concept
class Rectangle{
Polygon private:
int numVertices;
float *xCoord, *yCoord;
public:
Rectangle
Triangle void set(float *x, float *y, int nV);
float area();
};
class Polygon{ class Triangle{
private: private:
int numVertices; int numVertices;
float *xCoord, *yCoord; float *xCoord, *yCoord;
public: public:
void set(float *x, float *y, int nV); void set(float *x, float *y, int nV);
float area();
}; 15
};
Inheritance Concept
Polygon
class Polygon{
protected:
int numVertices;
float *xCoord, float *yCoord;
public:
void set(float *x, float *y, int nV);
Rectangle };
Triangle
class Rectangle{
protected:
class Rectangle : public Polygon{ int numVertices;
public: float *xCoord, float *yCoord;
float area(); public:
void set(float *x, float *y, int nV);
};
float area(); 16
};
Inheritance Concept
class Polygon{
Polygon
protected:
int numVertices;
float *xCoord, float *yCoord;
public:
void set(float *x, float *y, int nV);
Rectangle };
Triangle
class Triangle{
protected:
class Triangle : public Polygon{ int numVertices;
public: float *xCoord, float *yCoord;
float area(); public:
void set(float *x, float *y, int nV);
};
float area(); 17
};
Inheritance Concept
x
class Point{
Point protected:
y
int x, y;
public:
Circle 3D-Point void set (int a, int b);
x x
};
y y
r z
• Syntax:
class DerivedClassName : access-level BaseClassName
where
– access-level specifies the type of derivation
• private by default, or
• public
• Any class can serve as a base class
– Thus a derived class can also be a base class
19
Class Derivation
21
Access Control Over the Members
class Point{
protected: int x, y;
public: void set(int a, int b);
derived class/ subclass/ };
child class class Circle : public Point{
……
}; 22
Constructor Rules for Derived Classes
The default constructor and the destructor of the base class are
always called when a new object of a derived class is created or
destroyed.
output: A:default
B test(1);
B 23
Constructor Rules for Derived Classes
You can also specify a constructor of the base class
other than the default constructor
class A {
class B : public A {
protected:
public:
int x, y;
void print ()
public:
{cout<<“From B”<<endl;}
void print ()
};
{cout<<“From
A”<<endl;}
26
};
Access a Method
class Point{
class Circle : public Point{
protected:
private: double r;
int x, y;
public:
public:
void set (int a, int b, double c) {
void set(int a, int b)
Point :: set(a, b); //same name function call
{x=a; y=b;}
r = c;
void foo ();
}
void print();
void print(); };
};
Circle C;
Point A; C.set(10,10,100); // from class Circle
A.set(30,50); // from base class Point C.foo (); // from base class Point
A.print(); // from base class Point C.print(); // from class Circle
27
point and circle classes
class Point
{
protected:
int x,y;
public:
Point(int ,int);
void display(void);
};
Point::Point(int a,int b)
{
x=a;
y=b;
}
void Point::display(void)
{
cout<<"point = [" <<x<<","<<y<<"]";
}
class Circle : public Point
{
double radius;
public:
Circle(int ,int ,double );
void display(void);
};
Circle::Circle(int a,int b,double c):Point(a,b) {
radius = c;
}
void Circle::display(void) {
Point::display();
cout<<" and radius = "<<radius;
}
int main(void)
{
Circle c(3,4,2.5);
c.display();
return 0;
}
Output:
point=[3,4] and radius = 2.5
Inheritance Demo
Reading References
• Dietel & Dietel
– Chapter 12, Page 499, 8th Edition
• DS Malik
– Chapter 13, Page 723, 5th Edition
• Robert Lafore
– Chapter 9, Page 371, 4th Edition
• http://www.cplusplus.com/articles/y8hv0pDG/
Note:
Don’t depend on lecture slides until and unless you don’t read book
you cannot have good understanding and concept