Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

OOP Part 1: Classes and Objects

Lesson 33 of 53 in the free Foundation of C & C++ notes on Siksha Sarovar, written by Rohit Jangra.

A Fundamental Shift in Thinking

Object-Oriented Programming (OOP) is a style of programming where you group related data and functions into a single unit called an Object.

1. The Class (The Blueprint)

A class is like a blueprint for a house. It isn't a house itself; it just describes what a house should have (walls, roof, windows).

class Rectangle {
  public: // Everyone can see this
    int length;
    int width;

    int getArea() {
        return length * width;
    }
};

2. The Object (The Instance)

An object is a specific house built from that blueprint.

Rectangle r1; // Created an object
r1.length = 10;
r1.width = 5;
cout << r1.getArea(); // Result: 50

The Four Pillars of OOP

  1. Encapsulation: Hiding the internal data and only showing simple "buttons" to the outside world.
  2. Abstraction: Hiding complex details and showing only the essentials.
  3. Inheritance: Creating a new class based on an existing one.
  4. Polymorphism: Allowing one interface to be used for different types of objects.

Access Modifiers: The Security Guards

  • public: Anyone can access these members.
  • private: Only the class itself can see these. (This is the default!).
  • protected: Only the class and its "children" can see these.
In C++, the only difference between a struct and a class is that a struct's members are public by default, while a class's members are private by default.