Professional Documents
Culture Documents
01. Variables, Constant, Data Types
01. Variables, Constant, Data Types
Variables in C++ is a name given to a memory location. It is the basic unit of storage in a program.
Types of Variables
There are three types of variables based on the scope of variables in C++
Local Variables
Instance Variables
Static Variables
1. Local Variables: A variable defined within a block or method or constructor is called a local
variable.
These variables are created when entered into the block or the function is called and destroyed
after exiting from the block or when the call returns from the function.
The scope of these variables exists only within the block in which the variable is declared. i.e. we
can access this variable only within that block.
Initialization of Local Variable is Mandatory.
1. Instance Variables: Instance variables are non-static variables and are declared in a class
outside any method, constructor, or block.
As instance variables are declared in a class, these variables are created when an object of the
class is created and destroyed when the object is destroyed.
Unlike local variables, we may use access specifiers for instance variables. If we do not specify
any access specifier then the default access specifier will be used.
Initialization of Instance Variable is not Mandatory.
Instance Variable can be accessed only by creating objects.
These variables are declared similarly as instance variables, the difference is that static variables
are declared using the static keyword within a class outside any method constructor or block.
Unlike instance variables, we can only have one copy of a static variable per class irrespective of
how many objects we create.
Static variables are created at the start of program execution and destroyed automatically when
execution ends.
Initialization of Static Variable is not Mandatory. Its default value is 0
If we access the static variable like the Instance variable (through an object), the compiler will
show the warning message and it won’t halt the program. The compiler will replace the object
name with the class name automatically.
If we access the static variable without the class name, the Compiler will automatically append the
class name.
Constants
Constants are the variables with fixed values.
We have to initialize the constants at the time of declaration and we cannot change this value
later in the program.
const and constexpr can be used to define a constant.
#define is also used to define a macro constant.
Scope of Variables
1. Local Variables
2. Global Variables
A normal global variable can be made extern as well by placing the ‘extern’ keyword before its
declaration/definition in any function/block. The main purpose of using extern variables is that they can
be accessed between two different files which are part of a large program.
// C++ Program to illustrate the extern storage class
#include <iostream>
using namespace std;
int main()
{
return 0;
}
Output
Demonstrating extern class
Value of the variable 'x'declared, as extern: 0
Modified value of the variable 'x' declared as extern:
2
We can say that they are initialized only once and exist until the termination of the program. Thus, no
new memory is allocated because they are not re-declared. Global static variables can be accessed
anywhere in the program.
class Test {
public:
int x;
Test()
{
x = 4;
y = 10;
}
};
int main()
{
// t1 is set to constant
const Test t1;
Output
20
thread_local Storage Class
// driver code
int main()
{
// thread 1
thread th1([]() {
cout << "Thread 1 var Value: " << (var += 18) << '\n';
});
// thread 2
thread th2([]() {
cout << "Thread 2 var Value: " << (var += 7) << '\n';
});
// thread 3
thread th3([]() {
cout << "Thread 3 var Value: " << (var += 13) << '\n';
});
th1.join();
th2.join();
th3.join();
return 0;
}
Output
Constants in C++
Constants in C++ refer to variables with fixed values that cannot be changed. Once they are defined in
the program, they remain constant throughout the execution of the program.
Note: We have to initialize the constant variable at the time of declaration as we cannot modify
the value of it later in the program.
Syntax
#define MACRO_NAME replacement_value
C++ Data Types
typedef in C++
typedef keyword in C++ is used for aliasing existing data types, user-defined data types, and pointers
to a more meaningful name. Typedefs allow you to give descriptive names to standard data types,
which can also help you self-document your code. Mostly typedefs are used for aliasing, only if the
predefined name is too long or complex to write again and again.
Syntax:
Literals In C++
Literals are fundamental elements used to represent constant values used in C++ programming
language. These constants can include numbers, characters, strings, and more.
C++ Type Modifiers
Modifiers are used in C++ to change or give extra meaning to already existing data types. It’s added to
primitive data types as a prefix to change their meaning.