Skip to main content

C++ Cheat Sheet

 Friend Function:

  • Non-member function that can access members (private + protected) of class
  • Friend keyword is used during declaration only
  • Defined globally outside of class scope (it does not belong to any class)
  • Used in operator overloading
  • Not in java

Friend Class:

  • Can access private + protected members of other class without inheritance
  • Friendship is NOT mutual
  • Violates data hiding hence avoid

Static: When a variable in a class is declared static, space for it is allocated for the lifetime of the program. No matter how many objects of that class have been created, there is only one copy of the static member. So same static member can be accessed by all the objects of that class.


A static member function can be called even if no objects of the class exist and the static function are accessed using only the class name (A) and the scope resolution operator ::
cout<<A::a();

Abstract class : template for another class
A class is called an abstract class whose objects can never be created. Hence, it cannot be instantiated. Such a class exists as a parent for the derived classes. We can make a class abstract by placing a pure virtual function in the class.

Virtual function: a member function defined in base class and re-defined (overridden) in derived class. 
  • Runtime polymorphism
  • ​can't be static
virtual void fun(){
 }


A virtual function gets overridden in the derived class and instructs the C++ compiler to execute late binding on that function. A function call is resolved at runtime in late binding, so the compiler determines the object type at runtime.

We can call a virtual function from a constructor. But the behavior is a little different in this case. When a virtual function is called, the virtual call is resolved at runtime. It is always the member function of the current class that gets called. That is the virtual machine doesn’t work within the constructor. 

Pure virtual function has no implementation and is declared by assigning 0. It has no body.
virtual void fun()=0;
= sign has got nothing to do with the assignment, and value 0 is not assigned to anything. It simply tells the compiler that a function will be pure and it will not have anybody.

Pure virtual functions mean "You cannot create an object of this type".

Static functions mean "You can call this function without an object".
Constructer cannot be static. 

Inline function is expanded inline when invoked, thus saving time. The compiler replaces the function call with the corresponding function code, which reduces calling overhead. 

BFS is better when target is closer to Source. DFS is better when target is far from source. DFS uses less space.

Access specifiers:
  • Public: All members accessible outside the class.
  • Protected: All members accessible inside the class and to the derived class.
  • Private: All members not accessible outside the class.

Reference is like a pointer. It is another name of an already existing variable. We can declare an array of pointers but an array of references is not possible (because reference is not an object) .

Void pointer have no datatype associated with it & can hold addresses of any type.
void *ptr; 
char *str;
ptr=str;                // no error 
str=ptr;                // error because of type mismatch
We can assign a pointer of any type to a void pointer but the reverse is not true unless you typecast it as
str=(char*) ptr;

Constructor is automatically called when an object is first created. Similarly when an object is destroyed a function called destructor automatically gets called. A destructor has the same name as the constructor (which is the same as the class name) but is preceded by a tilde.

Destructor overloading is not possible. Destructors take no arguments, so there’s only one way to destroy an object. 

First, the most-base class(at the top of the inheritance tree) is constructed. Then each child class is constructed in order until the most-child class is constructed last. 
During the destruction exactly reverse order is followed. Destructor starts at the most-derived class and works its way down to base class

Comments

Popular posts from this blog

Data Representation

 Digital Number System In digital representation, various number systems are used. The most common ones are : Binary : base 2 (0, 1) Octal : base 8 (0, 1, 2, 3, 4, 5, 6, 7) Decimal : base 10 (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) Hexadecimal : base 16 (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F) Number Conversions : From Decimal to any system (base) (Decimal) 10 = (Any) base

Data Type Modifiers

 As the name suggests, data type modifiers are used with fundamental data types to modify the length of data that a particular data type can hold. Following 4 datatype modifiers are available in C++: Signed Unsigned Short Long

Selection statement

  If statement  If tests a particular condition, if the condition evaluates to true then set of statements under the body of 'if' statement will be executed otherwise it will be ignored by the compiler. Syntax : if(<condition>) {      statements; } <> : programmer defined If else statement If we use 'else' statement with 'if' statement then if the condition of 'if' statement evaluates to false, in that case body of else statement will be executed, otherwise it will be ignored. Syntax :  if(<condition>) {      statements; } else {     statements; } If-else-if ladder If we use 'else if' statement with 'if' statement then if any condition evaluates to true, in that case its associated statement will be executed and all other 'else if' and 'else' statement will be ignored by the compiler. Syntax :  if(<condition>) {      statements; } else if(<condition>) {    statements; } els...