Static data member in C++

When we declare a normal variable (data member) in a class, different copies of those data members create with the associated objects.

In some cases when we need a common data member that should be same for all objects, we cannot do this using normal data members. To fulfill such cases, we need static data members.

Declaration :
static data_type member_name;
Example :
static int a;

Static Member Functions in C++ :

By declaring a function member as static, you make it independent of any particular object of the class. A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator (::)

A static member function can only access static data member, other static member functions and any other functions from outside the class.

Static member functions have a class scope and they do not have access to the this pointer of the class.

Lets take an example :


                    #include <iostream>
                    using namespace std;
                    class Cube {
                          int side; // normal data member
                          static int objectCount;// static data member
                          // Constructor definition
                             // Increase every time object is created
                              // creating a static function that returns static data member
                              static int getCount() {
                             return objectCount;
                    // Initialize static member of class Box
                    int Cube::objectCount = 0;
                        int main(void) {
                           Cube c1;
                           // Object Count.
                           cout << "Total objects: " << Cube::getCount() << endl;
                         Cube c2;
                           // Object Count.
                           cout << "Total objects: " << Cube::getCount() << endl;
                           return 0;


Explaination :

If you check above program we have created a static member function named getCount() which returns the static data member objectCount value. Since getCount is a static member function, it can access only static data and can be directly called by using the scope resolution operator (::)

Note : A static member function is a special member function, which is used to access only static data members

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.

Lets take an example :
                  class Abc
                      int i;
                          cout << "constructor";
                          cout << "destructor";
                  void f()
                      static Abc obj;
                  int main()
                      int x=0;
                      cout << "END";

                  Output :
                  constructor END destructor .

You must be thinking, why was the destructor not called upon the end of the scope of if condition, where the reference of object obj should get destroyed. This is because object was static, which has scope till the program’s lifetime, hence destructor for this object was called when main() function exits.


Operator Overloading in C++

Very Important topic exam will 100 % ask objective on this so read carefully .

The meaning of an operator is always same for variable of basic types like: int, float, double etc. For example: To add two integers, + operator is used.

However, for user-defined types (like: objects), you can redefine the way operator works. For example:

If there are two objects of a class that contains string as its data members. You can redefine the meaning of + operator and use it to concatenate those strings.

This feature in C++ programming that allows programmer to redefine the meaning of an operator (when they operate on class objects) is known as operator overloading.

Why is operator overloading used? :

We can write any C++ program without the knowledge of operator overloading. However, operator operating are profoundly used by programmers to make program intuitive. For example, We can replace the code like:

                    calculation = add(multiply(a, b),divide(a, b));


                    calculation = (a*b)+(a/b);


Lets take an example of coding :

                    #include < iostream>
                    using namespace std;
                    class Test
                          int no;
                           Test(): no(5){}
                           void operator ++() 
                              no = no+1; 
                           void Display() { cout<<"no: "<< no; }
                    int main()
                        Test t;
                        // this calls "function void operator ++()" function
                        return 0;


Output :
                        no : 6 

Some Important Points to note :

Operator overloading allows you to redefine the way operator works for user-defined types only (objects, structures). It cannot be used for built-in types (int, float, char etc.).

Two operators = and & are already overloaded by default in C++. For example: To copy objects of same class, you can directly use = operator. You do not need to create an operator function.

Operator overloading cannot change the precedence and associatively of operators. However, if you want to change the order of evaluation, parenthesis should be used.

There are 4 operators that cannot be overloaded in C++. They are :: (scope resolution), . (member selection), .* (member selection through pointer to function) and ?: (ternary operator).

C++ Function Overloading :

In C++ programming, two functions can have same name if number and/or type of arguments passed are different.

These functions having different number or type (or both) of parameters are known as overloaded functions. For example:

Example :

                    int test() { }
                    int test(int a) { }
                    float test(double a) { }
                    int test(int a, double b) { }


See above all functions habe same name but different args .

Means functions overloading is possible but we need to pass different args .

Note : the return type of all these 4 functions are not same. Overloaded functions may or may not have different return type but it should have different argument(s).

Lets take an small program :
                    #include < iostream>
                    using namespace std;
                     void display(int);
                     void display(float);
                     void display(int, float);
                     int main() {
                         int a = 15;
                         float b = 0.5;
                         display(a);    // see here a is int 
                         display(b);    // here same function name with diff data type
                         display(a, b); 
                         return 0;
                     void display(int var) {
                         cout << "Integer number: " << var << endl;
                     void display(float var) {
                         cout << "Float number: " << var << endl;
                     void display(int var1, float var2) {
                         cout << "Integer number: " << var1;
                         cout << " and float number:" << var2;


Output :
                    Integer number: 15
                    Float number: 0.5
                    Integer number: 15 and float number: 0.5

Here, the display() function is called three times with different type or number of arguments.

The return type of all these functions are same but it’s not necessary.


Lets do an very Imp excerices for exam (Objective questions) :


1. Which of the following operator functions cannot be global, i.e., must be a member function.
                (A) new
                (B) delete
                (C) Converstion Operator
                (D) All of the above

                Answer : C 



2. Which of the following operators should be preferred to overload as a global function rather than a member method?
                    (A) Postfix ++
                    (B) Comparison Operator
                    (C) Insertion Operator <<
                    (D) Prefix++
                    Answer: (C) 

3. How does C++ compiler differs between overloaded postfix and prefix operators?
                    (A) C++ doesn’t allow both operators to be overlaoded in a class
                    (B) A postfix ++ has a dummy parameter
                    (C) A prefix ++ has a dummy parameter
                    (D) By making prefix ++ as a global function and postfix as a member function.

                    Answer: (B) 

4. Which of the following operators cannot be overloaded ?
                    (A) . (Member Access or Dot operator)
                    (B) ?: (Ternary or Conditional Operator )
                    (C) :: (Scope Resolution Operator)
                    (D) .* (Pointer-to-member Operator )
                    (E) All of the above

                    Answer: (E)