0% found this document useful (0 votes)
17 views38 pages

Inheritance Part 2

Inheritance allows new classes called derived classes to inherit properties from existing classes called base classes. Derived classes inherit all public and protected properties of the base class but not private properties. This allows code reuse and hierarchical relationships where derived classes "are-a" type of base class. Inheritance can be single, where a derived class inherits from one base class, or multiple, where a derived class inherits from more than one base class.

Uploaded by

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

Inheritance Part 2

Inheritance allows new classes called derived classes to inherit properties from existing classes called base classes. Derived classes inherit all public and protected properties of the base class but not private properties. This allows code reuse and hierarchical relationships where derived classes "are-a" type of base class. Inheritance can be single, where a derived class inherits from one base class, or multiple, where a derived class inherits from more than one base class.

Uploaded by

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

1

INHERITA NCE
C++ Programming: From Problem Analysis to Program Design,
2
Fourth Edition

Inheritance
• Inheritance is an “is-a” relationship
• Example: “every employee is a person”
• Inheritance lets us create new classes from existing
classes
• New classes are called the derived classes
• Existing classes are called the base classes
• Derived classes inherit the properties of the base classes
Examples
Base class Derived classes

• Student • Graduate Student


• Undergraduate student

• Circle
• Shape
• Rectangle

• Faculty Member
• Employee • Staff member
C++ Programming: From Problem Analysis to Program Design,
4
Fourth Edition

Inheritance (continued)
• Inheritance can be viewed as a tree-like, or hierarchical,
structure wherein a base class is shown with its derived
classes
Inheritance (continued)
• Single inheritance: derived class has a single base class
• Ex. Circle class from Shape class

• Multiple inheritance: derived class has more than one


base class…will not be discussed in this chapter.
• Ex. Son class from Mother class and Father class

• Public inheritance: all public members of base class are


inherited as public members by derived class
refercence: www.netacad.com

Defining a simple subclass (1)


We can use each class as a base (or a foundation) to define or build another
class (a subclass). It’s also possible to use more than one class to define a
subclass. You can see both of these cases on the right →
Note that the arrows always point to the superclass(es).

The left diagram illustrates a “single inheritance”, and the right one a
“multiple inheritance” or “multi-inheritance”.
We’ll show you some examples of both types of inheritance.
We can also write about super classes as base classes, and subclasses
as derived classes.
refercence: www.netacad.com
refercence: www.netacad.com

Defining a simple subclass (2)


The class on the right →
will serve as a
superclass. Analyse its
structure – it’s not difficult,
we promise.
The program emits the
following text:

101
C++ Programming: From Problem Analysis to Program Design,
9
Fourth Edition

Inheritance (continued)
• General syntax of a derived class:

• Where memberAccessSpecifier is public,


protected, or private (default)
• The private members of a base class are private to
the derived class
• Derived class cannot directly access them
• Ex. Class SonClass: public FatherClass
{ };
refercence: www.netacad.com

Defining a simple subclass (3)


If we want to define a class named Y as a subclass of a superclass named X, we use
the following syntax →

The difference from the notation we used before is in the fact that we have to:
place a colon after the subclass class name
optionally place a so-called visibility specifier (we’ll return to this soon)
add a superclass name
If there’s more than one superclass, we have to enlist them all using commas as
separators, like this:
class A : X, Y, Z { … };
Let’s start with the simplest possible case.
refercence: www.netacad.com

Defining a simple subclass (4)


Take a look here → We’ve
defined a class named Sub,
which is a subclass of a class
named Super. We may also
say that the Sub class is
derived from the Super class.
The Sub class introduces
neither new variables nor new
functions. Does this mean that
any object of the Subclass
inherits all the traits of
the Super class, being in fact a
copy of the Super class’s
objects?
No. It doesn’t.
refercence: www.netacad.com

Defining a simple subclass (4) cont’d


If we compile the following code, we’ll get nothing but compilation errors
saying that the put and get methods are inaccessible. Why?
When we omit the visibility specifier, the compiler assumes that we’re
going to apply a “private inheritance”. This means that all public
superclass components turn into private access, and private
superclass components won't be accessible at all. It consequently
means that you’re not allowed to use the latter inside the subclass.
This is exactly what we want now.
refercence: www.netacad.com

Defining a simple subclass (4) cont’d


We have to tell the compiler that we want to preserve the
previously used access policy. We do this by using a
“public” visibility specifier:
class Sub : public Super { };
Don’t be misled: this doesn’t mean that the private
components of the Superclass (like the storagevariable) will
magically turn into public ones. Private components will
remain private, public components will remain public.
refercence: www.netacad.com

Defining a simple subclass (5)


Objects of the Sub class may
do almost the same things as
their older siblings created from
the Superclass. We use the
word ‘almost’ because being a
subclass also means that the
class has lost access to the
private components of the
superclass. We cannot write a
member function of
the Sub class which would be
able to directly manipulate
the storage variable.
refercence: www.netacad.com

Defining a simple subclass (5) cont’d


This is a very serious restriction. Is there any workaround?
Yes.
There’s the third access level we haven’t mentioned yet. It’s
called “protected”.
The keyword protected means that any component marked
with it behaves like a public component when used by
any of the subclasses and looks like a private
component to the rest of the world.
We should add that this is true only for publicly inherited
classes (like the Super class in our example previous
example)
Let’s make use of the keyword right now.
refercence: www.netacad.com

Defining a simple subclass (6)


As you can see in the example code → we’ve
added some new functionality to the Sub class.
We’ve added the print function. It isn’t
especially sophisticated, but it does one
important thing: it accesses
the storage variable from the Superclass. This
wouldn’t be possible if the variable was
declared as private.
In the main function scope, the variable
remains hidden anyway. You mustn’t write
anything like this:
object.storage = 0;
The compiler will be very stubborn about this.
We almost forgot to mention that our new
program will produce the following output:
storage = 101
x objA
Base y

z
printA
setA

var1
objB
derived
var2

printB
setB
A
x
y

printA
setA
x objA
y

z
printA
setA

var1 objB
Can not access x, y
var2

printB
setB
printA
setA
x objA
y

z
printA
setA

var1 objB

var2

printB
setB
printA
setA
var1 objB

var2

printB
setB
printA
setA
refercence: www.netacad.com

Defining a simple subclass (7)


Now’s a good opportunity to do a little summarising here.
We know that any component of the class may be declared
as:
• public
• private
• Protected
These three keywords may also be used in a completely
different context to specify the visibility inheritance model.
So far, we’ve talked about public and private keywords
used in such a case. It should be no surprise to you that
the protected keyword can be employed in this role, too.
C++ Programming: From Problem Analysis to Program Design,
22
Fourth Edition

Inheritance (continued)
• public members of base class can be inherited as
public or private members

• The derived class can include additional members--data


and/or functions

• The derived class can redefine the public member


functions of the base class

• All members of the base class are also member variables


of the derived class
refercence: www.netacad.com

Defining a simple subclass (7) cont’d


Take a look at the table here →
It gathers all of the possible
combinations of the component
declaration and inheritance model,
presenting the resulting access to
the components when the
subclass is completely defined.
It reads in the following way (take
a look at the first row): if a
component is declared as public
and its class is inherited as public,
the resulting access is public.

Familiarize yourself with the table


– it’s a basic tool to resolve all the
issues regarding the inheritance of
the class components.
Redefining (Overriding) Member
Functions of the Base Class
• To redefine (override) a public member function of a
base class

• Corresponding function in the derived class must have the


same name, number, and types of parameters. Redefined

• If the function has a different signature, this would be function


overloading.
Redefining (Overriding) Member Functions
of the Base Class (continued)
If derived class overrides a public member function of the
base class, then to call the base class function, specify:
• Name of the base class
• Scope resolution operator (::)
• Function name with the appropriate parameter list
var1

var2

print
setB
A::print
setA
C++ Programming: From Problem Analysis to Program Design,
27
Fourth Edition

Constructors of Derived and Base


Classes
• Derived class constructor cannot directly access
private members of the base class
• Derived class can directly initialize only public member
variables of the base class
• When a derived object is declared
• It must execute one of the base class constructors
• Call to base class constructor is specified in heading of
derived class constructor definition
Example
Example
Example

C++ Programming: From Problem Analysis 30


to Program Design, Third Edition
Initialization List and Inheritance

objB

3 x

4 y

Initialization list goes


with constructor of
derived class and
uses class name to
pass parameters to
base class
constructor.
C++ Programming: From Problem Analysis to Program Design,
32
Fourth Edition

Protected Members of a Class


• Private members of a class cannot be directly accessed
outside the class
• For a base class to give derived class access to a
private member
• Declare that member as protected
• The accessibility of a protected member of a class is in
between public and private
• A derived class can directly access the protected member of the
base class
C++ Programming: From Problem Analysis to Program Design,
33
Fourth Edition

Inheritance as public, protected, or


private

• If memberAccessSpecifier is public:
• public members of A are public members of B and can be
directly accessed in class B
• protected members of A are protected members of B and can
be directly accessed by member functions (and friend functions) of
B
• private members of A are hidden in B and can be accessed by
member functions of B through public or protected members of
A
C++ Programming: From Problem Analysis to Program Design,
34
Fourth Edition

Inheritance as public, protected, or


private (continued)

• If memberAccessSpecifier is protected:
• public members of A are protected members of B and can
be accessed by the member functions (and friend functions) of
B
• protected members of A are protected members of B and
can be accessed by the member functions (and friend
functions) of B
• private members of A are hidden in B and can be accessed
by member functions of B through public or protected
members of A
C++ Programming: From Problem Analysis to Program Design,
35
Fourth Edition

Inheritance as public, protected, or


private (continued)

• If memberAccessSpecifier is private:
• public members of A are private members of B and can be
accessed by member functions of B
• protected members of A are private members of B and
can be accessed by member functions (and friend functions) of
B
• private members of A are hidden in B and can be accessed
by member functions of B through public/protected
members of A
refercence: www.netacad.com

Defining a simple subclass (8)


We’ll finish our current topic with a very simple example
demonstrating multi-inheritance. We need to emphasize
that using this technique is commonly recognized as error-
prone and obfuscating class hierarchy.

Any solution that avoids multi-inheritance is generally better


and in fact many contemporary object programming
languages don’t offer multi-inheritance at all. We think it’s a
good argument to consider when you’re making design
assumptions.
refercence: www.netacad.com

Defining a simple subclass (8) cont’d


This example should
be clear (we hope).

The program will


produce the following
output:

storage = 3

safe = 5

You might also like