Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
This lecture was delivered by Prof. Usman Younis at Quaid-i-Azam University. This lecture covers following points of course Object Oriented Programming using C plus plus: Operator, Overloading, Member, Functions, Argument, Friend, Private, Complex, Compiler
Typology: Slides
1 / 10
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
Usman Younis
Base Class’s Pointer (Contd..)
Base class’s pointer can access any of its derived classderived class, doesn’t matter where it is doesn’t matter where it is located down the hierarchy
However, it is not possible other way round
The inheritance should be public (access specifier)
Usman Younis
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 and Polymorphism
class Access { public:
class subtraction: public Access { private: public: int x; virtual int result(int a) { return 0; } };
class addition: public Access { private:
int x; public: subtraction() : x(20) { } int result(int a) { return x - a; } }; class division: public Access { private:
Usman Younis
private: int x; public: addition() : x(20) { } int result(int a) { return x + a; } };
private: int x; public: division() : x(20) { } int result(int a) { if(a != 0) return x / a; else return 0; } };
Virt Funcs & Polymorphism (Contd..)
void main() { Access* access;
addition add; subtraction sub; division div;
access = &add;
Usman Younis
access = ⊂ cout<<access->result(10)<<endl;
access = ÷ cout<<access->result(20)<<endl; }
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 Classes
class an_Abstract { //some data and pure virtual functions, which must be defined //in each child class };
Usman Younis
class Child: public an_Abstract { //some data and definitions of pure virtual functions };
an_Abstract* ptr = new Child();
Abstract Classes (contd..)
Usman Younis
Abstract Class Constructor
Usman Younis
Summary
Usman Younis