Classes & Objects in C++

    Atul Kabra4 min readUpdated

    A class in C++ is a blueprint that bundles data and the functions that work on that data into one unit. An object is a real, usable copy made from that blueprint. If a class is the design of a house, an object is an actual house built from it. This pairing of data and behaviour is the heart of object-oriented programming (OOP).

    Let us build one from scratch.

    Defining a class

    You declare a class with the class keyword, list its members inside braces, and end with a semicolon.

    #include <iostream>
    #include <string>
    
    class Student {
    public:                    // accessible from outside the class
        std::string name;      // data member (attribute)
        int rollNumber;
    
        // member function (behaviour)
        void introduce() const {
            std::cout << "Hi, I am " << name
                      << " (roll " << rollNumber << ")\n";
        }
    };
    
    int main() {
        Student s;             // create an object of type Student
        s.name = "Asha";       // set its data using the dot operator
        s.rollNumber = 12;
        s.introduce();         // call its member function
    }
    

    Output:

    Hi, I am Asha (roll 12)
    

    The class Student describes what a student is. The object s is one specific student living in memory.

    Objects: many copies from one blueprint

    Each object keeps its own copy of the data members. Changing one object never touches another.

    Student a, b;
    a.name = "Ravi";
    b.name = "Meera";
    // a.name is still "Ravi" — the two objects are independent
    

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses

    public vs private — access control

    By default, members of a class are private: only the class's own functions can touch them. Marking members public opens them to the outside world. Good design keeps data private and exposes controlled functions instead — this is encapsulation.

    #include <iostream>
    
    class BankAccount {
    private:
        double balance = 0.0;          // hidden from outside
    
    public:
        void deposit(double amount) {  // controlled way to change balance
            if (amount > 0) {
                balance += amount;
            }
        }
    
        double getBalance() const {    // read-only access
            return balance;
        }
    };
    
    int main() {
        BankAccount acc;
        acc.deposit(500.0);
        // acc.balance = 1'000'000;    // ERROR: balance is private
        std::cout << "Balance: " << acc.getBalance() << "\n";
    }
    

    Because balance is private, nobody can secretly set it to a wrong value. The only path in is deposit, which checks the amount first.

    struct vs class

    A struct is almost identical to a class — the only difference is that struct members are public by default, while class members are private by default. Use struct for plain data bundles and class when you want behaviour and hidden state.

    The const member function

    Notice void introduce() const. The trailing const promises that the function will not modify the object. Mark every read-only member function const — it lets the function be called on constant objects and signals intent clearly. If you try to change a data member inside a const function, the compiler stops you, which catches a whole class of accidental bugs.

    This habit pays off the moment your objects start travelling through your program as const references. A function that takes const Student& (the efficient, read-only way to pass an object) can only call the const member functions — so forgetting that single keyword can quietly lock you out of your own getters.

    Common mistakes

    • Forgetting the semicolon after the closing brace. A class definition must end with };. Leaving it out produces a confusing compiler error on the next line.
    • Trying to access private members from outside. acc.balance fails to compile when balance is private. Add a public getter instead.
    • Confusing the class with the object. You cannot call Student.introduce(). You must first create an object, then call s.introduce().
    • Defining data members but never initialising them. An uninitialised int rollNumber; holds garbage. Prefer in-class initialisers like int rollNumber = 0;.

    FAQ

    Is a class the same as an object? No. A class is the design; an object is a concrete instance built from that design. One class can produce many objects.

    Should I make members public or private? Default to private and expose only the functions callers genuinely need. This keeps your data safe and your class easy to change later.

    Keep learning

    Want hands-on guidance with mentors in Jalgaon? Join the waitlist for our C++ Programming course and build real projects step by step.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses
    Atul Kabra

    Founder, Infoplanet

    Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.

    Related guides