Wednesday, August 7, 2013

VIRTUAL FUNCTION - c++


VIRTUAL FUNCTION :- 

Virtual Function is a function that is declared within a base class and redefined in the derived class. Virtual functions are declared by preceding the class declaration with a keyword "virtual". When a virtual function is declared C++ decides to execute a function based on the type of object pointed by the base pointer and not on the type of pointer.
Example:
#include <iostream.h>
#include<conio.h>
#include<stdio.h>
class BB
  {
    public:
       void  show() { cout  << " BASE BASE\n" ; }
       virtual void print() { cout << "base base\n"; }
  };
class DD : public BB
  {
    public:
      void show() { cout << "DERIVED DERIVED\n"; }
      void print() { cout << "derived derived\n"; }
  };
int main()
  {
    
     BB B;
     DD D;
     BB *ptr;
    
     cout << "ptr points to base class\n" ;
     ptr =  &B;
     ptr->show();
     ptr->print();
  
     cout << "ptr points derived class\n";
     ptr = &D;
     ptr->show();
     ptr->print();
     getch();
     return 0;
  }

OUTPUT:
ptr points to base class
BASE BASE
base base
ptr points derived class
BASE BASE
derived derived

Sunday, July 28, 2013

Difference Between Pointers and References


               REFERENCES                                         VS                              POINTER
        

* We can not change the location to which the reference belongs that is why we have to initialize the references when we do declare them unlike we can change the locations to which the  pointer belongs.

* We have  to dereference the pointers explicitly but the references are pre-dereferenced.

* we cant use ++ on reference variables but we can use ++operator on pointers(as they then after points to the next memory of its type).

Upcoming Student career counselling implementation

I've been working on student career counselling and will be coming with its implementation in few more days...