C++ Inheritance and Overloading

Download Report

Transcript C++ Inheritance and Overloading

Inheritance- C++
Inheritance in C++
Extending Classes
Ms Kavitesh Sharma, PGT(CS)
KV No.2(AFS) Jodhpur
Inheritance- C++
Overview
•
•
•
•
•
Introduction
Derived and Base Classes
Need for Inheritance
Different Forms of Inheritance
Inheritance and Access Control
Ms Kavitesh Sharma, PGT(CS)
KV No.2(AFS) Jodhpur
Inheritance- C++
INHERITANCE : Introduction
• Inheritance is capability of one class to inherit
properties from another class.
• This property let us to generate a object that is
much closer to real world.
• Inheritance let us to derive new classes ( called
derived classes) fro old ones, with the derived
class inheriting the properties, including the
methods of the old classes, known as base class.
Ms Kavitesh Sharma, PGT(CS)
KV No.2(AFS) Jodhpur
Inheritance- C++
Inheritance: Derived and Base Class
• Objects are often defined in terms of hierarchical classes
with a base class and one or more levels of classes that
inherit from the classes that are above it in the hierarchy.
• For instance, graphics objects might be defined as follows:
Ms Kavitesh Sharma, PGT(CS)
KV No.2(AFS) Jodhpur
Inheritance- C++
Inheritance (continued)
• This hierarchy could, of course, be continued for more
levels.
• Each level inherits the attributes of the above level. Shape
is the base class. 2-D and 3-D are derived from Shape and
Circle, Square, and Triangle are derived from 2-D. Similarly,
Sphere, Cube, and Tetrahedron are derived from 3-D.
Ms Kavitesh Sharma, PGT(CS)
KV No.2(AFS) Jodhpur
Inheritance- C++
Need For Inheritance
• One major reason behind use of inheritance is the
capability to express the inheritance relationship
which ensure the closeness with the real world
models.
• Another reason is the idea of reusability, resulting
in faster development time, easier maintenance,
and easy to extend.
• Last but not least reason is transitive nature of
inheritance i.e. If class B inherits properties of
class A, then all subclasses of class B will
automatically inherits the properties of class A
Ms Kavitesh Sharma, PGT(CS)
KV No.2(AFS) Jodhpur
Inheritance- C++
Different Forms of Inheritance
•
•
•
•
•
Single Inheritance
Multiple Inheritance
Hierarchical Inheritance
Multilevel Inheritance
Hybrid Inheritance
Ms Kavitesh Sharma, PGT(CS)
KV No.2(AFS) Jodhpur
Inheritance- C++
Single Level Inheritance
• When a subclass inherits
only from one base class.
Ms Kavitesh Sharma, PGT(CS)
KV No.2(AFS) Jodhpur
BASE CLASS
X
SUB CLASS
Y
Inheritance- C++
Multiple Inheritance
• When a subclass inherits
from multiple base class.
BASE CLASS
A
SUB CLASS
Ms Kavitesh Sharma, PGT(CS)
KV No.2(AFS) Jodhpur
B
C
Inheritance- C++
Hierarchical Inheritance
• When many subclasses
inherit from a single base
class
BASE CLASS
SUB CLASS
Ms Kavitesh Sharma, PGT(CS)
KV No.2(AFS) Jodhpur
Y
X
z
W
Inheritance- C++
Multilevel Inheritance
• When a subclass inherits
from a class that itself
inherits from another
class.
BASE CLASS OF Y
SUB CLASS OF X
BASE CLASS OF Z
X
Y
Z
SUB CLASS OF Y
Ms Kavitesh Sharma, PGT(CS)
KV No.2(AFS) Jodhpur
Inheritance- C++
HYBRID INHERITANCE
• This form combines two or
more forms of inheritance.
X
Y
z
Q
Ms Kavitesh Sharma, PGT(CS)
KV No.2(AFS) Jodhpur
W
Inheritance- C++
Inheritance (continued)
class A : base class access specifier B
{
member access specifier(s):
...
member data and member function(s);
...
}
Valid access specifiers include public, private, and
protected
Ms Kavitesh Sharma, PGT(CS)
KV No.2(AFS) Jodhpur
Inheritance- C++
Visibility Mode :Public Inheritance
class A : public B
{
// Class A now inherits the members of Class B
// with no change in the “access specifier” for
}
// the inherited members
public base class (B)
public members
protected members
private members
Ms Kavitesh Sharma, PGT(CS)
KV No.2(AFS) Jodhpur
derived class (A)
public
protected
inherited but not
accessible
Inheritance- C++
Visibility Mode : Protected Inheritance
class A : protected B
{
// Class A now inherits the members of Class B
// with public members “promoted” to protected
}
// but no other changes to the inherited members
protected base class (B)
public members
protected members
private members
Ms Kavitesh Sharma, PGT(CS)
KV No.2(AFS) Jodhpur
derived class (A)
protected
protected
inherited but not
accessible
Inheritance- C++
Visibility Mode : Private Inheritance
class A : private B
{
// Class A now inherits the members of Class B
// with public and protected members
}
// “promoted” to private
private base class (B)
public members
protected members
private members
Ms Kavitesh Sharma, PGT(CS)
KV No.2(AFS) Jodhpur
derived class (A)
private
private
inherited but not
accessible
Inheritance- C++
Inheritance (continued)
class Shape
{
public:
int GetColor ( ) ;
protected:
// so derived classes can access it
int color;
};
class Two_D : public Shape
{
// put members specific to 2D shapes here
};
class Three_D : public Shape
{
// put members specific to 3D shapes here
};
Ms Kavitesh Sharma, PGT(CS)
KV No.2(AFS) Jodhpur
Inheritance- C++
Inheritance (continued)
class Square : public Two_D
{
public:
float getArea ( ) ;
protected:
float edge_length;
};
class Cube : public Three_D
{
public:
float getVolume ( ) ;
protected:
float edge_length;
};
Ms Kavitesh Sharma, PGT(CS)
KV No.2(AFS) Jodhpur
Inheritance- C++
Inheritance (continued)
int main ( )
{
Square mySquare;
Cube myCube;
mySquare.getColor ( ); // Square inherits getColor()
mySquare.getArea ( );
myCube.getColor ( );
// Cube inherits getColor()
myCube.getVolume ( );
}
Ms Kavitesh Sharma, PGT(CS)
KV No.2(AFS) Jodhpur
Inheritance- C++
ASSIGNMENT
• Why the concept of inheritance is useful?
• What do you mean by base class and derived
class?
• How many types of inheritance are applicable in
c++? Explain with example?
Ms Kavitesh Sharma, PGT(CS)
KV No.2(AFS) Jodhpur