#Previous question #Index of C++ Questions #Next question

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.

Objects

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

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.

Disadvantages

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);
10    result = a + b;
11    printf ("%d + %d = %d", a, b, result);
12  }

Object Oriented Programming code Example

#include <iostream>
class calculator
{
  private:
  
  int a;
  int b;
  int result;
  
10    public:
11 
12    int & GetOperand1()
13    {
14      return this->a;
15    }
16    int & GetOperand2()
17    {
18      return this->b;
19    }
20    int & GetResult()
21    {
22      return this->result;
23    }
24    void SetOperand1(int a)
25    {
26      this->= a;
27    }
28    void SetOperand2(int b)
29    {
30      this->= b;
31    }
32    void Calculate(char c)
33    {
34      switch(c){
35        case '+':
36          result = a + b;
37          break;
38        /* Extended functionality */
39        case '-':
40          result = a - b;
41          break;
42        case '*':
43          result = a * b;
44          break;
45        case '/':
46          result = a / b;
47          break;
48        default:
49          break;
50      }
51    }
52  };
53  using namespace std;
54  int main (int argc, char *argv[])
55  {
56      calculator calc;
57      cout << "Calculator demo\n";
58      cout << "First Operand: ";
59      cin >> calc.GetOperand1();
60      cout << "Second Operand: ";
61      cin >> calc.GetOperand2();
62      calc.Calculate('+');
63      cout << calc.GetOperand1() << " + " << calc.GetOperand2() << " = ";
64      cout << calc.GetResult();
65  }

Output

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

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

 Vote 0

Similar topics related to this section

object-oriented, C vs C++, C++ vs Java, encapsulation, constructor, overloaded constructor, destructor, destructor overloaded, copy constructor, copy constructor, deep copy, deep copy vs shallow copy, singleton, default access modifier, default access modifier, default access modifier, abstraction, THIS pointer, static function, THIS pointer, a static function, scope resolution operator, inline function, private vs protected, const function, polymorphism, operator overloading, function overloading, overriding, prefix vs postfix, friend class, friend function, sizeof class with virtual, vfptr and vftable, vfptr and vftable using C, early binding and late binding, inheritance, virtual function call from a constructor/destructor, virtual destructor, virtual base class, virtual base class, exception, stack unwinding, exception handling, try-catch block, namespace, mutable variable and const function, mutable keyword, explicit, access a member function, object slicing, ctor sequence of constructor, dtor sequence of destructor, virtual destructor, print the type, dynamic casting, static casting, dynamic and static casting, const casting, reinterpret casting, static and reinterpret casting, template class, template vs macro,

#Object-Oriented Programming in C++ (4th Edition)
#Programming: Principles and Practice Using C++ (2nd Edition)
#The C++ Programming Language, 4th Edition
#Professional C++ Paperback by Nicholas A.Solter, Scott J.Kleper
#Effective Modern C++: 42 Specific Ways to Improve Your Use of C++11 and C++14