Pages

Sunday, 24 August 2014

pointers and Polymorphism

Polymorphism
Compile time Polymorphism: early binding, static binding or static linking here objects are bound to its functions at compile time.
Run time polymorphism:  late binding selection of the appropriate function is done at rum time.

Callback functions: pointer to function (cannot be dereferenced)
return_type (*func_pointr_name) (arg_list) ;

Creating array of object using pointers
classname *ptr = new classname [10] ; [default constructor should be present in the class]
we can also achieve the same in the following way
classname *ptr[10];
ptr[0]= new classname ; // each member of the array is individually initialized
ptr->show () ; ptr ++; //will point to the next object in the array

this pointer: used to represent the object using which the member function is invoked.

If a base class pointer is used to point to a derived class object then only those members which are inherited from the base class can be accessed. If a member has the same name in the derived class then the pointer will always access the base class member.
But if casting is used then the derived member can be accessed in the case of overidding
((DC*)bptr)->show();

Virtual Functions: If the function declaration in the base class is preceded with keyword virtual  then the C++ determines which function to use in run time  based on which object the base pointer points to. if it isn’t a virtual function then the base pointer will always call the function in the base, even it points to an object  of the derived class.
Rules For virtual functions:
Pure virtual function:   virtual void display () =0 ;  a class containing one or more pure virtual functions is called an Abstract class….used for inheritance , no object can be created.

No comments:

Post a Comment