Special Offer Diwali Special Bundle Offer: 3 courses for ₹8,999, save ₹9,001 (50% OFF)! Limited seats available. View Bundles →
0% Completed
Tutorials / C++ Programming Tutorial / Chapter 3: Object-Oriented Programming (OOP) in C++
CPP ⏱ 12 min read

Classes, Objects, and Access Specifiers

Classes are the heart of C++. They encapsulate state and logic together, providing strict data protection.

What is it?

A class is a user-defined blueprint for creating objects. Access specifiers (`public`, `private`, `protected`) control visibility of members.

Why is it used?

Protects internal object state from unauthorized modification and models real-world entities.

Detailed Explanation

Access Specifiers: • `private`: Members can only be accessed from within the class itself. Default for classes in C++! • `public`: Members can be accessed from outside the class. • `protected`: Members can be accessed inside the class and by derived subclasses. • Methods defined inside class bodies are automatically treated as inline candidates.

Syntax Structure

Syntax Blueprint
class Student {
private:
    int id;
public:
    void setId(int newId) { id = newId; }
};

Code Example

CPP Code
#include <iostream>
#include <string>

class Student {
private:
    // Private attributes (Encapsulated data)
    int student_id;
    std::string name;
    std::string enrolled_course;
    double balance_due;

public:
    // Public Setter Method
    void set_details(int id, const std::string &student_name, const std::string &course, double due) {
        student_id = id;
        name = student_name;
        enrolled_course = course;
        balance_due = due;
    }

    // Public Business Logic Method
    void record_payment(double payment_amount) {
        if (payment_amount <= 0) {
            std::cout << "[ERROR] Invalid payment amount.\n";
            return;
        }
        balance_due -= payment_amount;
        std::cout << "Payment of ₹" << payment_amount << " recorded for " << name << ".\n";
    }

    // Public Display Method
    void display_card() const {
        std::cout << "[" << student_id << "] " << name 
                  << " | Course: " << enrolled_course 
                  << " | Dues: ₹" << balance_due << "\n";
    }
};

int main() {
    // Create object instance
    Student s1;
    s1.set_details(101, "Ramesh Kumar", "KLiC Advanced Python", 2500.0);
    
    s1.display_card();
    s1.record_payment(1500.0);
    s1.display_card();
    
    return 0;
}

Explanation of the Code

• Member variables are `private` to prevent direct modification like `s1.balance_due = -100`. • `record_payment` validates input before mutating internal balance. • `const` on `display_card()` promises the method will not alter object state.

Expected Output

[101] Ramesh Kumar | Course: KLiC Advanced Python | Dues: ₹2500 Payment of ₹1500 recorded for Ramesh Kumar. [101] Ramesh Kumar | Course: KLiC Advanced Python | Dues: ₹1000

Real-World Application

Used across banking systems, inventory databases, and game character entity systems.

Common Mistakes to Avoid

• Forgetting the closing semicolon `;` at the end of class definition `};`. • Forgetting that class members are private by default in C++ (unlike structs which are public by default).

Quick Practice Challenge

Create a `Course` class with private `title` and `price`, and a public method to print them.

Key Takeaways

• Classes are private by default in C++. • Use `private` for member variables and `public` for API methods. • Mark non-mutating inspection methods with `const`.