TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Monday, 24 August 2020

Storage Classes In C++| Storage Classes In C++ Example| Storage Classes In C++ Program| Storage Classes In C++ In Hindi

 

Storage Classes In C++

Storage Classes are used to describe the features of a variable/function. These features basically include the scope, visibility and life-time which help us to trace the existence of a particular variable during the runtime of a program. To specify the storage class for a variable.


image of Storage Classes In C++

Fig:Storage Classes In C++

Synatx:

storage_class  var_data_type var_name;

 C++ uses 5 storage classes, namely:

1.    auto

2.    register

3.    extern

4.    static

5.    mutable

 

.               1.   Auto:

The auto keyword provides type inference capabilities, using which automatic deduction of the data type of an expression in a programming language can be done. As all the types are deduced in compiler phase only, the time for compilation increases slightly but it does not affect the run time of the program.

Program:

#include <iostream>

#include<conio.h>

void autoStorageClass()

{

 

     cout << "Demonstrating auto class\n"; 

     auto a = 32;

     auto b = 3.2;

     auto c = "Pramod Dwivedi";

     auto d = 'G'; 

     cout << a << " \n";

     cout << b << " \n";

     cout << c << " \n";

     cout << d << " \n";

}

    void main()

{

     autoStorageClass();

     getch();

}

Output:

Demostrating auto class

32

3.2

Pramod Dwivedi

G

   

      2.   Extern:

 Extern storage class simply tells us that the variable is defined elsewhere and not within the same block where it is used. Basically, the value is assigned to it in a different block and this can be overwritten/changed in a different block as well. 

Program:

#include <iostream>

#include<conio.h>

int x;

void externStorageClass()

{

          cout << "Demonstrating extern class\n";

                 extern int x;

          cout << "Value of the variable 'x'"

                   << "declared, as extern: " << x << "\n";

                          x = 2;

 

          cout

                   << "Modified value of the variable 'x'"

                   << " declared as extern: \n"

                   << x;

}

 

void main()

{

 

           

          externStorageClass();

getch();

}

Output:

Demonstrating extern class

Value of the variable ‘X’ declare as extern:0

Modified value of the variable ‘x’ declared as extern;

2

       3.   Static:

This storage class is used to declare static variables which are popularly used while writing programs in C++ language. Static variables have a property of preserving their value even after they are out of their scope.

Program:

#include <iostream>

#include<conio.h>

int staticFun()

{

     cout << "For static variables: ";

     static int count = 0;

     count++;

     return count;

}

      int nonStaticFun()

{

     cout << "For Non-Static variables: ";

 

     int count = 0;

     count++;

     return count;

}

 

void main()

{

     cout << staticFun() << "\n";

     cout << staticFun() << "\n";

     ;

     cout << nonStaticFun() << "\n";

     ;

     cout << nonStaticFun() << "\n";

     ;

     getch();

}

Output:

For static variables:1

For static variables:2

For non-static variables:1

For non-static variables:1

        

            4.   Register:

 This storage class declares register variables which have the same functionality as that of the auto variables. The only difference is that the compiler tries to store these variables in the register of the microprocessor if a free register is available. This makes the use of register variables to be much faster than that of the variables stored in the memory during the runtime of the program. 

Program:

#include <iostream>

#include<conio.h>

void registerStorageClass()

{

     cout << "Demonstrating register class\n";

          register char b = 'G';

     cout << "Value of the variable 'b'"

              << " declared as register: " << b;

}

voidmain()

{ 

     registerStorageClass();

     getch();

}

Output:

Demonstrating register class

Value of the variable ‘b’ declared as register:G

        5.   Mutable

 Sometimes there is a requirement to modify one or more data members of class/struct through const function even though you don’t want the function to update other members of class/struct. This task can be easily performed by using the mutable keyword. The keyword mutable is mainly used to allow a particular data member of const object to be modified.

Program:

#include <iostream>

#include<conio.h>

using std::cout;

class Test {

public:

     int x; 

     mutable int y;

     Test()

     {

              x = 4;

              y = 10;

     }

};

void main()

{

     const Test t1;

          t1.y = 20;

     cout << t1.y;

getch();

}

Output:

20

No comments:

Post a Comment