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.
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 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.
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
|2||int main (int argc, char *argv)|
|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);|
Object Oriented Programming code Example:
|12||int & GetOperand1()|
|16||int & GetOperand2()|
|20||int & GetResult()|
|24||void SetOperand1(int a)|
|26||this–>a = a;|
|28||void SetOperand2(int b)|
|30||this–>b = b;|
|32||void Calculate(char c)|
|36||result = a + b;|
|38||/* Extended functionality */|
|40||result = a – b;|
|43||result = a * b;|
|46||result = a / b;|
|53||using namespace std;|
|54||int main (int argc, char *argv)|
|57||cout << “Calculator demo\n”;|
|58||cout << “First Operand: “;|
|59||cin >> calc.GetOperand1();|
|60||cout << “Second Operand: “;|
|61||cin >> calc.GetOperand2();|
|63||cout << calc.GetOperand1() << ” + “ << calc.GetOperand2() << ” = “;|
|64||cout << calc.GetResult();|
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!