Object Oriented Programming – Four Pillars of OOP

What is OOP -or- Object Oriented Programming Language

Before we understand what an object is, we must first understand primitive data types.

Primitive data types are the basic building blocks of any programming language. These data types are already available in the language when it is created.

Some common primitive data types in C++ are:

  • int
  • float
  • char
  • bool
  • double

These are called simple data types because they store only a single value at a time.

For example:

  • int stores whole numbers.
  • float and double store decimal values.
  • char stores a single character.
  • bool stores true or false.

Primitive data types are useful when we need to store simple and individual pieces of data.

However, real-world problems are rarely simple. Most applications require storing multiple related values together.

For example:

  • A student has a name, roll number, and marks.
  • A car has a model, speed, and fuel level.
  • A microcontroller peripheral may have registers, configuration settings, and status flags.

If we try to manage all these using only primitive data types, the program becomes difficult to organize and maintain.

This is where Classes and Objects come into the picture.

A class allows us to group related primitive data types together into one structured unit.
An object is an instance of that class, which holds actual values.

In simple terms:

  • Primitive data types store simple data.
  • Classes help us store complex data by grouping related primitive data together.
  • Objects represent real-world entities created from classes.

That is why we need Objects and Classes — they help us organize complex data and build programs using the principles of Object-Oriented Programming (OOP).

What is an Object? and What is a Class ?

  • Object is an Instance of class
  • Class is a template of Objects

🔥 Introducing Classes

  • A class allows us to group related primitive data types together into one structured unit. Instead of Handling separate variables everywhere, we define a class

📦 What Is an Object?

  • Now once we define a class, we can create something called an object.
  • An object is simply an instance of class
  • Example and Difference
    • A Class is a Blueprint of a Car for say
    • An Object is the actual Car created using that blueprint
  • These are the relation between class and object

Class and Object – Car Build Example

Porsche Company is planning to build 3 different cars parallelly and manage these information with their App.

Code Snippet

#include <iostream>
#include <string>

using namespace std;

// Class Definition (Blueprint)
class Car
{
private:
    string model;
    string color;
    int year;
    double price;

public:
    // Constructor
    Car(string m, string c, int y, double p)
    {
        model = m;
        color = c;
        year = y;
        price = p;
    }

    // Member function to display car details
    void displayInfo()
    {
        cout << "Model: " << model << endl;
        cout << "Color: " << color << endl;
        cout << "Year: " << year << endl;
        cout << "Price: $" << price << endl;
        cout << "---------------------------" << endl;
    }
};

int main()
{
    // Creating 3 different car objects
    Car car1("Porsche 911", "Red", 2024, 120000);
    Car car2("Porsche Cayenne", "Black", 2023, 95000);
    Car car3("Porsche Taycan", "White", 2024, 150000);

    // Displaying information
    car1.displayInfo();
    car2.displayInfo();
    car3.displayInfo();

    return 0;
}

Explanation

Class = Blueprint, Here Car class defines

  • Model
  • Color
  • Year
  • Price

It describe what a car should have, but with this it doesn’t represent a real car. Now let’s see about Object

Object = Real Car Info

Car car1(...);
Car car2(...);
Car car3(...);

These are called as Object – where it represent the car details/info using the class which created

Now Lets See about OOP Principles

There are four pillars to understand about OOP Principles

  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism
image

Example Code to Understand OOP Principle

#include <iostream>
using namespace std;

// -------------------------------
// Base Class (Abstraction + Encapsulation)
// -------------------------------
class Vehicle
{
private:
    int speed;   // Encapsulation (data hidden)

public:
    // Constructor
    Vehicle() : speed(0) {}

    // Public method to control speed (Encapsulation)
    void setSpeed(int s)
    {
        if (s >= 0)
            speed = s;
    }

    int getSpeed()
    {
        return speed;
    }

    // Virtual function (Polymorphism)
    virtual void start()
    {
        cout << "Vehicle is starting..." << endl;
    }
};

// -------------------------------
// Derived Class (Inheritance)
// -------------------------------
class Car : public Vehicle
{
private:
    string model;

public:
    Car(string m)
    {
        model = m;
    }

    // Abstraction: Simple interface
    void display()
    {
        cout << "Car Model: " << model << endl;
        cout << "Speed: " << getSpeed() << " km/h" << endl;
    }

    // Polymorphism (Function Overriding)
    void start() override
    {
        cout << model << " engine started with push button!" << endl;
    }
};

// -------------------------------
// Main Function
// -------------------------------
int main()
{
    Car myCar("Porsche 911");

    // Encapsulation
    myCar.setSpeed(120);

    // Abstraction
    myCar.display();

    // Polymorphism
    myCar.start();

    return 0;
}

Encapsulation

  • Hiding Data. Prevent direct access from outside
  • Encapsulation is Grouping data with methods in a class

Leave a Reply