Object Oriented Programming

C++ is called object oriented programming (OOP) language because C++ language views a problem in terms of objects involved rather than the procedure for doing it.


An object is an identifiable entity with some characteristics and behaviors.

While programming using object oriented approach, the characteristics of an object are represented by its data and its behavior is represented by its functions/operators associated. Therefore, in object oriented programming object represent an entity that can store data and has its interface through functions.


Advantages of OOP Programming are that it makes the program less complex, enhances readability. Components are reusable and extendible. Object oriented programming is always good for writing large business logics and large applications or games. Several components of a large program can be easily extended by introducing new classes or introducing new function/operators. OOPs is also very much desired for maintenance and long term support.


It may not be well suit for small functional problems like adding two numbers or calculating factorial. All the member functions of an object may not be used thus it introduce code overhead.

Source code Example

Below is an example takes two input from user and calculates sum and then prints in console. First block contains source code written in structured programming language i.e. in C. Next to this is the same example written in object oriented way i.e. written in C++. Here we have introduced a class called calculator and implemented its addition operation. We have extended this class to support other operations like subtract, multiply and division.

Structured Programming code Example

#include <stdio.h>
int main (int argc, char *argv[])
  int a, b, result;
  printf ("Calculator demo\n");
  printf ("First Operand: ");
  scanf ("%d", &a);
  printf ("Second Operand: ");
  scanf ("%d", &b);
  result = a + b;
  printf ("%d + %d = %d", a, b, result);

Object Oriented Programming code Example

#include <iostream>
class calculator
  int a;
  int b;
  int result;

  int & GetOperand1()
    return this->a;
  int & GetOperand2()
    return this->b;
  int & GetResult()
    return this->result;
  void SetOperand1(int a)
    this->= a;
  void SetOperand2(int b)
    this->= b;
  void Calculate(char c)
      case '+':
        result = a + b;
      /* Extended functionality */
      case '-':
        result = a - b;
      case '*':
        result = a * b;
      case '/':
        result = a / b;
using namespace std;
int main (int argc, char *argv[])
    calculator calc;
    cout << "Calculator demo\n";
    cout << "First Operand: ";
    cin >> calc.GetOperand1();
    cout << "Second Operand: ";
    cin >> calc.GetOperand2();
    cout << calc.GetOperand1() << " + " << calc.GetOperand2() << " = ";
    cout << calc.GetResult();


Calculator demo
First Operand: 1
Second Operand: 2
1 + 2 = 3

About our authors: Team EQA

You have viewed 1 page out of 62. Your C++ learning is 0.00% complete. Login to check your learning progress.

Learn on Youtube