How to overload main
To overload main, it is necessary to use class & declare main as member function
class Overloading1{
public static void main(int a){
System.out.println(a);
}
public static void main(String args[]){
System.out.println("main() method invoked");
main(10);
}
}
New is a c++ operator which will dynamically allocate memory and call constructor of the class to initialize memory for which object is created. It Doesn’t need size and gets casted appropriately and returns the exact datatype.

Ifdef -> if following is defined
Ifndef -> if following is not defined
Commenting code
#if 0
Commented code
#endif

Constructor
Copy constructor – creates object by initializing it with an object of same class, which was already created. The copy constructor is used to:
- Initialize one object from another of the same type.
- Copy an object to pass it as an argument to a function.
- Copy an object to return it from a function.
Example:
classname (const classname &obj) {
// body of constructor
}
Conversion constructor – if class has constructor with 1 argument, it is conversion constructor as it allows automatic conversion of the class being constructed.
Example
class Test
{
private:
int x;
public:
Test(int i) {x = i;}
void show() { cout<<" x = "<<x<<endl; }
};
int main()
{
Test t(20);
t.show();
t = 30; // conversion constructor is called here.
t.show();
getchar();
return 0;
}
Static member function can’t be constant or volatile as const affects the this pointer of nonstatic function whereas static function doesn’t have this pointer.
No virtual constructor is allowed but virtual destructor is allowed to remove object from derived class.
Virtual constructor
“virtual” allows us to call a function knowing only any interfaces and not the exact type of the object. To create an object you need complete information. In particular, you need to know the exact type of what you want to create. Consequently, a “call to a constructor” cannot be virtual.
Virtual destructors are used to delete an instance of a derived class through a pointer to base class:
class Base
{
// some virtual methods
};
class Derived : public Base
{
~Derived()
{
// Do some important cleanup
}
}
Base *b = new Derived();
// use b
delete b; // Here's the problem!
Since Base’s destructor is not virtual and b is a Base* pointing to a Derived object, delete b has undefined behaviour. In most implementations, the call to the destructor will be resolved like any non-virtual code, meaning that the destructor of the base class will be called but not the one of the derived class, resulting in resources leak.
To sum up, always make base classes’ destructors virtual when they’re meant to be manipulated polymorphically.
If you want to prevent the deletion of an instance through a base class pointer, you can make the base class destuctor protected and nonvirtual; by doing so, the compiler won’t let you call delete on a base class pointer.
Abstract class
Abstract class has atleast one pure virtual function as any class with at least 1 pure virtual function must be abstract because it needs to be extended so that method can actually be implemented.
An abstract method is just another way of describing the characteristics of a pure virtual function. Both just mean a method with no implementation provided that needs to be implemented in a sub-class before the class can actually be instantiated.
If we do not override the pure virtual function in derived class, then derived class also becomes abstract class.
Characteristics
- Classes inheriting an abstract class must provide definition to the pure virtual function, otherwise they will also become abstract class.
- Abstract classes can’t be instantiated, but pointers and references of Abstract class type can be created.
- Abstract can have normal functions and variables along with pure virtual function.
Virtual function
Virtual function whose behavior can be overridden within inherited class by function with same signature.
A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have implementation, we only declare it. A pure virtual function is declared by assigning 0 in declaration. See the following example.
// An abstract class
class Test
{
// Data members of class
public:
// Pure Virtual Function
virtual void show() = 0;
/* Other members */
};
Storage class
A storage class defines the scope (visibility) and life-time of variables and/or functions within a C++ Program. Types are:-
- auto – default storage class for all local variables.
- Register – should be stored in a register instead of RAM. Can’t have the unary ‘&’ operator as it doesn’t have memory location. should only be used for variables that require quick access such as counters
- Static – instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope. The static modifier may also be applied to global variables. When this is done, it causes that variable’s scope to be restricted to the file in which it is declared.
- Extern – give a reference of a global variable that is visible to ALL the program files. When you use ‘extern’ the variable cannot be initialized as all it does is point the variable name at a storage location that has been previously defined.
- Mutable – Mutable means “subject to change or alteration.” The mutable keyword allows constant member functions to modify mutable data members of constant structures and objects.
Extern
If you want a variable to be accessible across multiple files then define the variable as extern int count in a header file and import it across multiple files.
First File:
int count ;
extern void write_extern();
main()
{
count = 5;
write_extern();
}
Second File:
extern int count;
void write_extern(void)
{
std::cout << "Count is " << count << std::endl;
}

Static
- If the static keyword is applied to a class, all the members of the class must be static.
- Static methods can only access static members of same class. Static properties are used to get or set the value of static fields of a class.
- Static constructor can’t be parameterized. Access modifiers can’t be applied on Static constructor, it is always a public default constructor which is used to initialize static fields of the class.
Mutable
There are some cases where data members are declaring as const, and later want to change the value of the const variable.In C++, it is not legal to change the const value, by using mutable it is possible. This keyword can be applied to only non-static and non-constant data members.
The mutable keyword allows constant member functions to modify mutable data members of constant structures and objects.
class mute{
public:
int x ;
mutable int y; // mutable variable declaration
mute() // constructor
{
x=10;
y=10;
}
};
int main()
{
const mute m; // constant object
//m.x=20; // Illegal cant change the data members in a constant object
m.y=20; // Legal, we can change , because its a mutable
}
Volatile
volatile keyword is intended to prevent the compiler from applying certain optimizations which it might have otherwise applied because ordinarily it is assumed variables cannot change value “on their own”.
Consider this code:
int some_int = 100;
while(some_int == 100)
{
//your code
}
When this program gets compiled, the compiler may optimize this code, if it finds that the program never ever makes any attempt to change the value of some_int, so it may be tempted to optimize the while loop by changing it from while(some_int == 100) to simply while(true) so that the execution could be fast.
However, sometimes, optimization (of some parts of your program) may be undesirable, because it may be that someone else is changing the value of some_int from outside the program which compiler is not aware of, since it can’t see it; but it’s how you’ve designed it. In that case, compiler’s optimization would not produce the desired result!
So, to ensure the desired result, you need to somehow stop the compiler from optimizing the while loop. That is where the volatile keyword plays it’s role. All you need to do is this,
volatile int some_int = 100;

Operator overloading
Done so as to use operators with user-defined types or objects.
Overloaded operators are functions with special names the keyword operator followed by the symbol for the operator being defined.
class temp
{
private:
int count;
public:
temp():count(5){ }
void operator ++() {
count=count+1;
}
void Display() { cout<<"Count: "<<count; }
};
int main()
{
temp t;
++t; /* operator function void operator ++() is called */
t.Display();
return 0;
}
NOTE
- Operator overloading cannot be used to change the way operator works on built-in types. Operator overloading only allows to redefine the meaning of operator for user-defined types.
- There are two operators assignment operator(=) and address operator(&) which does not need to be overloaded. Because these two operators are already overloaded in C++ library. For example: If obj1 and obj2 are two objects of same class then, you can use code obj1=obj2; without overloading = operator. This code will copy the contents object of obj2 to obj1.
- Operator overloading cannot change the precedence of operators and associativity of operators. But, if you want to change the order of evaluation, parenthesis should be used.
- The operators that cannot be overloaded in C++ are ::(scope resolution), .(member selection), .*(member selection through pointer to function) and ?:(ternary operator).
Inline function
If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time which reduces the overhead of function calling. Inline function increases efficiency but if we make large function inline then it might lead to code bloat and affect speed as well. Call infine where function size is small and is getting called quite often.
Inline is a request to the compiler. Very complicated function like Recursive function, function containing static variable, function containing return statement or loop or goto or switch statements are not made inline even if we declare them so.Also small functions which are defined inside a class (ie their code is written inside the class) are taken as inline by the compiler even if we don’t explicitly declare them so. They are called auto inline functions.
Static class
There are no static class in java or c++. There are static nested classes in Java.
But a class can be simulated to behave like a static class like this:
- Declare your class final – Prevents extension of the class since extending a static class makes no sense
- Make the constructor private – Prevents instantiation by client code as it makes no sense to instantiate a static class
- Make all the members and functions of the class static – Since the class cannot be instantiated no instance methods can be called or instance fields accessed.