Day 44: Introduction to OOPs (Part 1)

๐Ÿ“… Date: Feb 13, 2026

๐Ÿง  Mood: Architecting Reality ๐Ÿ—️

๐Ÿ”ฅ Topic: OOPs Part 1: Introduction to Classes & Objects

๐Ÿ›‘ The Paradigm Shift

Up until yesterday, we were writing Procedural Code. We treated code like a recipe: "Do this, then do that." This works great for simple calculators or pattern printing.

But imagine building PUBG or GTA. You have 100 players, 50 cars, and 500 guns. If you use int playerHealth1, playerHealth2..., you will go crazy.

Enter Object-Oriented Programming (OOPs). Instead of writing "steps", we create "Things" (Objects) that interact with each other. It's about modeling real-world entities in code.

Procedural is about Actions. OOP is about Objects.


๐Ÿ›️ Class vs. Object (The Holy Grail)

This is the foundation. If you understand this difference, you understand 50% of OOPs.

1. The Class (Blueprint)

  • It is a user-defined Data Type.
  • It is just a Blueprint or Map.
  • Does it take space in memory? NO.
  • Example: The Architect's drawing of a house.

2. The Object (Instance)

  • It is the Real Entity created from the class.
  • It is an Instance of the Class.
  • Does it take space in memory? YES.
  • Example: The actual House built from that drawing.

๐Ÿ’ป The Syntax: Building a Hero

Let's create a Class called Hero and spawn some Objects.

#include <iostream>
#include <string>
using namespace std;

// -------------------------------------
// 1. CREATING THE CLASS (The Blueprint)
// -------------------------------------
class Hero {
    
    // Access Modifier
    public: 
    
    // Properties (Data Members)
    string name;
    int health;
    
    // Behaviour (Member Functions)
    void attack() {
        cout << name << " is attacking! HP: " << health << endl;
    }
};

int main() {
    
    // -------------------------------------
    // 2. CREATING OBJECTS (The Real Deal)
    // -------------------------------------
    
    // Syntax: ClassName ObjectName;
    Hero h1; 
    
    // Assigning values using dot (.) operator
    h1.name = "Batman";
    h1.health = 100;
    
    // Accessing behaviour
    h1.attack(); // Output: Batman is attacking! HP: 100
    
    // Creating another object
    Hero h2;
    h2.name = "Superman";
    h2.health = 500;
    h2.attack(); // Output: Superman is attacking! HP: 500

    return 0;
}

⚠️ Important: Access Modifiers

Did you see public: inside the class?

By default, members of a class are Private. If I remove public:, the main() function cannot touch h1.name or h1.health. We will discuss this deeper in Part 2.


๐Ÿง  Deep Dive: How Memory Works?

This is a favorite Interview Question.

Statement Memory Allocated?
class Hero { int a; }; No. (It's just a definition)
Hero h1; Yes. (4 bytes for int)

❓ Trick Question: Size of Empty Class

What is the size of an object if the class has NO properties?

class Empty {};
Empty e1;
cout << sizeof(e1);

Answer: 1 Byte.

Why? Because the system needs to give it at least some unique address in memory to identify that the object exists.


๐Ÿš€ Day 44 Challenge

Write a C++ program to model a Smartphone.

  1. Create a class Phone.
  2. Add properties: model, ram, battery.
  3. Add a function playGame() that prints "Playing BGMI on [model]".
  4. Create 2 objects: iPhone and Pixel and make them work.

Next Up: Day 45 - Access Modifiers & Getters/Setters (Security).

No comments:

Post a Comment