0% found this document useful (0 votes)
85 views

Inheritance Polymorphism and Coding Guidelines Content - V1.2

The document discusses inheritance, polymorphism, and coding guidelines in C++. It describes inheritance as a mechanism to create new classes that are similar but not identical to other classes. Inheritance allows for code reusability and easy extension of functionality through overriding virtual functions in derived classes. Runtime polymorphism is implemented using virtual pointer tables to resolve function calls based on the object type. Composition is presented as an alternative to inheritance for code reuse. The document provides examples and guidelines around various inheritance types, issues like the diamond problem, and choosing between inheritance and composition.

Uploaded by

wenad
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)
85 views

Inheritance Polymorphism and Coding Guidelines Content - V1.2

The document discusses inheritance, polymorphism, and coding guidelines in C++. It describes inheritance as a mechanism to create new classes that are similar but not identical to other classes. Inheritance allows for code reusability and easy extension of functionality through overriding virtual functions in derived classes. Runtime polymorphism is implemented using virtual pointer tables to resolve function calls based on the object type. Composition is presented as an alternative to inheritance for code reuse. The document provides examples and guidelines around various inheritance types, issues like the diamond problem, and choosing between inheritance and composition.

Uploaded by

wenad
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/ 6

C++ - Inheritance, Polymorphism and Coding Guidelines

What is the
problem?

Inheritance
Inheritance & Basics Presentation
Benefits inheritance
and
Polymorphism
Inheritance and Assignments
Polymorphism
C++
Inheritance & References
Inheritance Types Polymorphism Codes

Coding
Composition Vs Guidelines
Inheritance Inheritance
Modes and
Composition Vs Types
Inheritance
Coding Guidelines
A product usecase problem
Cisco has a family of router products each one with unique features. Cisco 7500 router has unique
characteristics that distinguish it from a Cisco 4500 router. For instance, it may have more sensitive
environmental characteristics that need to be managed differently; thus, it is necessary to have a series of
management policies dedicated to the Cisco 7500 router class. However, both the Cisco 4500 and 7500 are
manufactured by Cisco and would therefore share vendor-specific MIB information. Additionally, both are
routers and contain management policies dedicated to handling general routing table information.
Design a solution to maintain Cisco router family of products which will allow extension without overhead.
Video #1. Check this video on code repetition

Solution is use

What is Inheritance?
• A mechanism to create new classes which are similar but not identical to other classes.
• Used to model relationship among similar types.
• Object classes located lower in the hierarchical structure inherit from their parents.
• Root class is the base class and other inheriting classes are derived classes.

Inheritance based applications: Cisco’s Router products, Mobile Wireless Fault Mediator
What are the benefits?

Code reusability

Class extension

Benefits
Easy maintenance

Quick development

How does Inheritance help in extending functionality?


• New derived classes can override the base class functions to provide extended features.
• Functions expected to be redefined/overridden (in derived class) is marked with keyword
virtual in base class.
• Derived classes override all or part of the base class virtual functions .
• All derived class objects can be accessed using a base pointer.
• Runtime/dynamic polymorphism using virtual function, vptr and vtable helps to resolve
the address of the function to be invoked based on the address of the object.

class Org // class


{
public:
virtual void Init();
virtual void Display();

};
Class Emp:public Org //derived class
{
public:
void Init(){ cout << “Emp Init”}
void Display(){ cout << “Emp Display”}
};

Coding Guidelines : Virtual


} functions
• Have to be public
• Cannot be static or friend
• Overridden functions may or may not use virtual keyword
• Never override the default parameters of the virtual functions
• Do not override non virtual functions
• In case of shadowed functions (i.e non virtual functions with same name in base and
derived), use scope resolution operator :: to explicitly call base class function
What is Static and Dynamic binding?
Static/compile time/early binding: Function address resolution is at compile time
Dynamic /runtime/late binding: Function address resolution is at runtime and depends
on the type of the object the pointer or reference is bound to. This helps to access objects
of difference classes in inheritance hierarchy.
Behind the hood – How is Runtime polymorphism implemented?
Using vptr and vtable. For more information check this link
Video #2. vptr, vtable
Learn Inheritance using an example code
Can we control access to base class attributes?
Yes, Use access specifiers
Inheritance Types
1. Based on access specifier
2. Based on number of classes/structures

1. Based on access specifier

o Inheritance can be private, public or protected.


▪ If public inheritance →no change in the access specifier.
▪ If protected inheritance → both public and protected members of base become
protected for derived
▪ If private inheritance → both public and protected members of base become private

Video #3. Advanced C++ Inheritance - public protected and private

From how many classes can I inherit?


One or more
2. Based on number of classes/structures

o Single Inheritance
o Multiple Inheritance
o Multilevel Inheritance
o Hierarchical Inheritance
o Hybrid(Virtual) Inheritance

For more details on Inheritance Types

Issues with Inheritance

• Breaks encapsulation
• Constructor and Destructor order is reversed
• Needs a virtual destructor

4/6
• With multiple inheritance, when two super classes of a class have a common base class then
the derived class gets two copies of all attributes. This is known as Diamond problem. Check
the solution here to solve Diamond problem solution
Video #4. Learn Inheritance and Polymorphism through an example

Composition Vs inheritance

Composition
(An alternate to Inheritance
for code reuse)
Choosing Composition Vs Inheritance
• In Composition there is less coupling between reused code and reuser
• In Composition it is either compile or runtime binding whereas in Inheritance it is only
compile time binding.
• Composition is the only solution if the existing class cannot be extended using Inheritance.

Composition code example

CPP Coding Guidelines


Altran C++ Coding Guidelines
Cpp CoreGuidelines

Summary

Use derived classes to extend the based class and to provide specialized/customized handling
Runtime polymorphism is achieved using virtual functions and vtable
In case of pure virtual functions in base class it is mandatory to provide the implementation in derived
class
Using base class pointer we can access all derived class objects

5/6
In case of derived classes, need to have a virtual destructor to do clean up of memory allocated to
base and derived class objects

Reference Codes

Presentation

Assignments

C++ Reference

C++ Libraries

C++ Guidelines

C++ Styleguide

6/6

You might also like