Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

C++ Classes and Objects — OOP Guide | Tutorials Logic

What is a Class?

A class is a user-defined blueprint that bundles data (attributes) and functions (methods) together. An object is an instance of a class - a concrete entity created from the blueprint.

Access SpecifierAccessible from
publicAnywhere - inside and outside the class
privateOnly inside the class (default for class members)
protectedInside the class and derived classes
Class with Constructor, Destructor, Getters/Setters
#include <iostream>
#include <string>
using namespace std;

class BankAccount {
private:
    string owner;
    double balance;

public:
    // Constructor - called when object is created
    BankAccount(string name, double initialBalance) {
        owner   = name;
        balance = initialBalance;
        cout << "Account created for " << owner << endl;
    }

    // Destructor - called when object is destroyed
    ~BankAccount() {
        cout << "Account closed for " << owner << endl;
    }

    // Getters (const - don't modify the object)
    string getOwner()  const { return owner; }
    double getBalance() const { return balance; }

    // Methods
    void deposit(double amount) {
        if (amount > 0) balance += amount;
    }

    bool withdraw(double amount) {
        if (amount > 0 && amount <= balance) {
            balance -= amount;
            return true;
        }
        return false;
    }

    void display() const {
        cout << owner << ": $" << balance << endl;
    }
};

int main() {
    BankAccount acc("Alice", 1000.0);
    acc.deposit(500.0);
    acc.withdraw(200.0);
    acc.display();  // Alice: $1300

    // acc.balance = 9999;  // ERROR: private member

    // Object on heap
    BankAccount *ptr = new BankAccount("Bob", 500.0);
    ptr->deposit(100.0);
    ptr->display();  // Bob: $600
    delete ptr;

    return 0;
}

Constructor Types

Default, Parameterized, Copy Constructor
#include <iostream>
using namespace std;

class Point {
public:
    int x, y;

    // Default constructor
    Point() : x(0), y(0) {}

    // Parameterized constructor (initializer list - preferred)
    Point(int x, int y) : x(x), y(y) {}

    // Copy constructor
    Point(const Point &other) : x(other.x), y(other.y) {
        cout << "Copy constructor called" << endl;
    }

    void print() const {
        cout << "(" << x << ", " << y << ")" << endl;
    }
};

int main() {
    Point p1;           // default: (0, 0)
    Point p2(3, 4);     // parameterized: (3, 4)
    Point p3 = p2;      // copy constructor: (3, 4)

    p1.print();  // (0, 0)
    p2.print();  // (3, 4)
    p3.print();  // (3, 4)

    // Static member - shared across all objects
    return 0;
}

Static Members and this Pointer

Static Members and this Pointer
#include <iostream>
using namespace std;

class Counter {
private:
    int id;
    static int count;  // shared across all instances

public:
    Counter() {
        count++;
        id = count;
    }

    // this pointer - points to the current object
    Counter& setId(int id) {
        this->id = id;  // disambiguate member vs parameter
        return *this;   // enables method chaining
    }

    static int getCount() { return count; }  // static method
    int getId() const { return id; }
};

int Counter::count = 0;  // define static member outside class

int main() {
    Counter c1, c2, c3;
    cout << "Total: " << Counter::getCount() << endl;  // 3

    c1.setId(10);
    cout << "c1 id: " << c1.getId() << endl;  // 10

    return 0;
}

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.