Skillversitas

Classes and Objects in C++

Classes and objects are the foundation of object-oriented programming (OOP) in C++. A class is a template or blueprint that defines the structure and behavior of an entity. An object is an instance of that class — a concrete entity created based on the class blueprint.

With classes and objects, we can group data (attributes) and functions (methods) into a single organized unit. This makes code more modular, easier to maintain, and reusable.

Code Example

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

class BankAccount {
private:
    string ownerName;
    double balance;

public:
    BankAccount(string name, double initialBalance) {
        ownerName = name;
        balance = initialBalance;
    }

    void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
            cout << "Deposit successful. Balance: " << balance << endl;
        }
    }

    void withdraw(double amount) {
        if (amount > 0 && amount <= balance) {
            balance -= amount;
            cout << "Withdrawal successful. Balance: " << balance << endl;
        } else {
            cout << "Insufficient balance!" << endl;
        }
    }

    void displayInfo() {
        cout << "Owner: " << ownerName << ", Balance: " << balance << endl;
    }

    double getBalance() {
        return balance;
    }
};

int main() {
    BankAccount account1("John Smith", 500000);
    account1.displayInfo();

    account1.deposit(200000);
    account1.withdraw(100000);
    account1.displayInfo();

    BankAccount* account2 = new BankAccount("Sarah Johnson", 1000000);
    account2->deposit(500000);
    account2->displayInfo();

    delete account2;
    return 0;
}

Code Explanation

  1. class BankAccount - Defines a class named BankAccount. Inside the class there are data members (member variables) and member functions.

  2. private: - An access specifier that makes ownerName and balance accessible only from inside the class. They cannot be accessed directly from outside.

  3. public: - An access specifier that makes the functions deposit(), withdraw(), displayInfo(), and getBalance() accessible from outside the class.

  4. BankAccount(string name, double initialBalance) - The class constructor. Called automatically when an object is created. Initializes the data members.

  5. BankAccount account1(...) - Creates the object account1 on the stack memory using the BankAccount data type.

  6. account1.deposit(200000) - Uses the dot operator (.) to call a member function on an object declared normally.

  7. BankAccount* account2 = new BankAccount(...) - Creates the object account2 on the heap memory using a pointer.

  8. account2->deposit(500000) - Uses the arrow operator (->) to call a member function through a pointer to the object.

  9. delete account2 - Frees the memory allocated for the object on the heap.

Everyday Use Cases

Imagine you have a bank account. The bank is a class that defines the account's rules (owner name, balance, how to deposit/withdraw). Each customer who opens an account is an object of the BankAccount class. Customer A has their own name and balance, and customer B has their own name and balance. The way they save (deposit) and withdraw money is the same, but each one's balance data is different.

Conclusion

A class is a blueprint that contains the definitions of attributes and methods. An object is a real instance of the class. Use the dot operator (.) for regular objects and the arrow operator (->) for pointer objects. Access specifiers (private, public) control who is allowed to access a class's data and member functions.