virtual function and pointer to object

28
Virtual function and pointer to object

Upload: miggi0007

Post on 12-Nov-2015

229 views

Category:

Documents


2 download

DESCRIPTION

Virtual Function and Pointer to Object

TRANSCRIPT

  • Virtual function and pointer to object

  • Pointer to derived class objectClass base1{Public:Void display(){Cout
  • Void main(){base1 *ptr;der1 d;ptr=&d;ptrdisplay();getch()}OutputBase class displayed

  • Memory managementThe basic idea of new and delete is simple: new creates an object of a given type and gives a pointer to it, and delete destroys an object created by new, given a pointer to it. The reason that new and delete exist in the language is that code often does not know when it is compiled exactly which objects it will need to create at runtime, or how many of them. Thus new and delete expressions allow for "dynamic" allocation of objects.

  • int main(){ int * p = new int(3); int * q = p; delete q; // the same as delete p return 0; }

  • VIRTUAL FUNCTIONS

  • Virtual functionIt is the member function declared in the base class using the key word virtual. whose functionality redefined in the derived class.

  • DefinitionVirtual means existing in appearance but not in reality. Virtual functions are used in late binding or dynamic binding.The concept of pointers play important role in the virtual functions.To make member function virtual, keyword VIRTUAL is being used.

  • Static BindingBinding means linking.In c++, programs choosing the function in a normal way during the compilation time is called the static binding or static linking or compile time binding or early binding.In this, compiler determines which function is to be used based upon the parameters passed to the function.

  • Late Binding or Dynamic Bindingvirtual function is an example of late binding or dynamic binding/run time polymorphism. The linking of the functions at run time is called run time binding.

  • Class base1{Public:Virtual Void display(){Cout
  • Void main(){base1 *ptr;der1 d;ptr=&d;ptrdisplay();getch()}Outputderived class displayed

  • Pure Virtual FunctionA pure virtual function is a virtual function which has no body i.e no arguments, no variables and no expressions or statements inside it.It has only declaration but no definition.Pure virtual functions can be declared inside and outside the class.

  • Why we use Pure virtual function??To make the code simpler.To access the members of derived class through the pointer of base class, we need virtual functions.But if the virtual function is of no use, because it will not be called then we make it as pure virtual function.

  • Abstract base classThe class in which at least one pure virtual function declared is called an abstract base class.We can not create object of the abstract class, but we can create pointer to that class.A class that does not have a pure virtual function is called a concrete class.

  • Pure Virtual Function Is a function without a body Is created by adding the notation =0 to the virtual function declaration Example: virtual int calc_net_salary()=0;

  • class shape{protected:int a,b;public:void read(){cin>>a>>b;}virtual void cal_area()=0;};

  • class rectangle:public shape{void cal_area(){doubles area=a*b;cout
  • int main(){shape *ptr[2];rectangle r1;cout
  • Enter leng and breadth 10 20Enter base and perpendicular 5 20Area of rectangle=200Area of trinagle=50

  • Virtual destructor"A destructor is a member function of a class, which gets called when the object goes out of scope". This means all clean ups and final steps of class destruction are to be done in destructor. A virtual function is something which helps a derived class in overriding the implementation of a functionality of a base class.The order of execution of destructor in an inherited class during a clean up is like this. 1. Derived class destructor 2. Base class destructor

  • A difference between a destructor (of course also the constructor) and other member functions is that, if a regular member function has a body at the derived class, only the version at Derived class gets executed. Whereas in case of destructors, both derived as well as base class versions get executed.

  • #include class Base { public: Base(){ cout
  • the constructors are getting called in the proper order. But to the dread of a programmer of a large project, the destructor of the derived class was not called at all.This is where the virtual mechanism comes into our rescue. By making the Base class Destructor virtual, both the destructors will be called in order. The following is the corrected sample.

  • #include class Base { public: Base(){ cout
  • If the destructor in the base class is not made virtual, then an object that might have been declared of type base class and instance of child class would simply call the base class destructor without calling the derived class destructor.Hence, by making the destructor in the base class virtual, we ensure that the derived class destructor gets called before the base class destructor.

  • Object SlicingWhen a Derived Class object is assigned to Base class, the base class' contents in the derived object are copied to the base class leaving behind the derived class specific contents. This is referred as Object Slicing. That is, the base class object can access only the base class members. This also implies the separation of base class members from derived class members has happened."Slicing" is where you assign an object of a derived class to an instance of a base class, thereby losing part of the information - some of it is "sliced" away.

  • Class Base { public: int i;Base(){i=10; }};class Derived : public Base { public: int j;Derived(){i=20;j=30; }}; int main() { Base B1; Derived D1; B1 = D1; //only i is copied to B1 }