CSCI 3110
Inheritance
Concepts
C++ Inheritance
Kinds of Inheritance
Overriding v.s.
Overloading
Advanced Data Structures
Chapter 8: Advanced C++ Topics
Polymorphism
Static Binding
Dynamic Binding
Comparison
Zhijiang Dong
Dept. of Computer Science
Middle Tennessee State University
Chapter 8: Advanced C++ Topics
CSCI 3110
Inheritance
Concepts
C++ Inheritance
1
Inheritance
Concepts
C++ Syntax of Inheritance
Kinds of Inheritance
Overriding v.s. Overloading
2
Polymorphism
Static Binding
Dynamic Binding
Comparison
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
Chapter 8: Advanced C++ Topics
CSCI 3110
Inheritance
Concepts
C++ Inheritance
1
Inheritance
Concepts
C++ Syntax of Inheritance
Kinds of Inheritance
Overriding v.s. Overloading
2
Polymorphism
Static Binding
Dynamic Binding
Comparison
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
Chapter 8: Advanced C++ Topics
CSCI 3110
Inheritance
Concepts
C++ Inheritance
1
Inheritance
Concepts
C++ Syntax of Inheritance
Kinds of Inheritance
Overriding v.s. Overloading
2
Polymorphism
Static Binding
Dynamic Binding
Comparison
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
Generalization and Specialization
CSCI 3110
Inheritance
In the real world, many objects are specialized versions
of other more general objects.
Concepts
C++ Inheritance
Kinds of Inheritance
Overriding v.s.
Overloading
Insect is a very general type of creature with numerous
characteristics.
Polymorphism
Static Binding
Dynamic Binding
Comparison
Since grasshoppers and bumble bees are insects, they
have all the general characteristics of an insect.
Additionally, they have special characteristics of their
own. The grasshopper has its jumping ability, and the
bumble bee has its stinger.
Grasshoppers and bumble bees are specialized
versions of an insect.
Inheritance and the "Is-a" Relationship
CSCI 3110
Inheritance
Concepts
C++ Inheritance
Kinds of Inheritance
When one object is a specialized version of another
object, there is an "is-a" relationship between them.
Overriding v.s.
Overloading
Polymorphism
Static Binding
A grasshopper is an insect.
Dynamic Binding
Comparison
A poodle is a dog.
A car is a vehicle.
A rectangle is a shape.
Inheritance and the "Is-a" Relationship
CSCI 3110
Inheritance
Concepts
C++ Inheritance
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
When an “is a” relationship exists between classes, it
means that the specialized class has all of the
characteristics of the general class, plus additional
characteristics that make it special.
In object-oriented programming, inheritance is used to
create “is a” relationship between classes.
Inheritance involves two classes: a base class (the
general class), and a derived class (the specialized
class).
You can think of the base class as the parent, and the
derived class as the child.
What a derived class inherits?
CSCI 3110
Inheritance
Concepts
C++ Inheritance
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Every data member defined in the parent class
(although such members may not always be accessible
in the derived class)
Dynamic Binding
Comparison
Every ordinary member function of the parent class
(although such members may not always be accessible
in the derived class!)
What a derived class doesn’t inherit?
CSCI 3110
Inheritance
Concepts
C++ Inheritance
Kinds of Inheritance
Overriding v.s.
Overloading
The base class’s constructors and destructor
Polymorphism
Static Binding
Dynamic Binding
The base class’s assignment operator
Comparison
The base class’s friends
What a derived class can add?
CSCI 3110
Inheritance
Concepts
C++ Inheritance
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
New data members
New member functions
Dynamic Binding
Comparison
New constructors and destructor
New friends
Chapter 8: Advanced C++ Topics
CSCI 3110
Inheritance
Concepts
C++ Inheritance
1
Inheritance
Concepts
C++ Syntax of Inheritance
Kinds of Inheritance
Overriding v.s. Overloading
2
Polymorphism
Static Binding
Dynamic Binding
Comparison
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
Inheritance in C++
CSCI 3110
Inheritance
Concepts
C++ Inheritance
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
class Employee
{
public:
Employee();
Employee(string name, string ssn);
string getName() const;
string getSSN() const;
double getNetpay() const;
void setName( string name );
void setSSN( string ssn );
void setNetPay(double netpay);
void printCheck() const;
private:
string m_name;
string m_ssn;
double m_netpay;
};
Inheritance in C++
CSCI 3110
Inheritance
Concepts
C++ Inheritance
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
class HourlyEmployee : public Employee
{
public:
HourlyEmployee();
HourlyEmployee(string name, string ssn, double wageRate, double hours);
double getRate() const;
void setRate(double wageRate);
double getHours() const;
void setHours(double hoursWorked);
void printCheck();
//This function is overrided
private:
string m_wageRate;
double m_hours;
};
Inheritance in C++
CSCI 3110
Inheritance
Concepts
C++ Inheritance
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
class SalariedEmployee : public Employee
{
public:
SalariedEmployee();
SalariedEmployee(string name, string ssn, double weeklySalary);
double getSalary() const;
void setSalary(double salary);
void printCheck();
//This function is overrided
private:
string m_weeklySalary;
};
public, protected, and private
CSCI 3110
Inheritance
Concepts
C++ Inheritance
Kinds of Inheritance
public data and methods can be used by anyone
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
private data and methods can be used only by
methods and friends of the class
Comparison
protected data and methods can be used only by
methods and friends of both the class and any derived
class
Chapter 8: Advanced C++ Topics
CSCI 3110
Inheritance
Concepts
C++ Inheritance
1
Inheritance
Concepts
C++ Syntax of Inheritance
Kinds of Inheritance
Overriding v.s. Overloading
2
Polymorphism
Static Binding
Dynamic Binding
Comparison
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
Kinds of Inheritance
CSCI 3110
public inheritance
Inheritance
Concepts
C++ Inheritance
Kinds of Inheritance
public and protected members of the base class
remain, respectively, public and protected members of
the derived class.
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
protected inheritance
public and protected members of the base class are
protected members of the derived class
private inheritance
public and protected members of the base class are
private members of the derived class
On all cases, private section of a base class cannot be
accessed by a derived class
Other Class Relationship: “has-a”
CSCI 3110
Inheritance
Concepts
C++ Inheritance
Kinds of Inheritance
Overriding v.s.
Overloading
Has-a relationship
Also called containment
A class has an object as a data member
Cannot be implemented using inheritance
Polymorphism
Static Binding
Dynamic Binding
Comparison
Example: A has-a relationship between a pen and a
ball
class Pen
{
...
private:
Ball point;
};
Other Class Relationship: “As-a”
CSCI 3110
Inheritance
Concepts
C++ Inheritance
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
Use private inheritance
Example: Implement a stack as a list class Stack :
private List
Stack can manipulate the items on the stack by using
List’s methods
The underlying list is hidden from the clients and
descendants of the stack
Private inheritance is useful when
A class needs access to the protected members of
another class, or
If methods in a class need to be redefined
Chapter 8: Advanced C++ Topics
CSCI 3110
Inheritance
Concepts
C++ Inheritance
1
Inheritance
Concepts
C++ Syntax of Inheritance
Kinds of Inheritance
Overriding v.s. Overloading
2
Polymorphism
Static Binding
Dynamic Binding
Comparison
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
Override Member Function
CSCI 3110
Inheritance
A derived class inherits all the member functions of the
base class
Concepts
C++ Inheritance
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
If a derived class requires a different implementation for
an inherited member function, the function can be
redefined in the derived class, called overriding.
Dynamic Binding
Comparison
When a member function is redefined, you must list its
declaration in the definition of the derived class even
though the declaration is the same as in the base class.
If you don’t wish to redefine a member function
inherited from the base class, then it is not listed in the
definition of the derived class.
Overriding v.s. Overloading
CSCI 3110
Inheritance
Concepts
C++ Inheritance
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
When you redefine a function definition, the new
function definition given in the derived class has the
same number and types of parameters.
Static Binding
Dynamic Binding
Comparison
On the other hand, if the function in the derived class
were to have a different number of parameters or a
parameter of a different type from the function in the
base class, then the derived class would have both
functions. This is overloading.
What happens when a derived-class object is
created and destroyed?
CSCI 3110
Inheritance
Concepts
1 Space is allocated (on the stack or the heap) for the full object (that is,
enough space to store the data members inherited from the base class plus
the data members defined in the derived class itself)
C++ Inheritance
Kinds of Inheritance
Overriding v.s.
Overloading
2 The base class’s constructor is called to initialize the data members
inherited from the base class
Polymorphism
Static Binding
Dynamic Binding
Comparison
3 The derived class’s constructor is then called to initialize the data members
added in the derived class
4 The derived-class object is then usable
5 When the object is destroyed (goes out of scope or is deleted) the derived
class’s destructor is called on the object first
6 Then the base class’s destructor is called on the object
7 Finally the allocated space for the full object is reclaimed
Upcast
CSCI 3110
Inheritance
Concepts
C++ Inheritance
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
1 An upcast moves an object or a object pointer up a class hierarchy, from a
derived class to a class it is derived from.
2 An upcast is an implicit conversion
Dynamic Binding
Comparison
3 Reason: “is-a” relationship.
4 Example: A hourly paid employee is an employee, and a salary paid
employee is an employee.
Upcast: Code Example
CSCI 3110
Inheritance
Concepts
C++ Inheritance
// two function prototypes
void print(Employee);
void print(Employee*);
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
int main( void )
{
Employee John("John", "111-11-1111");
HourlyEmployee David("David", "222-22-2222", 13.0, 30);
print(John);
print(&John);
print(David); // X because of upcast
print(&David); // X because of upcast
John = David; // X because of upcast
return 0;
}
Upcast Pitfall – Slicing Problem
CSCI 3110
Inheritance
Concepts
C++ Inheritance
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
Assigning a derived class object to a base class object
slices off data.
Any data members in the derived class object that are
not also in the base class will be lost in the assignment.
Any member function that are not defined in the base
class are similarly unavailable to the resulting base
class object.
Example:
HourlyEmployee David("David", "222-22-2222", 13.0, 30);
Employee John;
John = David;
upcast John.getRate() ; // Compile Error: getRate sliced off
Chapter 8: Advanced C++ Topics
CSCI 3110
Inheritance
Concepts
C++ Inheritance
1
Inheritance
Concepts
C++ Syntax of Inheritance
Kinds of Inheritance
Overriding v.s. Overloading
2
Polymorphism
Static Binding
Dynamic Binding
Comparison
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
Chapter 8: Advanced C++ Topics
CSCI 3110
Inheritance
Concepts
C++ Inheritance
1
Inheritance
Concepts
C++ Syntax of Inheritance
Kinds of Inheritance
Overriding v.s. Overloading
2
Polymorphism
Static Binding
Dynamic Binding
Comparison
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
Static Binding (Earlier Binding)
CSCI 3110
Inheritance
Concepts
C++ Inheritance
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
When a member function is called through an object or
an object pointer, a decision needs to be made: which
“version” of that function should be used – parent’s
version or its own version
If the decision is made at compile time, it is called static
binding (earlier binding)
Static Binding: Examples
CSCI 3110
Inheritance
Concepts
C++ Inheritance
Example 1:
HourlyEmployee John("John", "999-99-9999", 10.00, 40);
John.getName(); // which function is called?
Since getName is not redefined by HourlyEmployee, the version of its
parent Employee is called.
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
Example 2:
John.printCheck(); // which function is called?
Since printCheck is redefined by HourlyEmployee, and the function is
called through a HourlyEmployee object, the version defined by class
HourlyEmployee is called.
Example 3:
Employee David("David", "111-11-1111"); // which function is called?
David.printCheck(); // which function is called?
Since printCheck is called through a Employee object, the version
defined by class Employee is called.
Similar to call member functions through an object
pointer.
Static Binding: Challenge
CSCI 3110
Inheritance
Concepts
Sometimes, static binding is not what we want.
Actually, it may cause disasters.
C++ Inheritance
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Example: Employee *emp;
HourlyEmployee John("John", "999-99-9999", 10.00, 40);
emp = &John; //This is correct since John is an employee
Dynamic Binding
Comparison
emp->printCheck(); // which version is called?
Answer: The version defined in Employee class is
called. Not the one defined in HourlyEmployee as we
expect.
Solution: Dynamic Binding
Chapter 8: Advanced C++ Topics
CSCI 3110
Inheritance
Concepts
C++ Inheritance
1
Inheritance
Concepts
C++ Syntax of Inheritance
Kinds of Inheritance
Overriding v.s. Overloading
2
Polymorphism
Static Binding
Dynamic Binding
Comparison
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
Dynamic Binding
CSCI 3110
Inheritance
Concepts
The decision of which member function version is
called is made dynamically at run time
C++ Inheritance
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Dynamic binding use object’s “real type” to select the
appropriate method that is invoked at run time.
Static Binding
Dynamic Binding
Comparison
To tell system to bind a member function call
dynamically, define the member function as a virtual
method
Virtual member function is selected dynamically (at
run-time) based on the type of the object, not the type
of the pointer/reference to that object
Virtual Method
CSCI 3110
Inheritance
Concepts
C++ Inheritance
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
class Animal
{
public:
virtual void eat() { cout << "I eat like a generic Animal. \n"; }
};
class Wolf : public Animal
{
public:
void eat() { cout << "I eat like a wolf!\n"; }
};
class Fish : public Animal
{
public:
void eat() { cout << "I eat like a fish!\n"; }
};
class OtherAnimal : public Animal { };
Virtual Method
CSCI 3110
Inheritance
Concepts
int main()
{
Animal *anAnimal[4];
C++ Inheritance
anAnimal[0] = new Animal();
anAnimal[1] = new Wolf();
anAnimal[2] = new Fish();
anAnimal[3] = new OtherAnimal();
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
for(int i = 0; i < 4; i++)
anAnimal[i]->eat();
Comparison
}
Output:
I eat like a generic Animal.
I eat like a wolf.
I eat like a fish.
I eat like a generic Animal.
Virtual Method
CSCI 3110
Inheritance
Concepts
C++ Inheritance
A base class Animal could have a virtual function eat.
Subclass Fish would implement eat() differently than
subclass Wolf.
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
But you can invoke eat() on any class instance referred
to as Animal, and get the eat() behavior of the specific
subclass.
This allows a programmer to process a list of objects of
class Animal, telling each in turn to eat, with no
knowledge of what kind of animal may be in the list.
You also do not need to have knowledge of how each
animal eats, or what the complete set of possible
animal types might be.
Virtual Destructor
CSCI 3110
Inheritance
Concepts
C++ Inheritance
If a class has a virtual member function, then it is
always a good idea to declare the destructor virtual.
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
The compiler will perform static binding on the
destructor if it is not declared virtual. This can lead to
problems such as:
When a base class pointer or reference variable refers
a derived class object. If the derived class has its own
destructor, it will not execute when the object is
destroyed or goes out of scope. Only the base class
destructor will execute.
See example Animal.cpp
Virtual Method
CSCI 3110
Inheritance
Concepts
C++ Inheritance
Virtual methods must be class methods, i.e. they
cannot be
Ordinary “stand-alone” functions
class data
Static methods
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Virtual methods have a fixed interface, but derived
implementations can change
Static Binding
Dynamic Binding
Comparison
Supplying virtual keyword is optional when overriding a
virtual method in derived classes.
You can declare a virtual method in any derived class.
Using virtual methods adds a small amount of time and
space overhead to the class/object size and method
invocation time.
Polymorphic behavior is not possible when an object is
passed by value.
Pure Virtual Method
CSCI 3110
Inheritance
Concepts
C++ Inheritance
class Shape
{
public:
virtual double area();
};
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
class Rectangle : public Shape
{
public:
double area();
};
class Triangle : public Shape
{
public:
double area();
};
class OtherShape : public Shape { };
Question: How to implement the function area() in class Shape?
Pure Virtual Method
CSCI 3110
Inheritance
Concepts
C++ Inheritance
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
Define area() as a pure virtual method:
class Shape
{
public:
virtual double area() = 0 ;
};
A pure virtual function is a virtual function that is required to be implemented
by a derived class that is not abstract.
Classes containing pure virtual methods are termed “abstract”.
Abstract classes cannot be instantiated directly.
A subclass of an abstract class can only be instantiated directly if all
inherited pure virtual methods have been implemented by that class or a
parent class.
Pure Virtual Destructors (NOT REQUIRED IN
3110)
CSCI 3110
Inheritance
Concepts
C++ Inheritance
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
The only effect of declaring a pure virtual destructor is
to cause the class being defined to be an abstract
class.
Destructors are not inherited, therefore
A pure virtual destructor in a base class will not force
derived classes to be abstract classes
Nor will any derived class be forced to declare a
destructor
Moreover, you have to implement the pure virtual
destructor in the base class.
Chapter 8: Advanced C++ Topics
CSCI 3110
Inheritance
Concepts
C++ Inheritance
1
Inheritance
Concepts
C++ Syntax of Inheritance
Kinds of Inheritance
Overriding v.s. Overloading
2
Polymorphism
Static Binding
Dynamic Binding
Comparison
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Comparison
When to choose different bindings?
CSCI 3110
Inheritance
Concepts
C++ Inheritance
Kinds of Inheritance
Overriding v.s.
Overloading
Polymorphism
Static Binding
Static Binding
If you are sure that any derived classes will not want to
override this operation dynamically (just redefine and
hide)
Use mostly for reuse or to form "concrete data types"
Dynamic Binding
Comparison
Dynamic Binding
When derived classes may be able to provide a different
(more efficient, more functional) implementation that
should be selected at run time.
Used to build dynamic type hierarchies and to form
“abstract data types”
Static v.s. Dynamic Binding
CSCI 3110
Inheritance
Concepts
C++ Inheritance
Kinds of Inheritance
Efficiency v.s. flexibility are the primary tradeoffs
between static and dynamic binding
Overriding v.s.
Overloading
Polymorphism
Static Binding
Dynamic Binding
Static binding is generally more efficient since
It has less time and space overhead
Comparison
It also enables method inlining
Dynamic binding is more flexible since it enables
developers to extend the behavior of a system
transparently
© Copyright 2026 Paperzz