You are on page 1of 11

Storage Classes in CPP:

Storage Classes are used to describe the features of a variable.


Storage classes will provide the following information to the compiler
➢ Storage Area of a variable
➢ Scope of a variable i.e in which block the variable is visible(accessible)
➢ Lifetime of a variable: How long variable is active i.e when it is erased
from memory
➢ Default value of a variable if it is not initialized
To specify the storage class for a variable, the following syntax is to be followed:
Syntax:
storageclass datatype varname;
e.g: auto int x;
register int y;

C++ uses 5 storage classes, namely:


1. auto
2. register
3. extern
4. static
5. mutable
Auto:
• The Automatic storage variables are stored in the stack area of the data
segment
• By default, any type of variable is auto and lifetime of auto variables is
restricted within the body i.e how many time we are calling the function
those many time that auto variables are constructed and destroyed
Example:
void demo()
{
auto int a=10; //auto variable
++a;
cout<<"a is " <<a<<"\n";
}
main()
{
demo(); //creates 'a' and destroys that memory
demo();//creates a new 'a' and destroys that memory
}

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++

Similarities between C and C++ are:

➢ Both the languages have a similar syntax.


➢ Code structure of both the languages is same.
➢ The compilation of both the languages is similar.
➢ They share the same basic syntax. Nearly all of C’s operators and keywords
are also present in C++ and do the same thing.
➢ C++ has a extended grammar than C, but the basic grammar is the same.
➢ Basic memory model of both is very close to the hardware.
➢ Same notions of stack, heap, file-scope and static variables are present in
both the languages.

Differences between C and C++ are:

C++ can be said a superset of C. Major added features in C++ are Object-Oriented
Programming, Exception Handling and rich C++ Library.

Below is the table of differences between C and C++:

C C++

C was developed by Dennis Ritchie in C++ was developed by Bjarne Stroustrup in


1972 at AT&T Bell Labs. 1979 at AT&T Bell Labs..

C is a subset of C++. C++ is a superset of C.

C contains 32 keywords. C++ contains 52 keywords.

C++ supports both procedural and object


C supports procedural programming. oriented programming .

scanf() and printf() functions are used cin and cout are used for input/output in
for input/output in C. C++.
C C++

Data and functions are encapsulated


Data and functions are separated in C together in form of an object in C++.

C does not support information hiding. Data is hidden in C++(abstraction)

C is a function driven language because


C is a procedural programming C++ is an object driven language because it
language. is an object oriented programming.

Function and operator overloading is Function and operator overloading is


not supported in C. supported by C++.

C is a function-driven language. C++ is an object-driven language

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.

Header file used by C is stdio.h. Header file used by C++ is iostream.h.

Reference variables are not supported


by C. Reference variables are supported by C++.

Virtual and friend functions are not Virtual and friend functions are supported
supported by C. by C++.

C does not support inheritance. C++ supports inheritance.

Direct support for exception handling is


not supported by C. Exception handling is supported by C++.
Explain basic syntaxes of declaring class, object & methods in C++.

Objects and Classes:

➢ A class is similar to a structure in C.


➢ A Class is a user defined data-type which has data members and member
functions.
➢ Data members are the data variables.
➢ Member functions are the functions used to manipulate these variables.
➢ For example of class Car, the data member will be speed limit, mileage etc
and member functions can be apply brakes, increase speed etc.
Defining a Class:

A class is defined in C++ using keyword class followed by the name of class.

Example:

class Add // defining class like structure


{
public:
int num1, num2; // data members

void sum() // defining member function


{
cout<<"sum= "<<num1+num2;
}
};
Creating Objects:

Once a class has been declared, we can create variables of that type by using the
class name as:

Classname object name;

For example, Add obj;

Add is the class name (i.e., the data type), whereas obj is an object (variable).

Accessing data members and member functions:

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( ) .

Add obj; // creating object to Add class like struct variable

obj.sum(); // calling method by Add class object

Defining Member Functions:

Member functions (methods of a class) can be defined in two places:

1. Inside the class definition.

2. Outside the class definition.

→ Inside the Class Definition:

Member functions that are declared inside a class have to be defined within
the class. Their definitions are very much like the normal functions.

They should have a function header and a function body.


Syntax:

return- type function-name (argument declaration)

Function body;

e.g:

class Demo // defining class like structure


{
public:
void show() // method
{
cout<<"Hello from show\n";
}
};

→ Outside the Class Definition: Member functions can be declared inside a


class (prototype) and can be defined (body) separately outside the class.

For example,

class Add
{

void sum(); // Declaring member function


};

void Add::sum() // defining member function outside class


{
cout<<"sum= "<<num1+num2;
}
Here :: is called scope resolution operator which shows a method
belongs to a class. i.e, sum method belongs to class Add.

Example programs:

// class program -member function inside the class

#include <iostream>
using namespace std;

class Demo // defining class like structure


{
public:
void show() // method
{
cout<<"Hello from show\n";
}
};

int main()
{
Demo obj; // creating object to Add class

obj.show(); // calling method by Add class object

return 0;
}
// class program -member function outside the class

#include <iostream>
using namespace std;

class Demo
{
public:
void show() ; // declaration of member function inside
};

void Demo::show() // member function definition outside


{
cout<<"Welcome\n";

int main()
{
Demo obj; // creating object to Add class

obj.show(); // calling method by Add class object

return 0;
}

You might also like