Object Oriented Programming (OOPs) Concepts with Examples

In Object Oriented Programming (OOP), more emphasis is given to the data rather than functions. In OOPs, Data is encapsulated with the associated functions that operate on it. That gives rise to a concept called Data Encapsulation. In OOPs, the real-life problem can be decomposed into a number of entities called Objects and then data and functions built around these objects.

Features of Object Oriented Programming

1. Emphasis on data rather than algorithm.

2. Programs are divided into objects.

3. Data abstraction introduced.

4. Data and associated functions are tied together into a single unit.

5. Data is hidden and is not accessible to the external functions.

6. Object communication takes place using functions.

7. Further data and functions can be added without disturbing the existing code.

What is an Object?

Objects are the physical and conceptual things that found in the universe. Hardware, Software, documents, human beings and even concepts are all examples of objects. Every object will have data structures called attributes and behavior called operation. Each object will have its own identity through its attributes and operations are same; the objects will never become equal. For instance, in case of student object, two students have the same attributes like name, address, sex and marks but they are not equal. Objects are the basic run-time entities in an object-oriented system.

What is the definition of a Class?

The objects with the same data structure (attributes) and behavior (operations) are grouped into a single entity called Class. All those objects processing similar properties are grouped into the same unit. Example of a class is as below.

Class Students {
private:
int rollNo;
char name[25];
char address[35];
char sex;
float marks;

public:
void getData();
void putData();
};

Each class describes a possible infinite set of individual objects; each object is said to be an instance of its class and each instance of the class has its own value for each attribute but shares the attribute name and operations with other instances of the class. The following points on classes can be noted.

1. A class is a pattern, template or blueprint for a category of structurally identical items.

2. A class is an abstraction of the real-world entities with similar properties.

3. A class is a theme that consists of both a pattern and a mechanism for creating items based on that pattern.

4. The class is an implementation of abstract data type.

Data Encapsulation

Data Encapsulation is the mechanism whereby the implementation details of a class are kept hidden from the user. It is sometimes referred to as data hiding. The user can only perform a restricted set of operations on the hidden members of the class by executing special functions. The advantages of using data encapsulation comes when the implementation of the class changes but the interface remains the same. The concept of data encapsulation is supported in C++ through the use of public, protected and private access modifiers.

#include <iostream>
using namespace std;

class Adder{
public:
// constructor for the class
Adder(int i = 0) {
results = i;
}
		
// interface to outside world
void addNumber(int num) {
results += num;
}
		
// interface to outside world
int getResults() {
return results;
};
		
private:
// using private access modifier hiding data from outside world
int results;
};

int main( ) {
Adder mul;
   
mul.addNumber(10);
mul.addNumber(20);

cout << "Results " << mul.getResults() << endl;
return 0;
}

Inheritance

Inheritance is a process, by which one object can acquire the characteristics from one or more other objects. It allows the declaration and implementation of one class to be based on one or more existing classes. Some object-oriented systems permit only single inheritance – a situation in which the characteristics of an object may be acquired from a single object. Many object-oriented systems allow multiple inheritance – a situation in which the characteristics of an object may be acquired from two or more corresponding objects. Inheritance is the most promising concepts of OOP, which help realize the goal of constructing software systems from reusable parts, rather than hand coding every system from scratch. Inheritance coupled with polymorphism and dynamic binding, minimizes the amount of existing code to be modified while enhancing a system. Read more about types of inheritance.

#include <iostream>
using namespace std;

// Base class for various shapes
class Shapes  {
public:
void setWidth(int wh) {
width = wh;
}
		
void setHeight(int hg) {
height = hg;
}
		
protected:
int width;
int height;
};

// Derived class rectangle from Shapes
class Rectangle: public Shapes {
public:
int getRectangleArea() { 
return (width * height); 
}
};

int main(void) {
Rectangle Rectan;
 
Rectan.setWidth(12);
Rectan.setHeight(17);

// Print the area of the object.
cout << "Total area of the Shape: " << Rectan.getRectangleArea() << endl;

return 0;
}

Polymorphism

Objects are smart enough to be dynamic. Specifically, they have the ability to react differently depending on the situation. By using Polymorphism, an object can be made to handle any scenario with the exact same method name. Thus, depending on what the object is asked to pint, it will be able to print it. Polymorphism refer to the ability of an object to have numerous methods with the same name. This is also referred to as function overloading. An object can have multiple functions with the same name but different parameter lists. The appropriate function to execute in chosen depending on which parameter list is provided in the function call. Instead of having separate functions like PrintPicture(), PrintLetters() and PrintNumber(), one unified function name as print() can be used. The object itself will determine which function in to be invoked, depending on the type of situation.

#include <iostream>
#include <string>

#define PI 3.14159

using namespace std;

//Base class for Shapes
class Shapes 
{
public:
Shapes() {}
double area_calculation() { return 0; }
};

// Rectangle derives from Shapes
class Rectangle : public Shapes
{
public:
double width, height;
Rectan(double wh = 2, double hg = 2)
{
width = wh;
height = hg;
}
double area_calculation()
{
return width*height;
}
};

// Circle derives from Shapes
class Circle : public Shapes
{
public:
double radius;
Circle(double r = 2) { radius = r; }
double area_calculation()
{
return PI*radius*radius;
}
};

int main()
{
Circle cir;
Rect rectan;

Shapes* ptr = &cir; //ptr -> cir
cout << "Circle Area: " << ptr->area_calculation << endl;

ptr = &rectan; //ptr -> rectan
cout << "Rectangle Area: " << ptr->area_calculation << endl;

return 0;
}

Referred From:

Book: Object Oriented Programing using C++
Author: Alok Kumar Jagadev, Amiya Kumar Rath, Satchidananda Dehuri