0% found this document useful (0 votes)
13 views32 pages

NU-Lec 9 Inheritance

Uploaded by

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

NU-Lec 9 Inheritance

Uploaded by

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

Computer Programming

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

• Inheritance provides us a mechanism of software


reusability which is one of the most important
principles of software engineering
Inheriting Data and Functions
• All data members (but not private) and member
functions of base class are inherited to derived
class

• Constructors, destructors and = operator are not


inherited
Friend functions and Inheritance
• Friend functions are not inherited

• If class A is derived from class B and fun is friend


function of B

• fun does not become a friend of A automatically


by inheritance, we must make fun as friend of A
explicitly if desirable
Some definitions in class hierarchy
• Direct base class
– Inherited explicitly (one level up hierarchy)
• Indirect base class
– Inherited two or more levels up hierarchy
• Single inheritance
– Inherits from one base class
• Multiple inheritance
– Inheritance from multiple classes
Animals: Class’s hierarchy

Classification Animal

Mammal Reptiles

Human
Dog
.....

man woman
Inheritance

John Mary
Inheritance Examples

Base class Derived classes

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

Administrator Teacher Single


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

class Circle : public Point{ class 3D-Point: public Point{


private: private:
double r; int z;
}; };
18
Define a Class Hierarchy

• 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

Point class Point{


protected:
int x, y;
3D-Point public:
void set (int a, int b);
};
Sphere
class 3D-Point : public Point{ class Sphere : public 3D-Point{
private: private:
double z; double r;
…… ……
}; };
Point is the base class of 3D-Point, while 3D-Point is the base class of Sphere
20
What to inherit?

• In principle, every member (but not private) of a


base class is inherited by a derived class
– just with different access permission

21
Access Control Over the Members

• Two levels of access control


base class/ superclass/ over class members
parent class – class definition
– inheritance type
members goes to
derive from

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.

class A { class B : public A


public: {
A() public:
{cout<< “A:default”<<endl;} B (int a)
A (int a) {cout<<“B”<<endl;}
{cout<<“A:parameter”<<endl;} };
};

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

DerivedClassCon ( derivedClass args ) : BaseClassCon


( baseClass args )
{ DerivedClass constructor body }
class A { class C : public A {
public: public:
A() C (int a) : A(a)
{cout<< “A:default”<<endl;} {cout<<“C”<<endl;}
A (int a) };
{cout<<“A:parameter”<<endl;}
};
output: A:parameter
C test(1);
C 24
Define its Own Members

The derived class can also class Point{


define its own members, in protected:
addition to the members int x, y;
inherited from the base class
public:
x void set(int a, int b);
Point
y };
x
class Circle{
y Circle protected:
r int x, y;
class Circle : public Point{ private:
private: double r;
double r; public:
public: void set(int a, int b);
void set_r(double void set_r(double c);
25
c); };
Even more …

• A derived class can override methods defined in its parent


class. With overriding,
– the method in the subclass has the identical signature to the
method in the base class.
– a subclass implements its own version of a base class method.

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

You might also like