C++: The Blueprint, Classes and Objects

Learn how to define a class as a blueprint, add member variables and functions, and bring objects to life by instantiating that blueprint.

From Procedural to Object-Oriented Thinking

In procedural code, data and logic live separately. A variable holds state and a function acts on it, but there is no formal connection between the two. Object-oriented programming (OOP) closes that gap by packaging related data and the functions that operate on it into a single unit called a class.

Procedural vs Object-Oriented

OOP maps real-world concepts directly to code. A Rectangle knows its own dimensions and can compute its own area.

  • Procedural: scattered variables + free functions that receive data as arguments
  • OOP: a class bundles the data (state) and the functions (behavior) that belong together
  • The class is the blueprint; the object is the actual instance built from it
  • One blueprint, many objects: create as many Rectangle objects as you need

Class Declaration and Definition

A class is declared with the class keyword followed by a name and a brace-enclosed body. Members are private by default, meaning they are hidden from code outside the class. Use the public: access specifier to expose members that callers are allowed to use.

class keyword

A class definition tells the compiler what data an object of that type will hold and what operations it can perform.

  • Syntax: class ClassName { access-specifier: members; };
  • Members before any specifier are private by default
  • private: accessible only inside the class
  • public: accessible from anywhere the object is visible
  • The closing brace must be followed by a semicolon

Declaring a Class

C++

Define Rectangle with two public member variables, then create and use an object.

Member Variables

Member variables (also called data members or fields) represent the state of an object. Each object gets its own copy of every member variable, so two Rectangle objects can have different dimensions without interfering with each other.

Member Variables

Think of member variables as the attributes that describe what an object IS: a Rectangle IS defined by its length and width.

  • Declared inside the class body, outside any function
  • Each object owns its own copy; changing one object does not affect another
  • Access with the dot operator: object.memberName
  • Typically declared private and exposed through public functions (encapsulation)

Each Object Has Its Own Data

C++

r1 and r2 are independent; modifying one never touches the other.

Member Functions

Member functions (also called methods) define the behavior of an object. They can read and modify the object's own member variables directly, without needing them passed as arguments. This is what makes objects self-contained: state and logic stay together.

Member Functions

A member function is defined inside (or associated with) a class and can access all of that class's data members directly.

  • Declared and defined inside the class body, just like a regular function
  • Called with the dot operator: object.functionName()
  • Can read and write the object's member variables without extra parameters
  • Helps keep related logic next to the data it operates on

Member Function

C++

calculateArea() reads length and width from the object itself, no arguments needed.

The Rectangle Class: State and Behavior Together

Combining member variables with member functions gives a class both state and behavior. The Rectangle below stores length and width as its state, and provides calculateArea() and calculatePerimeter() as its behavior.

State and Behavior

A well-designed class keeps its data and the functions that work on that data in the same place, making the code easier to read and reuse.

  • State: length, width, the values that make each object unique
  • Behavior: calculateArea(), calculatePerimeter(), what the object can do
  • Any number of objects can be created from the same class with different values
  • Each object calls its own copy of the member functions on its own data

Rectangle: Full Class

C++

State (length, width) and behavior (area, perimeter) live together in one class.

Creating Objects

Declaring a variable whose type is a class creates an object. This is called instantiation. Every object is an independent instance of its class with its own copy of the member variables. You can create as many objects as you need from the same class.

Instantiation

Instantiation is the act of creating an object from a class blueprint. The class defines the shape; the object is the actual thing in memory.

  • Syntax: ClassName objectName; (stack allocation)
  • Multiple objects: Rectangle r1, r2, r3; each is independent
  • You can also create objects dynamically: Rectangle* p = new Rectangle;
  • Objects on the stack are destroyed automatically when they go out of scope

Multiple Objects from One Class

C++

small and large are independent objects. Each calls calculateArea() on its own data.

Constructors: Initializing at Creation

A constructor is a special member function that runs automatically the moment an object is created. It has the same name as the class and no return type. Use it to give member variables their starting values so objects are never left in an uninitialized state.

Constructor

A constructor guarantees that every object starts life with valid, known values rather than random garbage.

  • Same name as the class, no return type, not even void
  • Called automatically on object creation, no manual call needed
  • Can accept parameters to set initial values at the point of creation
  • If you define none, the compiler generates a default constructor that does nothing

Constructor with Parameters

C++

Pass length and width at creation time; no separate assignment needed.

class vs struct

In C++, class and struct are almost identical. The only technical difference is the default access level for members.

class vs struct

The convention is to use struct for simple data holders and class for types that encapsulate behavior behind private data.

  • struct: members are public by default
  • class: members are private by default
  • Both support constructors, member functions, inheritance, and access specifiers
  • Use class when the type has meaningful behavior and needs encapsulation
Featureclassstruct
Default accessprivatepublic
Member functionsYesYes
ConstructorsYesYes
Inheritance defaultprivatepublic
Typical useEncapsulated types with behaviorSimple data holders

Knowledge Check

1. What is a class in C++?

2. What is an object?

3. What is the default access level for members declared inside a class?

4. Which keyword makes class members accessible from outside the class?

5. How do you call a member function on an object named rect?

6. What does a constructor do?

7. Which of the following is the correct way to define a class Rectangle with a public member int length?