DEV Community

Mihir Amin
Mihir Amin

Posted on

OOPs in a nutshell...

Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts defined below:

Class is a user-defined data type which defines its properties and its functions. Class is the only logical representation of the data. For example, Human being is a class. The body parts of a human being are its properties, and the actions performed by the body parts are known as functions. The class does not occupy any memory space till the time an object is instantiated.

Syntax for C++ (CLASS)

  class student{
      public:
             int id;
             int phone;
             string place;
             int add(int x, int y){
             return x + y;
     }
  };

Object is a run-time entity. It is an instance of the class. An object can represent a person, place or any other item. An object can operate on both data members and member functions.

Syntax for C++ (OBJECTS)

carModel l = new car(); 

Encapsulation

Encapsulation is the process of combining data and functions into a single unit called class. In Encapsulation, the data is not accessed directly, it is accessed through the functions present inside the class. In simpler words, attributes of the class are kept private and public getter and setter methods are provided to manipulate these attributes.

Polymorphism

Polymorphism is the ability to present the same interface for differing underlying forms. With polymorphism, each of these classes will have different underlying data. Precisely, Poly means ‘many’ and morphism means ‘forms’.

Types of Polymorphism :

  1. Compile Time Polymorphism
  2. Run Time Polymorphism

A. COMPILE TIME POLYMORPHISM

The polymorphism which is implemented at the compile time is known as compile-time polymorphism.

B. RUN TIME POLYMORPHISM

Runtime polymorphism is also known as dynamic polymorphism. Function overriding is an example of runtime polymorphism. Function overriding means when the child class contains the method which is already present in the parent class. Hence, the child class overrides the method of the parent class. In case of function overriding, parent and child classes both contain the same function with a different definition. The call to the function is determined at runtime is known as runtime polymorphism.

INHERITANCE

Inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In such a way, you can reuse, extend or modify the attributes and behaviors which are defined in other classes. In C++, the class which inherits the members of another class is called
derived class and the class whose members are inherited is called base class. The derived class is the specialized class for the base class.

Syntax for C++ (INHERITANCE)

class derived_class :: visibility-mode base_class;

Types of Inheritance in C++ :

  • Single Inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Multilevel Inheritance
  • Hybrid inheritance

Top comments (0)