Inheritance Polymorphism and Coding Guidelines Content - V1.2
Inheritance Polymorphism and Coding Guidelines Content - V1.2
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
};
Class Emp:public Org //derived class
{
public:
void Init(){ cout << “Emp Init”}
void Display(){ cout << “Emp Display”}
};
o Single Inheritance
o Multiple Inheritance
o Multilevel Inheritance
o Hierarchical Inheritance
o Hybrid(Virtual) 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.
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