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;
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.
#include <iostream> using namespace std; class Cube { private: int side; // normal data member public: static int objectCount;// static data member // Constructor definition Cube() { // Increase every time object is created objectCount++; } // 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; }
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 (::)
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.
class Abc { int i; public: Abc() { i=0; cout << "constructor"; } ~Abc() { cout << "destructor"; } }; void f() { static Abc obj; } int main() { int x=0; if(x==0) { f(); } 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.
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.
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)); With calculation = (a*b)+(a/b);
#include < iostream> using namespace std; class Test { private: int no; public: Test(): no(5){} void operator ++() { no = no+1; } void Display() { cout<<"no: "<< no; } }; int main() { Test t; // this calls "function void operator ++()" function ++t; t.Display(); return 0; }
no : 6
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).
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 .
#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; }
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.
(A) new (B) delete (C) Converstion Operator (D) All of the above Answer : C
(A) Postfix ++ (B) Comparison Operator (C) Insertion Operator << (D) Prefix++ Answer: (C)
(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)
(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)