






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Usman Younis
Inheritance – to – Polymorphism
Usman Younis
Using Base class’s pointer
class Base { public: Base()
class Derv1:public Base { public: Base() Derv1() { cout<<"this is base \n"; }
void a_function() { cout<<"I am base class function \n"; }
~Base()
Derv1() { cout<<"this is Derv1 \n"; }
void a_function() { cout<<"I am a Derv1 class function \n"; }
void a2_func()
Usman Younis
{ cout<<"Base is now destructed \n"; } };
{ cout<<"Another function \n"; }
~Derv1() { cout<<"Derv1 is now destructed \n"; } };
Base Class’s Pointer (Contd..)
class Derv2: public Derv
void main() { Base* ptr; { public: Derv2() { cout<<"this is Derv2 \n"; }
void a_function() { cout<<"I am a Derv2 class function \n"; }
Derv2 d;
ptr = &d;
ptr->a_function();
t D 1
Usman Younis
function \n ; }
~Derv2() { cout<<"Derv2 is now destructed \n";} };
ptr = new Derv1;
ptr->a_function();
}
Base Class’s Pointer (Contd..)
In the previous example, the pointer is statically bound to the Base classstatically bound to the Base class, even even when the newly created derived is pointed by it This is called compile-time or static-binding, i.e, the variables are bound to a specific type when the code is compiled
Usman Younis
yp p
This binding can be changed at the run- time, called dynamic- or late-binding. Working towards virtual functions
Virtual Functions
A virtual function is a function that is declared with a keyword virtual in the base class and then re-a keyword virtual in the base class and then re defined by the derived class When a virtual function is invoked by a base class pointer, the type of an object being pointed at determines which version of the function is called The keyword virtual is only required in the base
Usman Younis
class, but many programmers use it in derived classes, simply to remind themselves that the function is virtual
Virtual Functions (contd..)
Used to support polymorphism with pointers and referencesand references
Ensures derived class function definition is resolved dynamically
When you declare a function as a virtual in the base class, you indicate to the compiler
Usman Younis
that you want dynamic binding for that function in any class that is derived from this base class
Virtual Functions
Not every function can be declared virtual
Following functions cannot be virtualll f b l
Non-member functions Static member functions Constructors, and Friend functions
Usman Younis
Virtual Destructors
class A { public:
virtual ~A () {cout<<" ~A";} }; class B : public A { public: B () :A() {cout<<" B";} virtual ~B() {cout<<" ~B";} };
Usman Younis
}; void main () { A *ap = new B; //prints “A B” delete ap; //prints “~B ~A” //would have only printed //"~A“ if non-virtual }
Pure Virtual Functions
Usman Younis
Pure Virtual Functions
Pure virtual functions are used to give an abstract view of the class hierarchyabstract view of the class hierarchy
In this case base is only used to access the derived classes, and it (base) is never instantiated
Example:
Usman Younis
Please see the handout “Polymorphism - 2”
Abstract Class Constructor
Usman Younis
Summary
Usman Younis