Object Oriented Programming in C++

In this we will discuss POPs , OOPs and their features, as follows-

 

Introduction

As we know, C ++ is the updated version of C but when C language was already there, then why was there a need for C ++. The main difference in C and C ++ is OOPs.

C is a POP (Procedure Oriented Programming) language whereas C ++ Object Oriented Programming language however C ++ supports both OOPs and POPs whereas C only supports POPs.

When the OOPs feature was added to C, it was named C ++. Before programming in OOPs, we get to know about POPs and OOPs so that you will get to know why a new language C ++ was felt.

 

Procedure Oriented Programming (POP)

POP types have language set of function or collection of function. In the language that is based on this type of programming technique, a single program is divide into a function. Each of these functions has a statement to perform a task.

But when the number of function starts increasing in a single program or the size of the program starts increasing, in such a situation it is not easy to manage such a large code and as such a global data can be accessed by all the function declare in the program. But in large number of functions where many functions access the same data, then problems like data loss or data flow arise.
And by which the program does not give a suitable result, in other word, we can say there are bugs or errors in the program.

It is not easy to debug such a large program, which is devide into many functions, we have to determine each function, which is a complex process. C, Pascal, cobol are examples of POPs types of languages.

 

Object Oriented Programming (OOP)

Just as POPs are based language, set of function, just like OOPs are based language set of object, in this, programs are devide into object to perform real world problem or task!

The object is a collection of data members and member functions that bind together logically as one set, called a class, that is the code for a task in these data members and members function. These members cannot be accessed from outside the world or any external function. In a way the data remains secure, and because the function is accessed from the same class object, the chance of data flow in a large program is also less. goes

OOPs also provide the facility to reuse class members. C++, Java, smalltalk languages ​​support OOPs Programming. Because C++ supports both POP and OOP, so this is not a pure OOPs type of language.

This way you can understand why the need for C++ language was felt. As mentioned earlier, c ++ supports both POP and OOP, meaning you can program it in both POPs and OOPs so that so many programs in this tutorial have been made so that only the syntex is different
Beyond this, we will create the same programs using OOPs technique. Before this we need to understand the other features of OOPs.

The features are as follows:

 

class

class is a user defined data type. You can say that this is an updated version of the structure, just as C ++ is an updated version of C. So if your concept of structure is clear, then you can understand it easily.

Like members of class, structure, there is a collection of data members and function members, these members can be both type of data type (int, float, char) and derive data type (array, function). In which data members and function members are logically related to each other. These data members are only accessed by the function members of this class, that is, the data members cannot be accessed from outside the class. Which provides data security in a way.

The syntex of a class is the same as the syntex by default structure.

SYNTEX:

class class-name
 {
   data-member;
   function-member;
 };

In which class-name is an identifier like structure i.e. any unique name (except reserved keyword)

By using this syntex of class, we can create a class but cannot implement it, means if we create a class by taking this syntax, then there will be no error in it, but no output will also come.

Therefore there are some access specifier in class. Using which we implement a class. so new syntex is-

 class class_name
 {
   private:    //access specifier
     data member;
     function member; 
   public:    // access specifier
     data member;
      function member;
};

We will read about it further in class and object.

When we use any class in the program, we get other features of OOPs such as encapsulation, data abstraction, data hiding. Let’s discuss about them,

 

Object

Just as an object has its own property or behavior in the real world, even in OOPs, the property of an object, the function-member of the class, and the behavior are the data-member of the class.

object is a collection of data members and member functions that are class binds together. Which contains code to perform a task

So, in a way we can say that when we are programming in view of OOPs, then the code that is written to solve a problem is executed by calling the object of the same class. More about Object is given in Class and Object page.

 

Sending message

In this, object communicate by sending a message from each other, When an object receives a message, the object calls its own function and executes the function of the same class where it is defined.

 

Data encapsulation

Binding the data member and function member together in a class, is called data encapsulation.

data-encapsulation

as you can see that it is syntax of a class, it means that as soon as we implement the class or do programming using the class, we get the data encapsulation features automatically provided.

 

Data abstraction

Data abstraction is implemented by data encapsulation itself. abstraction shows only the detail that the end user needs without giving any background information.

For Example-

To call from a mobile, enter us the contact number, Let’s call, but how did mobile process this request, it doesn’t matter to you. In such a class, in which the object, sending the message, performs the task by calling the same class function.

Because a class provides a feature like abstraction, so a class is also called abstract data type (ADT).

 

inheritance

This concept of OOPs provides code reusability. In this we add the property or behavior (data or function) of one class to another class. Due to this, a class has its own property or behavior, but the property or behavior of another class also comes, that too without modifying any class. more about inheritance is given in inheritance and their types page.

 

Polymorphism

In Greek language. Poly means- much and morphishm mean-type different type of task but one single name. Let’s consider this with an example. For Example-

Polytechnic represents a whole lot of course in the real world-

As you can understand from the above given diagram, here the external interface is the same but the internal interface is different i.e.

Polytechnic (external interface) is representing all courses which are technical courses, but property and behavior of these courses (internal interface) differ from each other. But being all technical course, they are being represented by Polytechnic, such as

polymorphism-example

so here there is polytechnic, polymorphism.

In similar OOPs, the external interface of any objects or functions are same but their property and behavior (internal interface) are different from each other. Property and behavior mean that they contain a statement or code for different tasks. Example

We have declared three functions. And all three names are same (external interface) but the number of parameters in each function is more or less or different type of parameter is declared (internal interface) i.e. function name is same but parameters are different which will perform different task. i.e. behavior will be different.

In C++, polymorphism is divided into two parts-

polymorphism-types


previous-Storage class and their types

next- Class and object in C++