Giving a definition as simple as it theoretically possible, polymorphism is an ability to treat objects of different types as if they are of a same type. There are several types of polymorphism and different methods of achieving polymorphic behavior.
According to Bjarne Stroustrup, father of C++ language,
polymorphism — providing a single interface to entities of different types. virtual functions provide dynamic (run-time) polymorphism through an interface provided by a base class. Overloaded functions and templates provide static (compile-time) polymorphism.
Breaking down the Stroustrup’s definition, polymorphism can be divided in two categories:
Inheritance is one of the fundamental OOP principles. According to it, class can use variables and methods of another class as its own.
Class that inherits data is called subclass, derived class, or child. Class from which data or methods are inherited is called super class, base class, or parent. Terms “parent” and “child” are extremely helpful in understanding of inheritance. Just as child gets characteristics of his or her parents, derived class gets methods and variables from base class.
Inheritance might be highly useful because it allows to reuse previously written code, which in turn can significantly speed up…
Encapsulation is a set of tools which can restrict access to data or methods that can manipulate it. Detailed definition of encapsulation can be found in my previous Medium post here. This article is focused on examples of encapsulation in C++ and C programming languages.
By default, data and methods in
private — visible and modifiable only by object/class. Access level can be set to other values using appropriate key words provided by C++.
In C++, several access level specifiers are available, and they make data:
public—accessible by class, child classes, and from anywhere outside the class;
The definition of encapsulation is vague, and it differs dramatically from source to source. It is generally agreed that encapsulation is one of the fundamental OOP principles, however, several definitions of them omit encapsulation completely. For example, John C. Mitchell in his Concepts in programming languages states that main OOP language concepts are dynamic lookup, subtyping, inheritance, and abstraction, which is close to encapsulation by meaning, but is generally seen as more broad and high-level concept. On the other hand, Robert C. …
All programming languages can be divided in two major groups: statically or dynamically typed. In spite of some exceptions, the simple rule can be followed:
using namespace std;int main()
int a = 10; // type of 'a' = int
a = 'A'; // type of 'a' = int cout << "Type of 'a' is: " << typeid(a).name() << endl; return 0;