Polymorphism
Polymorphism
Polymorphism
base class
animal
Shape
void show()
{ cout << “Derv2\n”; }
};
Virtual pointer access
Static binding/ Early binding/Compile time
polymorphism
• Eg
• der1.show()
• der2.show
Dynamic binding/late binding/Run time
polymorphism
• Virtual function can be invoked using base
class pointer to a derive class object
– basePtr—>draw()
• Now the correct derived class draw
function will be selected dynamically
(execution time)
• This is called dynamic or late binding
• At run time, when it knows what class is
pointed by basePtr, the show() function of
that class is called
Allowable assignments b/w base and
derived class object and pointer
• Four ways to aim base and derive class
pointer at base and derived class objects
– Base class pointer to base class objects
– Derived class pointer to derived class objects
– Base class pointer to derive class objects
• Derive class object is also object of base class
– Derive class pointer to base class object.
• Not allowed
• “is a” relationship applies only from derive class to
base class not vice versa
Abstract Classes and Pure Virtual
class Base { Functions
public:
Pure virtual function
virtual void show() = 0;
}; main() {
Base* arr[2]; 0 1
class Derv1 : public Base {
arr[2]
public: Derv1 dv1;
void show() Derv2 dv2;
{ cout << “Derv1\n”; } arr[0] = &dv1;
}; arr[1] = &dv2; dv1 dv2
class Derv2 : public Base { arr[0]->show();
public: arr[1]->show(); Program Output
void show() } Base b; Derv1
{ cout << “Derv2\n”; } // Derv2
};
Note: objects of abstract class cannot be created
Note
• After aiming a base-class pointer at a
derived-class object, attempting to
reference derived-class only members
with the base-class pointer is a
compilation error
class B { class Derv1 : public B { main(){
public: public: B *ptr;
virtual void show() = 0; void show() Derv1 dv1;
}; { cout << “Derv1\n”; } ptr = &dv1;
void display() ptr—>show();
{cout<<“hello derv1”; } ptr—>display();
}; }
Virtual Destructors
• A problem can occur when using
polymorphism to process dynamically
allocated objects of a class hierarchy
• E.g. applying delete operator to a base
class pointer, which point to derived class
objects
– B *ptr;
– Derv1 dv1;
– ptr = &dv1;
– delete ptr;
• Solution : virtual destructor
Cont.
• A virtual destructor in base class makes all
derived class destructor virtual
– B *ptr; Derv1 dv1;
– ptr = &dv1;
– delete ptr;
• Now destructor for appropriate class is called
• When derived class object is destroyed, base
class part of derive class object is also
destroyed
• Base class destructor is executed after
derived class destructor
Example – virtual destructor
class Base { Without virtual Destructor
public: *pBase Object of
virtual ~Base() Derv
{ cout << “Base Object of
destroyed\n”; } Derv
}; Program Output
class Derv : public Base { Base destroyed
public:
~Derv() With virtual Destructor
{ cout << “Derv
Object of
destroyed\n”; } *pBase
Derv
};
main() { Program Output
Base* pBase = new Derv; Derv destroyed
delete pBase; Base destroyed
}
Virtual base classes
• A problem can arise if a member function
in the Grandchild class wants to access
data or functions in the Parent class
When the Child1 and
Child2 classes are
derived from Parent,
each inherits a copy of
Parent; this copy is
called a subobject
Cont.
class Parent {
protected: basedata
int basedata;
};
class Child1 : public Parent
{ };
class Child2 : public Parent basedata basedata
{ };
class Grandchild : public Child1,
public Child2 {
public: basedata
int getdata()
{ return basedata; }
}; This situation is ambiguous, and
that’s what the compiler reports
Cont.
class Parent { keyword virtual in these
protected: two classes causes them
int basedata; to share a single common
}; Sub object of their base
class Child1 : virtual public Parent class Parent
{ };
class Child2 : virtual public Parent
{ };
class Grandchild : public Child1,
public Child2 {
public:
int getdata()
{ return basedata; }
};
Friends function
• Non member function should not be able to
access the private data of that class.
• However there are situations to where these
restriction lead to considerable
• Imagine a function to operate on object of
two different classes.
– Void display(alpha a,beta b)
– Display() must be a friend function of alpha and
beta class.
class Beta class Beta {
class Alpha { private:
private: y:
x: public:
public: beta(){
Alpha(){ y=7;
x=3; }
} friend frifunc(alpha,beta);
friend frifunc(alpha,beta); };
};
int frifunc(alpha a,beta b)
{
return a.x+b.y.
};
Thanks
Any Question