One of the corner stone's of object-oriented programming is inheritance because it creates a different classifications of hierarchical. To make unique the few other specific classes whenever the class is inherited. Super class is called whenever a class in inherited. And the class which is not inherited is named as sub class. Her it is clear that sub class will appear by super class means specialised version. In this type of version its add unique elements whenever an methods and instance variables re inherited.
Example of class hierarchy
Inheritance basics
To inherit a class you simply incorporate the definition of one class into another.
The constructor and destructor of a base class are not inherited the assignment operator is not C++ classes can inherit both data and function members from other classes.
Some of the exceptions to be noted in C++ inheritance are inherited functions and friend classes of the base class are also not inherited.
The protected and public variables or members of the super class are all accessible in the derived class. But a private member variable not accessible by a sub class.
The protected and private members will not accessible outside the class. But a sub class is given access to protected members of the super class.
Exceptions to be noted by C++ inheritance
The non inherited base classes are constructor and destructor
Assignment operator
Friend classes and functions
We have notations to be remembered that in derived class accessible by a base class like members or public/protected variables. But in derived class private member variable will not accessible. Base classes which are declared to be virtual; this is called virtual inheritance. Inheritance graph exists base class that ensures only one instance avoid some ambiguity problems of multiple inheritance. This type of inheritance will not found in most the other languages, which allow derived class more than one base classes and also allow more elaborate inheritance relationships. In derived class, abstract base class normally explicit defined by member functions and not inherited implicitly.
Multiple inheritance
Public inheritance
Public syntax:
class Car : public Vehicle {
public:
...
};
Types of Inheritances
Single inheritance
Syntax: class classname2
{
//code
};
Class classname3: public classname2
{
//code
};
Multiple inheritance
Syntax: class classname1
{
//code
};
Class classname2: public classname1
{
//code
};
Class classname3: public classname2
{
//code
};
Hierarchal inheritance
Syntax: class classname1
{
//code
};
Class classname2
{
//code
};
Class classname3: public classname1,public classname2
{
//code
};
Multi level inheritance
Hybrid inheritance
Virtual function
In case the sub class needs some different functionality for the same functions start stop and run then the super class should implement the concept of virtual functions.
At the time of compilation, the definition that gives a correct function call by default i should match in c++ is named as static binding. And same for dynamic binding but it will find a correct function call at the time of running(run time). Declaration is given by virtual keyword and there is a specific function that dynamic binding will use at the time of compilation.
Problems which solves by the concept of the virtual function:
Whenever a base class is inherited by a derived class, then derived class objects are referred to as derived class type or base type class. If base class overridden by derived class, then the method is ambiguous behavior. This resolves ambiguity by distinguishing non virtual and virtual functions. Functioning of question is designated, derived is called next after base class is called this is in virtual functioning and its reciprocal in non virtual functioning, base is called after derived class is called. This type is called static binding and dynamic binding.
The problems in virtual functioning can be solved by defining programmer firstly declaring functions into base class and then redefined in each derived class. In C++ virtual functions and methods are declared by virtual keywords.
Properties
At run time function call can be resolved.
Usually in this derived class different functionality are appeared
The functions are declared with virtual keyword
It is a class member function.
All the time base class function will be called whenever an function is not declared virtual. During the compile time the function address bound will be statically found. Now function is declared as virtual then he candidate for derived class and run time linking will be invoked.
Abstract class
Abstract class which is designed is specifically used as a base class. It should contain at least one virtual function. A pure virtual function is declare by using a pure specifier (=0).If any class contains at least one abstract function, then such a class is called abstract class.
Syntax:
abstract class classname
{
Member variables
+
Concrete methods
+
Abstract methods
}
An abstract class can have any know of implemented methods and abstract methods. Reference variable of type abstract class can be created but object of type abstract cannot be created. Every abstract class requires subclass. Sub class can inherit all the properties of abstract base class including dummy methods (abstract methods) so from the sub class we must override the abstract methods which are inherited, otherwise derived class becomes abstract class.
Polymorphism:
Poly stands for many and morphism stands for forms
Polymorphism= many terms.
It any function behaves differently within the same class or different classes such a behavior is known as polymorphism
Polymorphism
Compile time Runtime
Dynamic binding
Function operator +
Overloading overloading Abstract methods or virtual methods
Properties:
Default access specifier for this class is private
Property is the member of a class which is used to write the data into the data field or read the data from data field
For the single function or an operator function in different ways must depend up on the proper usage of the function.
Different sub classes should be derived from only single super class
For an example
Consider vehicle as the super class that is only super class
And vehicles tiers and engine are considered as sub class which is many
And derived from only one super class.
The member function should be declared virtually in the super class
From the above example the member functions of super class that is vehicle must be made virtual
Usage
If the program is written with the polymorphism concept then we extend the application easily
There is no need of recompiling the original application but only the relinking is required to have the new changes with the original application
Polymorphism helps in reusability of the code
Using polymorphism applications can be maintained in an easier manner.
Polymorphism helps in achieving robustness in the application
Different kinds of polymorphism
virtual functions
overloading of function name
overloading of operators
Other than these polymorphisms we have other polymorphism
Run time
Time of compilation
Polymorphism Ad-hoc
Polymorphism at run time:
These inheritance and virtual functions are implemented
Compile time polymorphism:
This is implemented with templates.
Ad-hoc polymorphism:
Standard Template Library
In C++ we have a standard fundamental part i.e. Standard template library. STL provides a standard implemented facilities and tools that are used in many applications.
STL is C++ library which contains of
Container classes
Algorithms
Iterators
These provide many data structures and basic algorithms in computer science. Almost every STL components are heavily parameterised in a generic library. We have know about templates before working with STL.
Classification of STL components
Six STL components are below:
Containers
Iterators
Algorithms
Function objects
Utilities
Allocators
This STL document having two indices one is main index and other is divided index these indexes are listing in an alphabetical order and each having their own priority. STL here explains about adaptor which is not included in the above.
Adaptor is a transformation of one interface into another interface. The reason why adaptor is included is it will considered in a stack.
These components are classified in multiple ways.
These components are named as categories. This is a function classification:
Container
Iterator
Algorithm
Function Object
Utility
Adaptor
Allocator.
Next one is classification by structures:
Type (i.e. a struct or class)
Function
Concept
These multi categories are applied to each and every STL component and these schemes were not related to each other just like independent. For the STL component document at top of page these categories are applied. These two categories are applied one at left corner and other is at right corner. But these will always be applied upper side itself. First category to left side and second category to right side.
Divided index
Container
A collection of other objects (its elements) is stored in a container. These collection of objects are implemented as class templates these allows a great flexibility with the types supported in the elements. The container will manages the storage space for its elements and gives a member function to access them either through iterators or directly. Containers replicate structures very commonly used in programming:
dynamic arrays (vector),
queues (queue),
stacks (stack),
heaps (priority queue),
associative arrays
Many shared functionalities and member functions are used by containers. The container classes consist of other objects. It allows memory for elements to access them indirectly/directly by member functions. These classes are implemented with class templates. Containers are of 2 types:
Sequence container
Vector container
Vector is simple form compared to sequence containers. A vector is implemented in dynamic array by its elements. The accessing of elements is done with corresponding index any element can support by vector. In vector arrays allocation of memory is automatic. To hold elements some amount of memory is created by vector container. If we insert elements more than the memory then it automatically increase the memory. It will adds or removes the elements in the memory at the end.
Iterators
Iterator is an object in C++ which pointing to elements in a range of elements which has ability through the elements range using a set of operators like dereference operator (*) and increment operator (++). The pointer is the most obvious iterator form in C++, this pointer can point out any array and can iterate them by using an increment operator (++). There is another form of iterate exists i.e. container type such as a vector. This has an iterator type which has a designed iterate through which the elements are iterated by efficient way. Notice that while a pointer is a form of iterator, then all iterators will not have the same functionality a pointer has;
To distinguish them we are divided them into five categories:
The above five are the iterator categories which are used in the functioning of iterators.
Random access
Bidirectional
Forward
Input
Output
The most limited type iterators are input and output, these are specialised iterators used in performing input and output sequential operations.
By the range of iterate they are limited to one direction which are having functionality of input and output iterator is named as forward iterators.
An iterator is implemented through both directions is named as bidirectional iterator. The iterator of all standard containers will support at least bidirectional iterator types.
Algorithms
A header function is used to design on range of elements is named as algorithm. A sequence of objects range which can be accessed through pointers / iterators, such as an array or instance of some standard container. Containers will work with algorithms. In these container operations like sorting, merging, transformation, searching for contents present in the algorithm. These are present in the linear list and operate in a sequential manner.
Search: it will search in sequential manner.
Transform: which applies to range of elements.
Function objects
The functions whose syntax are similarly designed specially by function objects. In C++, this is achieved by defining member function operator() in their class, like for example:
Example:
struct myclass {
int operator()(int a) {return a;}
} myobject;
int x = myobject (0);
In standard algorithms, the function object is used as predicate and comparison functions. For many function objects standard library provides standard definitions and in some ways to modify and adapt their behaviour in header <functional>:
Utility
In C++ standard library utility will be used as header file. This header file has two main keys they are
​rel_ops​, It is a relational operators(​!=​, ​>​, ​<=​, and ​>=​ ) which sets a default behaviour containing a set of templates between objects of same type, it will based on user defined operators == and <​.
​pair, The two members objects are hold by a container template(1st and 2nd ) of arbitrary types. Additionally, the header defines default relational operators for pair​s which have both types in common.
Allocator
In standard library the classes that defines the memory models which are used by some parts where allocator are classes, and most specifically by STL containers.
An allocator will be defined by following types:
Pointer to type
Const pointer to type
Reference to type
Const reference to type
These types will specify the allocated elements which forms a pointers and references.
Declaration
template < class T > class allocator;