0% found this document useful (0 votes)
23 views

Lecture05 Unit01 Variables in C++

Uploaded by

fosefan745
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Lecture05 Unit01 Variables in C++

Uploaded by

fosefan745
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Paper Code: CIC-211

Subject: Object-Oriented Programming Using C++

Unit01, Lecture 05: Topics Covered: Variables

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

Variables can be initialized (assigned an initial value) at the time of declaration.


This process assigns a value to the variable right when it is declared.
type variableName = value;

int age = 25;


double salary = 50000.50;
char grade = 'A';

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

 Declared inside a function or a block.


 Accessible only within that function or block.
 Automatically destroyed once the block is exited.

void exampleFunction() {
int localVar = 10; // Local variable
cout << "Local Variable: " << localVar << endl;
}
b. Global Variables

 Declared outside of all functions.


 Accessible from any function within the same file (and possibly others if declared as
extern).
 Retain their value throughout the lifetime of the program.

int globalVar = 20; // Global variable

void exampleFunction() {
cout << "Global Variable: " << globalVar << endl;
}

c. Static Variables

 Declared with the static keyword.


 Retain their value between function calls.
 Have a lifetime for the duration of the program but a scope limited to the function or
block where they are declared.

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

 Serve as an alias for another variable.


 Declared using the & symbol.
 Must be initialized when declared and cannot be changed to refer to another variable.

int original = 30;


int &ref = original; // Reference variable
ref = 40; // Changes the value of 'original' to 40

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.

6. Variable Naming Rules

When naming variables in C++, follow these rules:

 Names must begin with a letter (a-z, A-Z) or an underscore (_).


 The rest of the name can include letters, digits (0-9), and underscores.
 C++ is case-sensitive, so age, Age, and AGE are three different variables.
 Keywords (reserved words) cannot be used as variable names.

int studentAge; // Valid variable name


int _totalMarks; // Valid variable name
int 2ndYear; // Invalid variable name (cannot start with a digit)

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

You might also like