Sie sind auf Seite 1von 54

Object-Oriented Programming

Using C++
Third Edition

Understanding Inheritance

Introduction
Inheritance allows to use Re-usability concept.
Once a class is written; it can be used and tested
by other programmers.
Mechanism of deriving new class from old-one is
called Inheritance.
Old class Base class
New class Derived class

Object-Oriented Programming Using C++, Third Edition

Types of Inheritance
A class can Inherit properties in following ways:

Single Inheritance
Multiple Inheritance
Hierarchical Inheritance
Multi-level Inheritance
Hybrid Inheritance

Object-Oriented Programming Using C++, Third Edition

Object-Oriented Programming Using C++, Third Edition

Object-Oriented Programming Using C++, Third Edition

Understanding the Advantages


Provided by Inheritance
Programs in which you derive new classes from
existing classes offer several advantages:
Much of the code is already written
Existing code has already been tested; it is reliable
You already understand how the base class works,
and you can concentrate on writing the
extensions
You can extend and revise a parent class without
corrupting the existing parent class features
If other classes have been derived from the parent
class, the parent class is even more reliable
Object-Oriented Programming Using C++, Third Edition

Defining Derived Class


General form of defining derived class:

Object-Oriented Programming Using C++, Third Edition

Object-Oriented Programming Using C++, Third Edition

Creating a Derived Class

Object-Oriented Programming Using C++, Third Edition

Creating a Derived Class (continued)

You can say every child class object is a parent


class object
For example, every Customer is a Person

The Customer class shown above contains all the


members of Person because it inherits them
Object-Oriented Programming Using C++, Third Edition

10

Creating a Derived Class (continued)

Object-Oriented Programming Using C++, Third Edition

11

Creating a Derived Class (continued)

Object-Oriented Programming Using C++, Third Edition

12

Understanding Inheritance Restrictions

If you replace the original outputBalDue()


function with the one shown above, programs that
use the function no longer run
The private Person field idNum is inaccessible

Object-Oriented Programming Using C++, Third Edition

13

Example (Single level Inheritance)

Object-Oriented Programming Using C++, Third Edition

14

Example (Multiple Inheritance)

Adding members Publicly & Privately


Public Inheritance

Private Inheritance

Understanding Inheritance Restrictions


(continued)

private members can be accessed only within a


class
protected members can be accessed only by
class functions, or by functions in child classes
public members can be accessed anywhere
Object-Oriented Programming Using C++, Third Edition

17

Public derivation
Class base
{
private:
int base_a;
protected:
int base_b;
public:
void base_set();
};
Class derived: public base
{
private:
int derived_a;
protected:
int derived_b;
public:
void derived_set();
};

Class derived: public base


{
private:
int derived_a;
protected:
int base_b;
int derived_b;
public:
void base_set();
void derived_set();
};

Protected derivation
Class base
{
private:
int base_a;
protected:
int base_b;
public:
void base_set();
};
Class derived: protected base
{
private:
int derived_a;
protected:
int derived_b;
public:
void derived_set();
};

Class derived: protected base


{
private:
int derived_a;
protected:
int base_b;
int derived_b;
void base_set();
public:
void derived_set();
};

Private derivation
Class base
{
private:
int base_a;
protected:
int base_b;
public:
void base_set();
};
Class derived: private base
{
private:
int derived_a;
protected:
int derived_b;
public:
void derived_set();
};

Class derived: protected base


{
private:
int derived_a;
int base_b;
void base_set();
protected:
int derived_b;
public:
void derived_set();
};

Understanding Inheritance Restrictions


(continued)
The following are never inherited:

constructors
destructors
friend functions
overloaded new operators

overloaded = operators

Class friendship is not inherited

Object-Oriented Programming Using C++, Third Edition

21

Choosing the Class Access Specifier


(continued)

If you dont use an access specifier when creating


a derived class, access is private by default
A classs private data can be accessed only by a
classs member functions
The inheritance access specifier in derived classes
is most often public
Object-Oriented Programming Using C++, Third Edition

22

Ambiguity Resolution

Overriding Inherited Access

Object-Oriented Programming Using C++, Third Edition

24

Overriding Inherited Access


(continued)

You cannot access protected or


private members of the class

Object-Oriented Programming Using C++, Third Edition

25

Overriding Inherited Access


(continued)
Protected
member is
made Public

Access for showPolicy() is public within


AutomobileInsurancePolicy, even though all
other public members of InsurancePolicy are
protected within the child
Object-Oriented Programming Using C++, Third Edition

26

Overriding and Overloading Parent


Class Functions
Base Class

Receiving 3
parameters

Object-Oriented Programming Using C++, Third Edition

27

Overriding and Overloading Parent


Class Functions (continued)
Derived Class
Receiving 5
parameters in
derived class

Initializes inherited
base class members

Object-Oriented Programming Using C++, Third Edition

28

Overriding and Overloading Parent


Class Functions (continued)
The following version of the setFields()
function requires less work on your part and takes
advantage of code reusability
And, it could be used even if the data fields in
Person were declared to be private rather than
protected
Alternately,
Initializes inherited
base class members

Object-Oriented Programming Using C++, Third Edition

29

Overriding and Overloading Parent


Class Functions (continued)
Base class object
Derived class object

Same outputData()
function is used

Object-Oriented Programming Using C++, Third Edition

30

Overriding and Overloading Parent


Class Functions (continued)

overrides the outputData()


function defined in Person

Object-Oriented Programming Using C++, Third Edition

31

Overriding and Overloading Parent


Class Functions (continued)
You can use the Person version of a function in an
Employee object by using the following syntax
worker.Person::setFields(id,last,first);

If a base class contains a function that the derived


class should not have, you can create a dummy
function with the same name in the derived class

Object-Oriented Programming Using C++, Third Edition

32

Overriding and Overloading Parent


Class Functions (continued)

When any class member function is called, the


following steps take place:
1. The compiler looks for a matching function in the
class of the object using the function name
2. If no match is found in this class, the compiler looks
for a matching function in the parent class
3. If no match is found in the parent class, the compiler
continues up the inheritance hierarchy, looking at
the parent of the parent, until the base class is
reached
4. If no match is found in any class, an error message
is issued

Object-Oriented Programming Using C++, Third Edition

33

Constructors in Derived Class

Object-Oriented Programming Using C++, Third Edition

34

Example
Derived class object created as:

Definition of constructor called:

Providing for Base Class Construction

Object-Oriented Programming Using C++, Third Edition

36

Providing for Base Class Construction


(continued)

When a child class object is instantiated, a


PetStoreItem object is constructed first, and the
PetStoreItem class constructor is called
The PetStoreItem constructor requires arguments
for stockNum and price, so those arguments have
to be provided to the constructor
Object-Oriented Programming Using C++, Third Edition

37

Providing for Base Class Construction


(continued)

Object-Oriented Programming Using C++, Third Edition

38

Providing for Base Class Construction


(continued)

When a derived class object is destroyed the child


class destructor is called first and the base class
destructor is called last

Object-Oriented Programming Using C++, Third Edition

39

Order of execution of Constructors

Object-Oriented Programming Using C++, Third Edition

40

Using Multiple Inheritance


When a child class derives from a single parent,
you are using single inheritance
A child class can also derive from more than one
base class; this is called multiple inheritance
An RV is both a Dwelling and a Vehicle
Multiple inheritance speeds development of the RV
class

Object-Oriented Programming Using C++, Third Edition

41

Using Multiple Inheritance (continued)


Base Class: 1

Constructor

Object-Oriented Programming Using C++, Third Edition

42

Using Multiple Inheritance (continued)


Base Class: 2

constructor

Object-Oriented Programming Using C++, Third Edition

43

Using Multiple Inheritance (continued)


Derived Class

constructor

Object-Oriented Programming Using C++, Third Edition

44

Using Multiple Inheritance (continued)

Object-Oriented Programming Using C++, Third Edition

45

Disadvantages of Using Multiple


Inheritance
Multiple inheritance is never required to solve a
programming problem
For example, the RV class could be written to inherit
from Vehicle, but could contain a Dwelling

If two parent classes contain members with the


same name then you must use the resolution
operator when working with those members
The definition of a class that inherits from a single
parent is almost always easier to understand and
less prone to error
Object-Oriented Programming Using C++, Third Edition

46

Hybrid Inheritance

Virtual Base Class

Here, all public & protected members are inherited


twice in Child class; and introduces ambiguity.
It can be avoided using keyword Virtual while
inheriting the base class.

Virtual Base Class (cont.)

Aggregation
Here object of one class is declared as member of another class
Object of
Date class

class student
class Date
{
{
private:
private:
int id_no;
int month, day, year;
Date dob;
public:
public:
Date(int m, int d, int year)
student(int id, int d, int m, int y)
{
: dob(d,m,y)
Constructor is
month = m;
{
called
day = d;
id_no = id;
year= y;
}
}
}
};

Object Slicing
Base & Derived class
class base
{
public:
int i, j;
};
class derived: public base
{
public:
int k;
};

Main() function
int main()
{
base b;
derived d;
b=d;
}

Object-Oriented Programming Using C++, Third Edition

Derived class
members are
copies one-by-one
into base class

51

Object Slicing (cont.)


class base
{
public:
int i;
int j;
};

copied

class derived: public base


{
public:
int i;
int j;
int k;
};
Gets sliced. Means on
copied to anything

Object Delegation
It is behaviour of object in terms of another object.
Alternative to class inheritance.
Here, 2 objects are involved in handling request:
A receiving object delegates operations to its
delegate.

It is analogous to child classes sending request to


parent classes.

Object-Oriented Programming Using C++, Third Edition

53

Example
A class object
declared

Constructor

Function of class A
called to perform
task

Object of class A
passed in constructor

Das könnte Ihnen auch gefallen