Ready to practice?
Sign up to access interactive coding exercises and track your progress.
Objects and Variables
Understand what objects and variables are, and learn the common data types in C++.
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++:
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:
- Compiler checks the type: "This is an
int, I need 4 bytes" - Finds available memory: "Address 4096 has 4 bytes free"
- Reserves the space: "This address is now reserved for variable
age" - Associates name with address: "
agemeans '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
Multiple assignment (chaining assignments) is generally not recommended as it can make code harder to read and debug.
Best Practices:
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.
Objects and Variables - Quiz
Test your understanding of the lesson.
Practice Exercises
Variable Declaration and Assignment
Practice declaring variables and assigning values.
Lesson Discussion
Share your thoughts and questions
No comments yet. Be the first to share your thoughts!