Object Oriented Concepts in C++
C++ evolved from C, by addition of Object Oriented approach. The real world things are modeled with classes and objects. The services objects provide are called functions (or methods). Object Oriented Software approach increases the reliability, adapdibiliy, modifiability, maintability and resuability of the code. The object oriented aproach is based on the concepts of Class, Object, Abstraction, Encapsulation, Inheritance and Polymorphism.
Class
Class is definition or blueprint for objects. It can hold data and functions (or methods). Class can be declared, using the key word class followed by class name (or identifier). The name declaration can precede by access modifier, such as "public", "private" and "protected". Public class is accessible by anyone, where class is visible. Private class is accessible from members of the same class. Protected class is accessible from members of the same class, and derived classes.
Objects
Object is created as an instance of the class. A C++ program will contain cooperative collections of objects, each one of which will be instance of some class. One can create multiple instances from the same class. Consider a class dog with type and behaviours, then particular dog can be a particular object (or instance).
Consider a simple example below which shows how to find the area of two triangles instantiated from a class.
#include <iostream >
using namespace std;
// class declaration
class Triangle {
int x, y;// by default for a class, x and y have private access.
public: // two member functions, values and area have public access.
void values (int,int);
int area () {return (x*y)/2;}
};
void Triangle::values (int a, int b) { // :: is scope operator. It denotes the member of class from outside the class definition.
x = a;
y = b;
}
int main () {
Triangle trifirst, trisecond; // trifirst and trisecond are two instances of class Triangle.
trifirst.values (6,7);
trisecond.values (4,5);
cout << "area of first triangle trifirst: " << trifirst.area() << endl;
cout << "area of second triangle trisecond : " << trisecond.area() << endl;
return 0;
}
Abstraction
Abstraction is the process which only exposes details and allows to hide some of the implementation details, so the focus is on what is necessary. It gives a clear separation between properties of data type and the associated implementation details. There are no instances created with Abstract type of class.
Encapsulation
Encapsulation allows to hide the data, as necessary. Classes are created with data and functions (or methods) to provide encapsulation. Accessibility to data in a class is provided by declaring members as public, private or protected. Encapsultion keep data safe from unwanted external interference and reusability.
Inheritance
It is the technique by which a class inherit any desired properties (data and functionality) of parent class without redefining. The technique simplifies the work of programmer by allowing resuability and better organisation of code. C++ supports multiple inheritances. It occurs when a class is derived from more then one parent class.
Polymorphism
Polymorphism is the mechanism by mean of which same variable, function or object can take various forms, depending upon the type of data passed to it. It is achieved by using inheritance. There are two main types of polymorphism, one is compile time (static or early binding), and other is run time (dynamic or late binding). Two common kind of polymorphism are Overloading and Overriding.