OOPS Concepts in C++

OOPS Concept in C++

There are four pillars of Object-Oriented Programming concept: Encapsulation,Data hiding, Inheritance and Polymorphism and they pretty much run the OOPS concept in C++ . These functionalities are implemented with the help of Classes and Objects.

class vs object in c++

OOPs Concept in Brief

PrepSter Tip: Consider Classes and Objects as the heart of any object-oriented language.

Following are main features of OOPS Concept in C++

  1. Class
  2. Objects
  3. Encapsulation
  4. Abstraction
  5. Polymorphism
  6. Inheritance
  7. Dynamic Binding
  8. Message Passing

Other Features of OOPS in C++ –

  • Dynamic Binding
  • Message Passing
OOPS Concepts In C++

Benefits of Object Orient Programming

  • More secure code and hacking resistivity, because of data hiding.
  • For any class multiple objects(instances) can co exist without any overlap of interest.
  • The code looks more organised as code segmentation(partition) is possible due to objects/classes
  • Because of inheritance redundant code is eliminated, as super class properties are extended to subclasses.
  • The Code is scalable from small to large.

Introduction to OOPs in C++

Objects in C++ –

This is the primary unit in object oriented programming, represent a collection of number of entities. Each object will contain its own data and code to do manipulation in the the other part of code/data, objects are instances of a class.

Example –

  1. Fruit is an Object and data will be – name, color, size, volume or size
  2. Student – name, rollNo, Age, percentage etc

More Information – 

  • Objects are real world representation in a code and contains its properties & functions etc properties like name, color, size etc.
  • Objects are Run time entities, i.e. they will only take up space at run time not compile time for any program
  • Objects can interact without having to know details of each others data or code
  • On program execution objects talk to one another by sending messages

Classes in C++ –

You can consider a class a blueprint or enclosing entity for objects. Class is a collection of objects of similar type. Objects are variables of the type class. Once a class has been defined, we can create any number of objects belonging to that class.

Example – student1, student2, student3 are the member of class student and they will have their own properties like name, rollNo, Age etc.

Prepster Tip – Class is a user defined data type like structures and unions in C

// creating Student class
class Student
{
    char name[20];
    int rollNo;
    
    public:
    
    void getdetails(){}
};

int main(){
    Student s1; //s1 is a object 
}

Note – The below object oriented concepts have their own posts, don’t worry if you don’t understand them completely, we are just trying to introduce these and will cover each of them in detail in the later posts.

Syntax for Class

class class_name
{
    private:
    // Here we will have private data members
    // functions declarations of the class
    
    public:
    // Here we will have public data members
    // functions declarations of the class

    protected:
    // Here we will have protected data members
    // functions declarations of the class
};