What are Objects and Variables?

In C++, an object is a piece of memory that can store values. A variable is a named object - it gives the object an identity we can use in our code.

Declaring Variables

To use a variable, you must first declare it by specifying its type and name:

int age;        // Declares a variable named 'age' of type int
double price;   // Declares a variable named 'price' of type double
char grade;     // Declares a variable named 'grade' of type char

Note: all variables must have a data type in C++.

Common Data Types

Here's a table of the most commonly used data types in C++:

📝 Quick Preview
This is a brief introduction to data types. We'll explore each type in more detail in the next lesson: Data Types. And in even more detail in the future.
Data Type Typical Size (bytes) Usage Example
int 4 Whole numbers int age = 25;
short 2 Small whole numbers short year = 2024;
long 8 Large integers long population = 8000000000;
float 4 Decimal numbers float price = 19.99f;
double 8 High-precision decimals double pi = 3.14159265359;
char 1 Individual characters char grade = 'A';
bool 1 Boolean logic bool isValid = true;

How Variables Work in Memory

When you create a variable, the computer reserves space in memory to store its value. Think of memory like a giant apartment building with numbered addresses.

Memory Addresses and Storage

int age = 25;
double price = 19.99;
char grade = 'A';

Each variable gets its own "address" in memory:

Memory Address   | Variable | Value | Size
Address 4096     | age      | 25    | 4 bytes
Address 4100     | price    | 19.99 | 8 bytes
Address 4108     | grade    | 'A'   | 1 byte

Note: Memory addresses are like house numbers on a street - each location has a unique number. The computer uses these numbers to find where your data is stored. The actual numbers don't matter; what's important is that each variable has its own unique location.

Memory Allocation Process

When you declare a variable:

  1. Compiler checks the type: "This is an int, I need 4 bytes"
  2. Finds available memory: "Address 4096 has 4 bytes free"
  3. Reserves the space: "This address is now reserved for variable age"
  4. Associates name with address: "age means 'the value at address 4096'"
int studentCount;        // Reserves 4 bytes at some memory address
studentCount = 30;       // Stores the value 30 at that address
std::cout << studentCount;    // Reads the value from that address

Variable Declaration vs Definition

  • Declaration: Tells the compiler about the variable's type and name
  • Definition: Actually creates the variable and allocates memory
int x;      // Declaration and definition
x = 10;     // Assignment

Multiple Variables

You can declare multiple variables of the same type:

int a, b, c;           // Three integer variables
double x, y, z;        // Three double variables
⚠️ Warning
Multiple assignment (chaining assignments) is generally not recommended as it can make code harder to read and debug.

Best Practices:

• Choose descriptive names: studentAge not a
• Initialize variables when possible
• Declare variables close to where you use them

Summary

Objects and variables are fundamental building blocks in C++:

Key Concepts:

  • Object: A piece of memory that can store values
  • Variable: A named object that gives you an identity to work with
  • Data types: Tell the compiler what kind of data and how much memory is needed

Memory Management:

  • Each variable gets its own memory address
  • The compiler handles memory allocation automatically
  • Different data types use different amounts of memory

Declaration vs Definition:

  • Declaration: Tells the compiler about the variable's type and name
  • Definition: Actually creates the variable and allocates memory
  • In most cases, declaration and definition happen together

Understanding how variables work in memory helps you write more efficient programs and debug issues when they arise.