Topic 41 of 91 8. Object-Oriented Programming in C++ Interactive Tutorial

Classes and Objects in C++: Defining Types and Creating Instances

7 mins read Last updated: 26 Sep 2026 20,117 views

Direct Answer & Core Concept

A class is defined using the class keyword containing data members and member functions. Objects can be instantiated automatically on the stack (automatic storage duration, destroyed when leaving scope) or dynamically on the heap using new or smart pointers (std::make_unique).

Detailed Explanation & Mechanics

1. Class Definition & Memory Layout:

A class defines the layout of bytes in memory. The size of an object equals the sum of its non-static data members plus alignment padding:

Architecture Diagram
STACK ALLOCATION vs HEAP ALLOCATION:
Stack: [BankAccount Object: balance(8B) | accountNumber(32B)] -> Automatic Scope Cleanup
Heap:  Stack Pointer (8B) ──► Heap Memory [BankAccount Object] -> Manual delete / Smart Pointer

2. Member Access:

  • Direct object: use dot operator obj.member()
  • Pointer to object: use arrow operator ptr->member() or (*ptr).member()

Runnable Code Example: Classes and Objects in C++

C++20 / C++23
main.cpp
Run Online
#include <iostream>
#include <string>
#include <memory>

class BankAccount {
private:
    std::string accountNumber;
    double balance;

public:
    BankAccount(std::string accNum, double initialDeposit)
        : accountNumber(std::move(accNum)), balance(initialDeposit) {}

    void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
            std::cout << "Deposited $" << amount << ". New balance: $" << balance << "\n";
        }
    }

    void displayInfo() const {
        std::cout << "Account: " << accountNumber << " | Balance: $" << balance << "\n";
    }
};

int main() {
    // 1. Stack Allocation (Automatic)
    BankAccount stackAcc("ACC-STACK-101", 1500.0);
    stackAcc.deposit(350.0);
    stackAcc.displayInfo();

    // 2. Heap Allocation with Smart Pointer (Modern Best Practice)
    auto heapAcc = std::make_unique<BankAccount>("ACC-HEAP-909", 5000.0);
    heapAcc->deposit(1000.0);
    heapAcc->displayInfo();

    return 0;
}

Common C++ Technical Interview Questions & Answers

Interview Prep
Q

Q: What is the default access specifier in a C++ class vs a struct?

A: In a class, members and base classes are private by default. In a struct, members and base classes are public by default.
Q

Q: What is the size of an empty class in C++ and why?

A: An empty class has a size of 1 byte (non-zero) to ensure distinct objects of that class have unique memory addresses.

Key Takeaways, Modern Best Practices & Idioms

C++ Best Practices & Production Rules

• Prefer stack allocation or smart pointers (std::unique_ptr) over raw new and delete.
• Mark member functions that do not alter member variables with const.
• Pass complex objects by const reference const Type& to avoid unnecessary deep copying.
We value your privacy & transparency

OperateTools uses cookies and local storage to ensure basic tool functionality, preserve preferences, and analyze aggregated traffic. We never sell your personal data or index your document files. Learn more in our Privacy Policy.