Computer Programming II Lecture 6

advertisement
Computer Programming II
Lecture 6
Inheritance
- One of the most important concepts in object-oriented
programming is that of inheritance.
- Inheritance allows us to define a class in terms of another
class, which makes it easier to create and maintain an
application. This also provides an opportunity to reuse the
code functionality and fast implementation time.
Inheritance
- When creating a class, instead of writing completely new
data members and member functions, the programmer
can designate that the new class should inherit the
members of an existing class. This existing class is called
the base class(super class), and the new class is referred
to as the derived class(sub class).
- The idea of inheritance implements the is a relationship.
For example, mammal IS-A animal, dog IS-A mammal
hence dog IS-A animal as well and so on.
Base & Derived Classes
•
The relationship between the class is: is a relationship
Inheritance
The syntax :
In order to derive a class from another, we use a colon (:) in the
declaration of the derived class using the following format:
Where derivedclassname is the name of the derived class ,
inheritance_accessspecifier is one of public, protected, or
private. If the access-specifier is not used, then it is private by
default and baseclassname is the name of the class on which it is
based.
Inheritance
Access Control and Inheritance:
- A derived class can access all the non-private members of
its base class. Thus base-class members that should not be
accessible to the member functions of derived classes
should be declared private in the base class.
- We can summarize the different access types according to
who can access them in the following way:
Inheritance
Type of Inheritance:
- When deriving a class from a base class, the base class may
be inherited through public, protected or private
inheritance. The type of inheritance is specified by the
access-specifier as explained above.
- We hardly use protected or private inheritance, but
public inheritance is commonly used.
Inheritance
Inheritance
Example:
Inheritance
- Inheritance allows to create classes which are derived from
other classes, so that they automatically include some of its
"parent's" members, plus its own.
- For example, we are going to suppose
that we want to declare a series of
classes that describe polygons like our
CRectangle, or like CTriangle. They have
certain common properties, such as both
can be described by means of only two
sides: height and base.
- This could be represented in the world
of classes with a class CPolygon from
which we would derive the two other
ones: CRectangle and CTriangle.
Inheritance
Inheritance
What is inherited from the base class?
In principle, a derived class inherits every member of a base
class except:
- Constructors and destructors of the base class.
- Overloaded operators of the base class.
- The friend functions of the base class.
Inheritance
Constructors, Destructors, and Inheritance:
- When an object of a derived class is being instantiated, the
base class constructor is called before the derived class
constructor. When the object is destroyed, the derived
class destructor is called before the base class destructor.
Inheritance
Inheritance
1- Suppose a program has the following class declaration:
Answer the following questions:
Inheritance
A- Suppose another class, Quiz, is derived from the
CheckPoint class. Here is the first line of its declaration:
class Quiz : private CheckPoint
Indicate whether each member of the CheckPoint class is
private, protected, public, or inaccessible:
a
b
c
setA
setB
setC
Inheritance
B - Suppose the Quiz class, derived from the CheckPoint
class, is declared as:
class Quiz : Checkpoint
Is the CheckPoint class a private, public, or protected base
class?
Multiple inheritance
A C++ class can inherit members from more than one
class.
This is done by simply separating the different base classes
with commas in the derived class declaration.
The syntax:
Where access-specifier is one of public, protected, or
private and would be given for every base class and they
will be separated by comma as shown above.
Multiple inheritance
For example, if we had a specific class to print on screen
(COutput) and we wanted our classes CRectangle and
CTriangle to also inherit its members in addition to those
of CPolygon we could write:
class CRectangle: public CPolygon, public COutput;
class CTriangle: public CPolygon, public COutput;
Multiple inheritance
Download