Ready to practice?
Sign up to access interactive coding exercises and track your progress.
Introduction to objects and variables
Understand what objects and variables are, and learn the common data types in C++.
Prerequisites
Introduction to Objects and Variables
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++:
Data Type | Size (bytes) | Range/Values | Usage | Example |
---|---|---|---|---|
int |
4 | -2,147,483,648 to 2,147,483,647 | Whole numbers | int age = 25; |
short |
2 | -32,768 to 32,767 | Small whole numbers | short year = 2024; |
long |
8 | Very large whole numbers | Large integers | long population = 8000000000; |
float |
4 | ±3.4 × 10^38 (6-7 digits precision) | Decimal numbers | float price = 19.99f; |
double |
8 | ±1.7 × 10^308 (15-16 digits precision) | High-precision decimals | double pi = 3.14159265359; |
char |
1 | Single character (ASCII) | Individual characters | char grade = 'A'; |
bool |
1 | true or false |
Boolean logic | bool isValid = true; |
string |
Variable | Text of any length | Text and words | string name = "John"; |
Key Notes:
- Integers: Use
int
for most whole numbers,long
for very large values - Decimals: Use
double
for most decimal calculations (better precision thanfloat
) - Characters: Use
char
for single characters,string
for text - Boolean: Use
bool
for true/false conditions - Size: Actual sizes may vary by system, but these are typical values
Integer Types
int
: Whole numbers (-2147483648 to 2147483647)short
: Smaller integers (-32768 to 32767)long
: Larger integers
Floating-Point Types
float
: Decimal numbers (6-7 decimal digits precision)double
: Double precision decimals (15-16 decimal digits precision)
Note: double
has more precision than float
. We will discuss precision in later chapters.
Other Types
char
: Single characters ('A', '5', '$')bool
: True/false valuesstring
: Text (requires #include)
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
Best Practices
- Choose descriptive names:
studentAge
nota
- Initialize variables when possible
- Declare variables close to where you use them
Introduction to 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