Object Oriented Programming in C++

C++ is an object oriented programming language. So let us first understand what is object oriented programming.

Object Oriented Concepts in C++

The term object oriented means organizing data and behaviour using a collection of objects.

What is an object?

An object is software entity or you can say code which represents anything either conceptual or physical in the real world. For example, we can create an object in C++ for dog, cat, student or a bank transaction or for an account which is a conceptual entity.

So now you know what is an object in C++.

Object oriented programming makes it possible to organize the complex code using classes, objects, inheritance and polymorphism. We are going to discuss about inheritance and polymorphism in detail in later tutorials. Let us understand what is class?

What is class?

A class is a template that can be used to create objects. A class can also be thought of a user defined type that consists of data members and member functions.

What is data member?

A data member is a variable which can be used to store some data value. Since it is the part of class is it a member of that class. So the word data member.
What is a variable?
A variable is a name given to some memory location in your Random Access Memory (RAM).

Look at the code below

// myVariable is a variable name

// if we want to store integer 5 in your RAM temporarily
// we write the following code in C++

myVariable = 5;

// after every line of code in C++ we need to
// write semicolon (;) otherwise we will get an error

// we can perform addition using + symbol
// and store it in variable like above

myVaribale = 5 + 6;

// now myVaribale contains the value 11;
Now you know what is a variable? Variables can also be further classified and hence the data members. We will cover that later.

What is member function?

A block of code statements which perfom a particular task and which may or may not return a value and is a part of the class is called a member function.

For example, if you want to add two numbers (say) 8 and 9 you can write the code like below
myVaribale = 8 + 9;

// now myVaribale contains the value 17
Now what if you are told to write a C++ code to add two number, this time 6 and 3, the next time other two numbers and again upto 1000 times, you will have to write 1000 lines of code.

But functions can save us. We just need to write the code once and every time when we want to do addition we need to tell the function which values to add. For example, consider your friend knows how to do addition, you just give him two numbers and he will tell you the answer. That's it that the job of functions.

The function definition above says, function may or may not return value which mean after performing the addition tell the result to the person who requested it. More on functions in later tutorials.

Example of a class:

Let us consider an example of student

Student
student_id
student_name
student_class
accept_details()
display_details()
modify_details()

student_id, student_name, student_class are data members while accept_details(), display_details(), modify_details() are member functions.

That was just an example. It is not the way you declare or use variables and members of a class in C++.

What is data encapsulation?

The binding of data members and functions into a single unit is called as encapsulation. It is most important feature of a class.

Encapsulation means keeping the data and related functions together in a class. Due to encapsulation, code outside the class in your program cannot directly access the data in a class. Only member functions can access the data members directly in a class.

This insulation of data from direct access by the program is called as information hiding or data hiding. This provides security to data.

What is inheritance in C++?

In object oriented programming inheritance means creating new classes based on existing classes.

If we have written a class named person which contains the following data members and functions

Student
person_name
person_age
accept_details()
display_details()
modify_details()

Now if we want to create a new class student which will also have name, age as data members and accept_details(), display_details() and modify_details() as member functions; assuming we have already written the code for person class. Why to write it again for student? We can just inherit it from person class.

This concept is called as Inheritance. It increasea the reusability of code. There are different types of inheritance that we are going to see in upcoming tutorials.

What is polymorphism?

The name itself suggest, Poly means many and morph means form. So the meaning of word polymorphism is many forms.

Though this concept is bit hard, in simple words polymorphism means the same code exhibits different behaviour at different time.

For example, the code to add two numbers and string is different but we can use polymorphism to decide which code to use depending upon whether the input is two numbers or two strings.

What is the need of object oriented programming?

Object oriented programming is needed to model real world entities very well, to provide better security to the data by using data encapsulation and data hiding. Also it useful for managing the complex codes by organizing them in the form of classes and objects.

So that was the straight forward answer. But to understand this we need to first understand procedure oriented programming.

What is procedure oriented programming?

Type of programming in which sequence of tasks are performed according to some predefined rules is called as procedure oriented programming.

In procedure oriented programming, a program is considered as sequence of tasks to be done and hence more concentration or efforts are put on making the algorithm better.

One might ask what is an algorithm?

An algorithm is a process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer.

Now let's get back procedure oriented programming.

To manage the complexity modular approach is used. As a result, not enough attension is given to the data. This affects the security of data and there comes the first need for object oriented programming.

Programs written using languages like COBOL, FORTRAN, and C use procedure oriented approach.

Disadvantages of Procedure Oriented Programming

  • Due to extensive use of global data or variable, it is very difficult to keep track of which variable is used by which function.
  • Beyond certain limit, procedure oriented programming cannot manage the complexity of problem.
  • If the data structure changes we need to modify all functions that access data.
  • Procedure oriented programming does not provide reusability, maintainability, security, portability etc.

To overcome the disadvantages of Procedure Oriented Programming (POP) we need Object Oriented Programming (OOP) concepts.

Now you know enough about object oriented programming. It is not necessary to read further. But if you want to know the and application areas of object oriented programming (OOP) then need to read it.

Applications of Object Oriented Programming (OOP)

  • Developing GUI (Graphical User Interface)
  • Artificial Intelligence
  • Compiler Construction
  • Operating Systems
  • CAD/CAM Software
  • Communication and Networking Software
  • Simulation and Modeling Software
  • System Applications

Let's summarise what you learned in this article.
  • C++ uses object oriented programming concepts.
  • Object oriented means organizing the data into collection of objects.
  • An object is software entity which models the real world entity. An object is created using class.
  • A class is user defined type that consists of data members and functions.
  • Data encapsulation means binding data and functions together in a single unit called as class.
  • Inheritance means creating new classes based on existing ones.
  • Polymorphism means exhibiting different behaviour at different time depending upon input.
  • Lastly we have seen the need the of object oriented programming and applications.

Now you can try to solve following MCQ on object oriented programming to see what you got from this article

In object oriented programming the focus is on what?
a) procedure
b) Inheritance
c) polymorphism
d) data


Answer:d) data

In procedure oriented programming the focus is on what?
a) function
b) data
c) class
d) objects


Answer:a) function

The insulation of data from direct access from unauthorized functions is called as what?
a) Inheritance
b) class
c) objects
d) data hiding


Answer:d) data hiding

What is the process called in which new class is created based on existing class?
a) Polymorphism
b) Encapsulation
c) Class
d) Inheritance


Answer: d) Inheritance

What is binding of data and functions into a single unit called?
a) Encapsulation
b) Data hiding
c) Information Binding
d) Inheritance


Answer: a) Encapsulation

That's it for this tutorial. Even if you didn't understood a thing from this tutorials it really doesn't mater because everything explained here is going to be convered in detail in the respective tutorials. So there you can better understand with C++ code.

Conclusion:

The whole purpose of this tutorial is to let you know what is there in C++, why C++ is better than other languages like COBOL, FORTRAN, C and why there is need of object oriented programming?

In the next tutorial we are going to see the structure of C++ program and we are going learn how to write, compile and run your first C++ program "Hello World!".