Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    C++C++

    C++ fundamentals

    • Your first C++ program
    • Variables and types
    • Operators and input
    • Conditions: if and switch
    • Loops

    Functions and collections

    • Functions
    • References: by value or by reference?
    • Strings and arrays
    • std::vector
    • map, set and unordered_map

    Memory and pointers

    • Pointers
    • Stack, heap, new and delete
    • Smart pointers and RAII

    Classes and OOP

    • struct and class
    • Constructors and destructors
    • Inheritance and virtual functions
    • Templates

    The STL and professional C++

    • Algorithms and lambdas
    • Exceptions
    • Recursion
    • Project: a grade book
    C++

    Classes and OOP · Lesson 14 of 21

    struct and class

    Data and functions in one type, public and private, and const methods.

    10 minutes3 games

    A class defines a new type: data (members) and functions that work on it (methods). The only difference between struct and class is that struct members default to public and class members to private. By convention, struct is for plain data and class when there are rules to protect.

    C++
    #include <iostream>
    #include <string>
    
    struct Point {
        int x = 0;
        int y = 0;
    };
    
    class Wallet {
    public:
        void deposit(int amount) {
            if (amount > 0) balance += amount;
        }
        bool withdraw(int amount) {
            if (amount <= 0 || amount > balance) return false;
            balance -= amount;
            return true;
        }
        int getBalance() const { return balance; }
    
    private:
        int balance = 0;       // nobody outside can set this directly
    };
    
    int main() {
        Point p{3, 4};
        std::cout << p.x + p.y << "\n";
    
        Wallet w;
        w.deposit(200);
        std::cout << w.withdraw(500) << " " << w.withdraw(50) << "\n";
        std::cout << w.getBalance() << "\n";
        return 0;
    }
    • class Wallet
      • public (anyone can call)
        • deposit(int)
        • withdraw(int)
        • getBalance() const
      • private (only Wallet's own code)
        • int balance

    This is encapsulation: the balance can't go negative, because the only ways to change it check first.

    Important

    const after a method's name (getBalance() const) means "this doesn't change the object". It's not decoration: through a const Wallet& you can only call const methods. So make every read-only method const.

    Spot the bug

    This program won't compile. Which line?

    Complete the code

    Complete it so area can be called on a const Rect&.

    #include <iostream>
    struct Rect {
        int w, h;
        int area()  { return w * h; }
    };
    int main() {
        const Rect r{3, 5};
        std::cout << r.area() << "\n";
    }

    Interview question

    What are OOP's four principles, and how does C++ support them?

    Exercise

    Finish the Timer class: addMinutes(int m) adds minutes, and format() (const) returns text like 2h 05m. The program prints 2h 05m.

    Expected output: 2h 05m

    C++
    #include <iostream>
    #include <string>
    
    class Timer {
    public:
        void addMinutes(int m) {
            
        }
        std::string format() const {
            return "";
        }
    private:
        int minutes = 0;
    };
    
    int main() {
        Timer t;
        t.addMinutes(95);
        t.addMinutes(30);
        std::cout << t.format() << "\n";
        return 0;
    }

    Learn more from

    • learncpp: Classes and class memberslearncpp.com
    • learncpp: Const class objects and member functionslearncpp.com
    • C++ Core Guidelines: Classesisocpp.github.io

    Played 0 of 3 games

    Sign in to save your progress and earn XP.

    PreviousSmart pointers and RAIINextConstructors and destructors
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.