Some C++ Tickles..



To start with, Of course, oops..
Classes, Objects, Inheritance,  Abstraction, Encapsulation, Polymorphism, Overloading, and Re-usability.


Encapsulation and abstraction
Encapsulation is the hiding of internal complexity. Abstraction is the presentation of concept(s) or object(s) in its simplest possible way with out detailing (Not the actual implementation).


The Four Polymorphisms
  1. Subtype polymorphism is AKA runtime polymorphism.
          Accessing derived classes through base class pointers and references
  2. Parametric polymorphism is AKA compile-time polymorphism.
          Using the same code regardless its type. ie Templates.
  3. Ad-hoc polymorphism AKA  overloading.
          Same name but different functions.
  4. Coercion AKA implicit or explicit casting.
          An object or a primitive is cast into another object type or primitive type.


Type casting
implicit, explicit (C - like), dynamic_cast (With RTTI check), static_cast (no RTTI check), reinterpret_cast (any pointer type to any pointer type), const_cast (removes constantness of object and vice versa)


Storage classes
A storage class defines the scope (visibility) and life time of variables and/or functions within a C++ Program. Auto, register, static, extern


Volatile..
To signify to the compiler, not to optimize use of the variable, that is declared volatile.


How to call C / C++ function from  C / C++ code ?
Declare the function  extern "C" (in your C++ code) and call it (from your C or C++ code).


'explicit' is used for..
To make the constructors to be non-converting. Single argument constructors can be initialised like obj(x) or obj = x. 'explicit' prevents this implicit conversion.


Assignment operator and copy constructor
The assignment operator is used to copy the values from one object to another already existing object. A copy constructor initializes a new object from an existing object.


When is assignment operator is called and when is copy constructor ?
If a new object has to be created before the copying can occur, the copy constructor is used else,the assignment operator.


When do we need user-defined copy constructor than default copy constructor ?
Default copy constructor will do a bit-wise (member-wise) copy of an object. (The destructor of one destroys the other,in this case, since both represents same memory). When this is inappropriate,use user defined one.  


When do we need user-defined  Assignment operator  than default Assignment operator ?
Same reason as user defined copy constructors - When a bit-wise copy is inappropriate.


Static Constructor and static destructor.
Constructor and destructor are used to create or destruct instance of object. It's pointless to call them without corresponding object instance.


Const constructor and Volatile constructor.
The only legal storage class for a constructor is inline. There are no const or volatile constructors in C++. 
However, Constructors are implicitly constant members of a class. They do not modify objects, but initialize. So const and non const objects can invoke them.


Private constructor and private destructor.
A class with only non-public constructors cannot be constructed directly by a user. A class with only non-public destructor cannot be destroyed directly by a user.
Private constructors prevent creation of objects (unless the class has a friend class or a friend function that uses its private constructors. ( Also, hiding the constructors and assignment operator can be useful to create singleton class )


What is a singleton class and multiton pattern?
A class whose number of instances that can be instantiated is limited to one is called a singleton class. Thus, at any given time only one instance can exist, no more.
Multiton pattern keeps a map(Key-value pair) of named instances.


virtual destructor : when is it needed?
When we delete derived class / call destructor of derived through base class pointer, we need virtual destructor.


Virtual Constructor : Why we can n't have it? 
To construct an object,the exact type of the object is needed. But 'virtual' makes it polymorphic (dynamic).
And what to do with vptr & vtable when an object is not created?!
Logically : Declaring something virtual in C++ means that it can be overridden by a sub-class of the current class and the latest declaration will be called when accessed through the base class. There is no such need for a constructor.


Why we shouldn't call virtual functions during construction or destruction ?
Virtual makes the behaviour 'dynamic' and can't guarantee the complete initialisation of base class when the derived class constructor is called.To create the object ,before the derived class constructor is called, the base class constructor must be initiated properly. 


Virtual constructor Also Known As : Factory Method of initialization
An idiom means to create a copy of an object or a new object without knowing its concrete type.


Move Constructor :
An idiom intent to to transfer the ownership of a resource held by an object to a new object.


Automatic pointer :
auto_ptr class provides a limited garbage collection facility.Deletes the object automatically when the auto_ptr is deleted.
No two auto_ptr can't  possess the ownership of same object.


Array and Vectors :
Vectors are dynamic arrays and are dynamically resizeable. Arrays are slightly better in performance and stored in contiguous memory locations.


Friend :
A friend class / function  can access the "private","public" and "protected" members of the class in which it is declared as a friend.


Static classes and object :
There are no static classes.but a static object is one with global lifetime, but constrained scope.


Objects with static members :
static keyword specifies that,only one copy of the member is shared by all instances of the class.


Why Static functions can not access non-static members?
Static members can access non static members!! But a static member function cannot access an instance member because the function does not have an implicit 'this' pointer.


Global and static global :
Static global has file scope and it can't be accessed outside of the file.Global has program scope and can be accessed outside the file in which it has been defined using 'extern'.


"inline" functions :
The compiler inlines the function, it replaces every call of that function with the actual body. Just like macro. Compiler can ignore this optimisation as well.


Use of "inline" functions :
Reducing the extra overhead created by the function call (placing data over stack and retrieving the result ).


Malloc and free  , new and delete . Can we use new and free, together ?
Bad idea! malloc & free work on raw memory. Where new and delete calls constructors and destructors respectively. Override new and delete if necessary.

No comments:

Post a Comment