Professional Documents
Culture Documents
Storage Classes in CPP PDF
Storage Classes in CPP PDF
Register:
• They are stored in CPU registers and can be easily accessed because access
speed from registers is faster than the normal memory
• When we are accessing a variable what the program most frequently then
declare that variable is registered so that program execution speed
improves because access time of variable comes down
Example:
int main() {
int n;
register int fact=1; //fact variable is stored in cpu register
cout<<" enter a number:";
cin>>n;
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
cout<<"\nfactorial of the "<<n<< "is:"<<fact;
}
Limitations
• CPU registers are very limited you cannot create a large number of
variables as register
• Not all variables declared as register will get stored in CPU registers it
depends on various restrictions on implementation and hardware
Static:
• Static storage class variables are created only once and active throughout
the program
• Static storage class variables are stored in the static area of the data
segment
Example:
demo()
{
auto int a=5; //non_static variable
static int x=5;//static variable
++a;
++x;
cout<<"a is "<<a<<"\t"<<" x is "<<x<<"\n";
}
main()
{
demo();
demo();
demo();
}
Extern:
• Extern storage class allows you to access the global variables declared in
one source file into another file.
• They are visible throughout the program and its lifetime is the same as a
lifetime of the program where it is declared
• As it is having Global scope it can be accessible to all functions in the
program
Example
File: add.cpp
int test=10; // assigning value to test
void add(int n)
{
test=test+n;
}
File: main.cpp
extern int test; // declaring test
int main()
{
cout<<test<<endl;
add(5);
cout<<test<<endl;
return 0;
}
Mutable (Exclusively in C++):
Definition: Mutable data members can be modified even if they are the part of
the objects that are declared as constant
Whenever make an object constant the data members of that object cannot be
modified during the program execution, but If we still want to change some of the
data members of a constant object, we need to make the data member of the
constant object as mutable so that it can be modified
Example:
mutable int a; //a value can be changed
a=3;
cout << "a : " << a; // value of a is 3
a = a*a*a;
cout << "a : " << a ; // value of a is 27
Similarities between C and C++ and Difference between C and C++
C++ can be said a superset of C. Major added features in C++ are Object-Oriented
Programming, Exception Handling and rich C++ Library.
C C++
scanf() and printf() functions are used cin and cout are used for input/output in
for input/output in C. C++.
C C++
Functions in C are not defined inside Functions can be used inside a structure in
structures. C++.
Namespace features are not present Namespace is used by C++, which avoid
inside the C. name collisions.
Virtual and friend functions are not Virtual and friend functions are supported
supported by C. by C++.
A class is defined in C++ using keyword class followed by the name of class.
Example:
Once a class has been declared, we can create variables of that type by using the
class name as:
Add is the class name (i.e., the data type), whereas obj is an object (variable).
The data members and member functions of class can be accessed using the
dot(‘.’) operator with the object. (Like struct variable).
For example, if the name of object is obj and you want to access the member
function with the name sum then you will have to write obj.show( ) .
Member functions that are declared inside a class have to be defined within
the class. Their definitions are very much like the normal functions.
Function body;
e.g:
For example,
class Add
{
Example programs:
#include <iostream>
using namespace std;
int main()
{
Demo obj; // creating object to Add class
return 0;
}
// class program -member function outside the class
#include <iostream>
using namespace std;
class Demo
{
public:
void show() ; // declaration of member function inside
};
int main()
{
Demo obj; // creating object to Add class
return 0;
}