Static keyword uses in C++

Static elements are allocated storage only once in a program lifetime in static storage area. And they have a scope till the program lifetime. Static Keyword can be used with following,

1. Static variable in functions
2. Static Class Objects
3. Static member Variable in class
4. Static Methods in class

Static variables inside Functions

Static variables when used inside function are initialized only once, and then they hold there value even through function calls.These static variables are stored on static storage area, not in stack.

Example :

#include<iostream.h>
#include<conio.h>
void fun()
{
staticint a;
cout<<"The a value is "<<a<<endl;
a++;
}
void fun1()
{
int a=1;
cout<<"The a value is "<<a<<endl;
a++;
}
void main()
{
clrscr();
fun();
fun();
fun1();
fun1();
getch();
}

If we do not use static keyword, the variable count, is reinitialized everytime when fun1() function is called, and gets destroyed each time when fun1() functions ends. But, if we make it static, once initialized count will have a scope till the end of main() function and it will carry its value through function calls too.If you don't initialize a static variable, they are by default initialized to zero.

Static class Objects

Static keyword works in the same way for class objects too. Objects declared static are allocated storage in static storage area, and have scope till the end of program.Static objects are also initialized using constructors like other normal objects. Assignment to zero, on using static keyword is only for primitive datatypes, not for user defined datatypes.

Example :

#include<iostream.h>
#include<conio.h>
class sa
{
public:
sa()
{
cout<<"This is Default Constructor"<<endl;
}
void print()
{
cout<<"Welcome";
}
~sa()
{
cout<<"This is Destructor";
}
};
void main()
{
clrscr();
static sa s;
s.print();
getch();
}

Static data member in class

Static data members of class are those members which are shared by all the objects. Static data member has a single piece of storage, and is not available as separate copy with each object, like other non-static data members.Static member variables (data members) are not initialized using constructor, because these are not dependent on object initialization.Also, it must be initialized explicitly, always outside the class. If not initialized, Linker will give error.

Example :

#include<iostream.h>
#include<conio.h>
class sa
{
public:
static int c;
public:
void print()
{
cout<<"The static variable value is "<<c<<endl;
c++;
}
};
intsa::c=10;
void main()
{
clrscr();
sa s;
s.print();
cout<<"The static variable value is "<<s.c;
cout<<"The static variable value is "<<sa::c;
getch();
}

Once the definition for static data member is made, user cannot redefine it. Though, arithmetic operations can be performed on it.

Static Member Functions

These functions work for the class as whole rather than for a particular object of a class.It can be called using an object and the direct member access .operator. But, its more typical to call a static member function by itself, using class name and scope resolution :: operator.

Example :

#include<iostream.h>
#include<conio.h>
classsa
{
public:
staticint c;
int a;
public:
void read()
{
cout<<"Enter two values";
cin>>a>>c;
}
static void inc()
{
c++;
// a++;
}
void print()
{
cout<<"The static variable value is "<<c<<endl;
cout<<"The variable value is "<<a<<endl;
}
};
intsa::c;
void main()
{
clrscr();
sa s;
s.read();
s.print();
sa::inc();
cout<<"The static variable value is "<<sa::c<<endl;
cout<<"The variable value is "<<s.a<<endl;
s.inc();
cout<<"The static variable value is "<<s.c<<endl;
cout<<"The variable value is "<<s.a<<endl;
getch();
}

These functions cannot access ordinary data members and member functions, but only static data members and static member functions.

C++ Basics

What is C++ and History of C++
C++ is an object oriented programming language. It is a combination of C language and Simula67 language. Because C is having power features and simula67 is having basics of class. C++ was introduced by Bjarne Stroustrup in the year 1980 at AT & T (American Telephony and Telegraphy) bell labs, Murry hill, New Jersy, USA. At that time the name C with classes. After that they changed the name in 1983 c with classes to C++.
Applications of C++

  • C++ is a Superset to C language. Because all C programs run under C++. But C++ programs cannot run under C. So C is a Subset.
  • C++ is a free form language.
  • C++ is a robust language. Because it is having rich set of operators, built in functions, keywords etc.

C++ CharacterSet
    The c++ character set is ASCII character Set
  • Alphabets - A to Z(65 to 90) a to z(97 to 122)
  • Numbers - 0 to 9(48 to 57)
  • Symbols - All Keyboard Symbols
    • Space 32
    • Enter 13
    • Esc 27
    • etc

Variable It’s a memory allocation which enable you to store the data temporarily during the program execution. In other words a space to store the data. An identifier is nothing but user defined names.
Syntax:datatype variablename;
C++ data types It represents the type of value to be presented or stored in the variables. All the data types of C language is legal in C++. There are 3 types.
1. Built in Datatypes
2. User-defined Datatypes
3. Derived Datatypes
Built-in Datatypes:-
These are 3 types.
Integral datatypes
Floating point datatypes
void
Integral Data types:-
integer, character
Integer short form is int.
Character short form is char.
Int occupies 2 bytes space in the memory. Range is -32,768 to 32,767.
unsigned int accepts only positive values. Range is 0 to 65,535.
Long int occupies 4 bytes space in the memory. Range is -2,147,483,648 to 2,147,483,647.
Unsigned long int range is 0 to 4,294,967,295.
Character short form char. It occupies 1 byte space in the memory.
Unsigned char Range is 0 to 255.
Signed char Range is -128 to 127.
Floating
Void:- occupies 0 bytes.
User defined Data types:-
User defined data types are class, structure, enumeration, union.
Derived Data types:- Derived data types are pointer, array, function.
Tokens Smallest individual word is nothing but token. Tokens are 5 types:
1. Keywords
2. Identifiers
3. String
4. Constants
5. Operators
Key Words:- Keywords are nothing but reserved words. The meaning of those words is already defined to the compiler for the particular purpose. We can’t change the meaning of the keyword. But we can apply the functionality of the keyword to any name. There are 48 keywords in c++. In that 32 are C keywords and 16 are additional. Keywords should not be used as identifiers.
autobreakcasechar
constcontinuedefaultdo
DoubleElseenumextern
floatforgotoif
intlongregisterreturn
shortsignedsizeofstatic
structswitchtypedefunion
unsignedVoidvolatilewhile
asmnewdeleteoperator
templateprivatepublicprotected
friendvirtualthistry
throwinlinecatchclass

Identifiers:- Identifiers means any name except keywords.
Ex:- variable names, function names, label names, string names, array names, etc.
String:- String is nothing but character array. String is a collection of character.
Syntax:- datatype variablename[size];
Constants:-we can make any variable as a constant. And we can use literal constants in c++. Literal constants means making string constants are identifying the integer constants. Through enum it is possible.
syntax:- const datatype variablename; is used for making the single variable constant.
Dealing with input and output functions with c++ cout(console out):- cout is an object of ostream class. which is used to perform the output operation. The cout works exactly similar with printf function in the C-language. The cout will supports an operator called insertion or put to operator(“<<”). As per programming requirements we can use escape sequences but does not supports to work with format specifiers. Cin(consloe in):- It is an object of istream class which enable you to extract the value from the input device and assign to the specified variable. It will works almost similar to the scanf function in c-language. The cin object will supports an operator called extraction (or)get from operator(“>>”)
NOTE: Using multiple insertion(<<) with the respective of cout are using multiple extraction operator (>>) with the respective of cin is known as cascading. The cin is an object of istream class. The cout is an object of ostream class. Both istream and ostream classes are existed in the iostream.h header file.
Why to use void as a return type?
As we know that function means which returns a value. If you feel to write void as a return type at the time of compilation you are getting a warning “Function should return a value”.
Every Function will return int type bytes to operating system implicitly to kill the nature of returning we need to write void as return type. (void means “0” bytes data type)When you are not interested to write void as return type, recommended to write “return 0” at the end of the program.
//sample program
#include < iostream.h >
#include < conio.h >
void main()
{
clrscr();
cout<<“Welcome to C++”;
getch();
}

How to save the program:-
press functional key F2.
Then type the filename.cpp
Ex:- D:\1.cpp enter
How to compile the program?
Alt+C- enter - Compile enter
How to Execute the program?
Alt+R - Enter - Execute
How to see the output?
Alt+F5 - Output