TeachMate
My ProgressJoin Session

Objects

0/2 completed · ~0h 4m

All Topics
OOPLearnObjects
1 / 2

Creating Objects

beginner
2 min read

An object is an instance of a class. Once you define a class, you can create multiple objects from it.

Creating Objects

Car myCar;           // Stack allocation
Car* ptr = new Car(); // Heap allocation

Accessing Members

myCar.brand = "Toyota";
myCar.year = 2024;
myCar.start();

Dot Operator (.)

  • Used with stack-allocated objects
  • Accesses members directly

Arrow Operator (->)

  • Used with pointers to objects
  • Dereferences and accesses members in one step

Object Size

  • Size of a class = sum of its member variables
  • Empty class takes 1 byte (to ensure unique addresses)
  • Member functions don't add to object size

Multiple objects each have their own copy of member variables but share the same member functions.

Key takeaway: Stack objects are created and destroyed automatically — prefer them over heap allocation.

TeachMate - Interactive learning platform for bachelor students

HomeLearnJoin Session|2 Subjects