CS Electrical And Electronics
@cselectricalandelectronics
All PostsCPPProgramming

What Is Object Oriented Programming, Features Of OPPs

Hello guys, welcome back to my blog. In this article, I will discuss what is object oriented programming, why object oriented programming is required, features of OOPs, with some examples and diagrams.

If you need an article on some other topics then click on ask question and add a new question. You can also catch me @ Instagram – Chetan Shidling.

Also, read:

  1. Exception Handling | Try, Throw, Catch Keyword, Syntax, Code.
  2. What Is An Inheritance In CPP, Types Of Inheritance, Code, Syntax.
  3. Top Best Programming Language To Learn In 2020 And 2021.

Object Oriented Programming

We know that an array is a collection of similar data items. But, it is not possible to have an array of dis-similar data types. This disadvantage can be eliminated using structures in C language. For example, consider the following structure.

struct students
{
      char name[20];
      int marks;
      float avgmarks;
}a;
Object in cpp

Observe that the above student record consisting of fields name, marks, and avgmarks are of different data types and can be grouped together using structures with the help of the keyword struct. The entire student record can be accessed using the variable a. The variable can be treated as an object. We can access the data stored in various fields of different data types using the variable with the help of the dot operator.

  1. The student name which is array of characters can be accessed using: a.name
  2. The student marks which is of type integer can be accessed using: a.marks
  3. The student average marks which is of type float can be accessed using: a.avgmarks

In C++, apart from grouping various types of data, we can also group various functions that can access the data.

  1. Suppose there are two functions get_data() and print_data() that uses the fields defined inside the structure such as name, marks, and avg_marks.
  2. In such situation, the two functions get_data() and print_data() must be enclosed within the braces.
struct students
{
    char name[20];
    int marks;
    float avgmarks;

    get_data();
    print_data();
}a;
object and class in cpp
  1. get_data() may be used to get student names, student marks, and average marks obtained by the students. This function can be invoked using the variable a by specifying a.get_data().
  2. print_data() may be used to display the student name, student marks, and average marks scored by the student. This function can be invoked using the variable a by writing a.print_data().
  3. The variables declared inside the structure are called data members or variable members. In the above example, name, marks, and avg_marks are called data members or variable members.
  4. The functions declared or defined inside the structure are called member functions. In the above structure definition, the functions get_data() and print_data() are called member functions.
  5. The member functions of a structure can access the data members of that structure directly and hence data members are not passed as parameters. In the above structure definition name, marks, and avg_marks can be directly accessed by the member functions get_data() and print_data(), and hence they are not passed as parameter.

Instead of using the keyword struct, we can use the keyword class in C++ like:

class student
{
   public:
       char name[20];
       int marks;
       float avg_marks;

       get_data();
       print_data();
}a;

whenever struct is replaced by class, include public access specifier.

What is a object?

An object is a variable of type class using which a collection of variables and functions declared in that class can be accessed. The variables declared inside the class are called data members and the functions defined or declared inside the class are called member functions.

class students
{
    public:
       char name[20];
       int marks;
       float avg_marks;

       get_data();
       print_data();
}a,b,c;

In above example, three objects a, b, and c are created and you can see images below.

object oriented programming

Memory is allocated only for the data members of each object. But, only one copy of each member function of a class is generated and these member functions are shared by various objects.

What is a object oriented programming?

The art of programming using objects which is supposed by encapsulation, data security, data initialization, inheritance and polymorphism is called object oriented programming. In OOP, the main objective is to create the objects and using these objects appropriate functions are invoked that in turn access the data to accomplish a task.

Features of object oriented programming

The features of object oriented programming are:

01. Encapsulation: In OOP data and functions are combined into one single entity. This process of combining the data and associated functions into one single entity is called encapsulation.

02. Data security: Since data and functions are combined into one single entity, data can be accessed and processed only by the associated member functions i.e., only the member function have right to change the data variables. The functions that are not member functions do not have right to change the data variables and hence the data is secure.

03. Data initialization: In the C language, the programmer has to explicitly call the initialization functions. But, in C++, a guaranteed and automatic initialization of data variables to the desired values is done. This can be done using the concept of constructors in C++.

04. Inheritance: Inheritance is the ability to create new objects from existing objects. The existing object is called the base object and the new object which is created using the existing objects is called the derived class. Click here to read in detail about inheritance. The existing object can be given general common characteristics while the derived object can be given more specific characteristics. This allows code re-usability i.e., the code in the base object need not be duplicated in the derived object.

05. Polymorphism: In OOP two or more functions can have the same name which is not possible in C. This is called function overloading. The same operator can have different meaning in different places. For example, the operator << is left shift operator. It can also be treated as insertion operator. So, the operator << has more than one meaning. This is called operator overloading. Thus, polymorphism is the phenomenon where an entity can exist in two or more forms. Polymorphism is the phenomenon where an entity can exist in two or more forms. Polymorphism is achieved using overloading of functions and overloading of operators in C++.

I hope this article may help you all a lot. Thank you for reading. If you have any doubts related to this article, then click on ask questions to add your question – Ask Question.

Also, read:

Author Profile

CS Electrical And ElectronicsChetu
Interest's ~ Engineering | Entrepreneurship | Politics | History | Travelling | Content Writing | Technology | Cooking
Share Now

CS Electrical And Electronics

Interest's ~ Engineering | Entrepreneurship | Politics | History | Travelling | Content Writing | Technology | Cooking