C++ is an OOP language

Object Oriented Programming

C++ is called object oriented programming (OOP) language and the reason for this is that C++ language views a problem in terms of objects which are involved rather than the procedure for it.

Objects

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

While programming using object oriented approach, the features and specialties of an object are represented by its behavior, data is represented by its functions associated with it. 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:

  • It makes the program less complex
  • enhances readability
  • Components are reusable and extensible

Object oriented programming is always nice for writing large business related logics and large applications or even games. Several components of a large program can be easily extended by introducing new classes or introducing new function/operators. OOPs are 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 different inputs from user and calculates sum and then finally 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 well called as ‘calculator’ and implemented its addition operation. We have extended this class to support other operations like subtract, multiply and division.

Structured Programming code Example

1 #include <stdio.h>
2 int main (int argc, char *argv[])
3 {
4   int a, b, result;
5   printf (“Calculator demo\n”);
6   printf (“First Operand: “);
7   scanf (“%d”, &a);
8   printf (“Second Operand: “);
9   scanf (“%d”, &b);
10   result = a + b;
11   printf (“%d + %d = %d”, a, b, result);
12 }

Object Oriented Programming code Example:

1 #include <iostream>
2 class calculator
3 {
4   private:
5
6   int a;
7   int b;
8   int result;
9
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 of the above pr0gram

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

C++ Vs JAVA

To avoid users from confusion, I also thought to create a differentiation between them:

C++: it is a language dependent on windows platform.
JAVA: it is a platform independent programming language.

C++: files created in C++ are executable.
JAVA: A Java Virtual Machine is required to run programs written in JAVA as it does not create executable files.

Now this was like what actually C++ is and I hope it was beneficial to know before introducing the language itself. Thank you for reading!

Leave a Reply