Thursday 19 April 2018

CPP


 Static elements are allocated storage only once in a program lifetime in static storage area. And they have a scope till the program lifetime. Static Keyword can be used with following,
  • static member functions do not have this pointer.
  • A static member function cannot be virtual
  • Member function declarations with the same name and the name parameter-type-list cannot be overloaded if any of them is a static member function declaration.
  • static member function can not be declared const, volatile, or const volatile.

 static function

In C language if the keyword static appears before any function declaration, it means that the function will now have a file scope or private scope. That is, the static function can be accessed only in the file that declares it. This means, the function is not known outside its own file. Another file may use the same name for a different function.
However in C++ the meaning of static is not the same. Here static member functions are those functions that can be called without any object creation. Scope resolution operator is used to call a static member function.

CONST

If a member function of a class does not alter any data in the class, then this member function may be declared as a constant function using the keyword const.
Consider the following declarations:

int max(int,int) const;
void prn(void) const;

Once a function is declared as const, it cannot alter the data values of the class. The complier will generate an error message if such functions try to change the data value.
-----------------------

Polymorphism is the ability for a message or data to be processed in more than one form.
Polymorphism is the property by virtue of which the same message can be sent to objects of several different classes, and each object can respond in a different way dependent on its class.

-------------

vfptr, vftable, virtual functions

C++ compiler creates a hidden class member called virtual-pointer or in short vfptr when there are one or more virtual functions. This vfptr is a pointer that points to a table of function pointers. This table is also created by compiler and called virtual function table or vftable. Each row of the vftable is a function pointer pointing to a corresponding virtual function.
VFPTRVFTABLEFUNCTION
b.vfptr ->Vftable[0] ->base::funct1()
Vftable[1] ->base::funct2()
To accomplish late binding, the compiler creates this vftable table for each class that contains virtual functions and for the class derived from it. The compiler places the addresses of the virtual functions for that particular class in vftable.


Virtual Table:
Every class that uses virtual functions (or is derived from a class that uses virtual functions) is given it's own virtual table as a secret data member.

This table is set up by the compiler at compile time.



A virtual table contains one entry as a function pointer for each virtual function that can be called by objects of the class.
Virtual table stores NULL pointer to pure virtual functions.


_vptr :
This vtable pointer or _vptr, is a hidden pointer added by the Compiler to the base class. And this pointer is pointing to the virtual table of that particular class.



#include

class Base  
 {  
 public:  
    virtual void function1() {cout<<"Base :: function1()\n";};  
    virtual void function2() {cout<<"Base :: function2()\n";};  
    virtual ~Base(){};
};  
   
class D1: public Base  
{  
public:  
   ~D1(){};
   virtual void function1() { cout<<"D1 :: function1()\n";};
};  
  
class D2: public Base  
{  
public:  
   ~D2(){};
   virtual void function2() { cout<< "D2 :: function2\n";};  
};  

int main()
{
  D1 *d = new D1;;
  Base *b = d; 

  b->function1();
  b->function2();

  delete (b);
  
  return (0);
}

output:
D1 :: function1()
Base :: function2()

[รข€‹IMG]
------------------
Early binding: When a non virtual class member function is called, compiler places the code to call the function by symbol name. Thus the function call jumps to a location that is decided by compile time or link time.


Late binding: When virtual function call is made through a base-class pointer, the compiler quietly inserts code to fetch the VPTR and look up the function address in the VTABLE, thus calling the right function and this is called late/dynamic binding.


---------
Virtual destructor in base class ensures that the destructor of derived class will be called when using base pointer to the object.


---------------
try
{
 //: throw exception;
}
catch(type exception)
{
  //code to be executed in 
  case of exception. 
}



Throw: This statement is used to throw an exception.

  • A catch expression, which corresponds to a specific type of exception that may be thrown by the �Try� block.
Try: A �try� block is a group of C++ statement enclosed in curly braces {}, which may cause (or throw) an exception. 



What is mutable keyword and how is it used?

The data members of a constant object cannot be changed. If we want some data members to change even when the object is constant, it can be using mutable keyword. The following function shows the use of mutable keyword:


---------------
The explicit specifier specifies that a constructor doesn't allow implicit conversions or copy-initialization.
explicit B(int) { }
    explicit B(int, int) { }
    explicit operator bool() const { return true; }
















//  B b1 = 1;      // error: copy-initialization does not consider B::B(int)
    B b2(2);       // OK: direct-initialization selects B::B(int)
    B b3 {4, 5};   // OK: direct-list-initialization selects B::B(int, int)
//  B b4 = {4, 5}; // error: copy-list-initialization does not consider B::B(int,int)
    B b5 = (B)1;   // OK: explicit cast performs static_cast
    if (b2) ;      // OK: B::operator bool()
//  bool nb1 = b2; // error: copy-initialization does not consider B::operator bool()
    bool nb2 = static_cast<bool>(b2); // OK: static_cast performs direct-initialization

-----------------


C++ Run Time Type Information Support also called RTTI. typeid() is a utility operator using which we can print the data type of the object during run time.
syntax: typeid().name()









No comments:

Post a Comment