Lecture05 Unit01 Variables in C++
Lecture05 Unit01 Variables in C++
Variables in C++
Variables in C++ are containers for storing data values. They represent memory
locations that hold a value that can be accessed and manipulated throughout the
program. Each variable in C++ has a name, a type, and a value.
1. Declaring Variables
To use a variable, you first need to declare it. A variable declaration specifies the
type of the variable and gives it a name.
type variableName;
Examples
int age;
double salary;
char grade;
In this example:
int is the data type, and age is the variable name that can store an integer value.
double is the data type, and salary is the variable name that can store a floating-
point value.
char is the data type, and grade is the variable name that can store a single character.
2. Variable Initialization
Here:
The variable age is initialized with the value 25.
The variable salary is initialized with the value 50000.50.
The variable grade is initialized with the character 'A'.
3. Types of Variables
C++ supports different types of variables based on their usage and scope:
a. Local Variables
void exampleFunction() {
int localVar = 10; // Local variable
cout << "Local Variable: " << localVar << endl;
}
b. Global Variables
void exampleFunction() {
cout << "Global Variable: " << globalVar << endl;
}
c. Static Variables
void exampleFunction() {
static int count = 0; // Static variable
count++;
cout << "Count: " << count << endl;
}
Every time exampleFunction() is called, the static variable count retains its value
across different calls.
d. Reference Variables
4. Variable Scope
The scope of a variable refers to the region of the program where the variable can
be accessed. It can be categorized as:
Local Scope: The variable is accessible only within the block or function where it is
declared.
Global Scope: The variable is accessible throughout the entire program.
Function Scope: Specifically for variables that are declared inside functions.
5. Variable Lifetime
The lifetime of a variable is the duration during which the variable occupies
memory and holds a value:
Automatic (Local) Variables: Have a lifetime that lasts until the function or block exits.
Static Variables: Have a lifetime that lasts for the entire program duration.
Global Variables: Have a lifetime that lasts for the entire program duration.
7. Dynamic Variables
Dynamic variables are created during runtime using pointers and dynamic memory
allocation (using new and delete operators).
int *ptr = new int; // Dynamically allocate memory for an integer
*ptr = 50;
cout << "Dynamically allocated value: " << *ptr << endl;
delete ptr; // Free the dynamically allocated memory
8. Const Variables
Variables declared with the const keyword are constant and cannot be modified
after initialization.
const int MAX_SIZE = 100;
MAX_SIZE = 200; // Error: Cannot modify a const variable
9. Volatile Variables
The volatile keyword tells the compiler that the value of the variable may change
at any time without any action being taken by the code the compiler finds nearby.
It’s often used in hardware programming.
volatile int timer;
References:-
Online
1. Tutorialspoint OOP Tutorial: Comprehensive tutorials on OOP concepts with examples. Link
2. GeeksforGeeks OOP Concepts: Articles and examples on OOP principles and applications.
Link
Book
1. "Object-Oriented Analysis and Design with Applications" by Grady Booch: Classic book
covering fundamental OOP concepts and real-world examples.
2. "Object-Oriented Programming in C++" by Robert Lafore: Detailed guide to OOP in C++,
explaining concepts with a practical example