Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
29 views

Object Oriented Programming 3

Polymorphism allows objects to take on multiple forms. In object-oriented programming, polymorphism means that derived classes can override methods from their base classes. There are two types of polymorphism: static polymorphism (or early binding), which occurs at compile time via function overloading, and dynamic polymorphism (or late binding), which occurs at runtime via virtual functions. With virtual functions, the decision of which function to call is determined by the actual object type, not the reference type, allowing base class pointers or references to transparently call derived class functions.

Uploaded by

abdullah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Object Oriented Programming 3

Polymorphism allows objects to take on multiple forms. In object-oriented programming, polymorphism means that derived classes can override methods from their base classes. There are two types of polymorphism: static polymorphism (or early binding), which occurs at compile time via function overloading, and dynamic polymorphism (or late binding), which occurs at runtime via virtual functions. With virtual functions, the decision of which function to call is determined by the actual object type, not the reference type, allowing base class pointers or references to transparently call derived class functions.

Uploaded by

abdullah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

CS-2012

Object Oriented Programming


Lecture no.13
FALL-2023
2

Polymorphism
• Combination of two Greek words
• Poly (many) morphism (form)

• Water -> Solid, Liquid, Gas


In shopping mall, behaves like a customer
In metro bus, behaves like a passenger
• For example, A Person In university, behaves like a student
In home, behaves like a daughter/son

• Same person have different behavior in different


situations. This is called Polymorphism.
Polymorphism

Static Polymorphism/
Dynamic Polymorphism/
Static Binding
Dynamic Binding

3
Binding Process
• Binding is the process to associate variable/
function names with memory addresses

• Binding is done for each variable and functions.

• For functions, it means that matching the call with


the right function definition by the compiler.
4
Compile-time Binding (Static Binding)
• Compile-time binding is to associate a function's
name with the entry point (start memory address)
of the function at compile time (also called early
binding)

Start address if
sayHi() function

5
Run-time Binding (Dynamic Binding)
• Run-time binding is to associate a function's name with
the entry point (start memory address) of the function
at run time (also called late binding)

• C++ provides both compile-time and run-time bindings:


– Non-Virtual functions (you have implemented so far) are
binded at compile time
– Virtual functions (in C++) are binded at run-time.

• Why virtual functions are used?


– To implement Polymorphism
6
Static Polymorphism int main() {

SomeClass obj1;
// The first 'func' is called
obj1.func(7);

// The second 'func' is called


//Function Overloading obj1.func(9.132);
class SomeClass
{ // The third 'func' is called
public: obj1.func(85,64);
// function with 1 int parameter return 0;
void func(int x) }
{
cout << "value of x is " << x << endl;
}
// function with same name but 1 double parameter
void func(double x)
{
cout << "value of x is " << x << endl;
}
// function with same name and 2 int parameters
void func(int x, int y)
{
cout << "value of x and y is " << x << ", " << y << endl;
} 7
};
Dynamic Polymorphism
• There is an inheritance hierarchy

• There is a pointer/reference of base class type that


can point/refer to derived class objects

8
Pointers to Derived Classes
• C++ allows base class pointers or references to
point/refer to both base class objects and also all
derived class objects.

• Let’s assume:
class Base { … };
class Derived : public Base { … };

• Then, we can write:


Base *p1;
Derived d_obj; p1 = &d_obj;
Base *p2 = new Derived;
9
Pointers to Derived Classes (contd.)
• While it is allowed for a base class pointer to point
to a derived object, the reverse is not true.

base b1;
derived *pd = &b1; // compiler error

10
Pointers to Derived Classes (contd.)
• Access to members of a class object is determined
by the type of
– An object name (i.e., variable, etc.)
– A reference to an object
– A pointer to an object

11
Pointers to Derived Classes (contd.)
• Using a base class pointer (pointing to a derived
class object) can access only those members of the
derived object that were inherited from the base.

• This is because the base pointer has knowledge only


of the base class.

• It knows nothing about the members added by the


derived class.
12
Pointer of Base Class
class A {
public:
void func() {
cout << "A's func" << endl;
}
};
class B:public A {
public:
void func() {
cout << "B's func" << endl;
}
};
void main() {
B b;
//pointer of class type A points to
//object of child class B
A* a = &b;
a->func(); //calls A's func
}
13
Reference of Base Class
class A {
public:
void func() {
cout << "A's func" << endl;
}
};
class B:public A {
public:
void func() {
cout << "B's func" << endl;
}
};
void main() {
B b;
//reference of class type A refers to
//object of child class B
A& a = b;
a.func(); //calls A's func
}
14
Pointer of Base Class
Parent class
class A { pointer/reference has
public:
void func() {
NO KNOWLEDGE of child
cout << "A's func" << endl; class functions
}
};
class B:public A {
public:
void func() {
cout << "B's func" << endl;
}
void foo() {}
};

void main() {
//pointer of class type A points to
//object of child class B
A* a = new B;
a->foo(); //ERROR
}

15
Summary – Based and Derived Class
Pointers
• Base-class pointer pointing to base-class object
– Straightforward

• Derived-class pointer pointing to derived-class object


– Straightforward

• Base-class pointer pointing to derived-class object


– Safe
– Can access non-virtual methods of only base-class
– Can access virtual methods of derived class

• Derived-class pointer pointing to base-class object


– Compilation error
16
Dynamic Polymorphism
• There is an inheritance hierarchy

• There is a pointer/reference of base class type that can point/refer


to derived class objects

• There is a pointer of base class type that is used to invoke virtual


functions of derived class.

• The first class that defines a virtual function is the base class of the
hierarchy that uses dynamic binding for that function name and
signature.

• Each of the derived classes in the hierarchy must have a virtual


function with same name and signature. Not an error but needed
for dynamic binding
17
Virtual Functions
• Virtual functions ensure that the correct function is
called for an object, regardless of the type of
reference (or pointer) used for function call

• They are mainly used to achieve Runtime


polymorphism

• Functions are declared with a virtual keyword in


base class

• The resolving of function call is done at runtime 18


Virtual Functions
• The virtual-ness of an operation is always inherited

• If a function is virtual in the base class, it must be


virtual in the derived class

• Even if the keyword “virtual” not specified (But


always use the keyword in children classes for
clarity.)

– If no overridden function is provided, the virtual function


of base class is used
19
Virtual function
• Declaring a function virtual will ensure late-binding

• To declare a function virtual, we use the Keyword


virtual:
class Shape
{
public:
virtual void sayHi ()
{
cout <<“Just hi! \n”;
}
};
20
Virtual function
class A {
public:
virtual void func() {
cout << "A's func" << endl;
}
};
class B:public A {
public:
void func() { //automatically virtual
cout << "B's func" << endl;
}
};
void main() {
B b;
//pointer of class type A points to
//object of child class B
A* a = &b;
a->func(); //calls B's func
}
21
Virtual function
Overridden function
parameters in derived
class A { class must be same as
public:
virtual void func() {
base class, otherwise
cout << "A's func" << endl; base class func will be
} called
};
class B:public A {
public:
void func(int a) { //automatically virtual
//BUT, parameters don't match base class func()
cout << "B's func" << endl;
}
};
void main() {
B b;
//pointer of class type A points to
//object of child class B
A* a = &b;
a->func(); //calls A's func
}

22
Virtual function
class A {
public:
virtual void func() {
cout << "A's func" << endl;
}
};
class B:public A {
public:
void func() { //automatically virtual
//use override keyword to ensure parameters match
cout << "B's func" << endl;
}
};
void main() {
B b;
//pointer of class type A points to
//object of child class B
A* a = &b;
a->func(); //calls B's func
}

23
Virtual function with Multilevel
class A {
public:
Inheritance
virtual void func() {
cout << "A's func" << endl;
}
};
class B:public A {
public:
void func() { //automatically virtual
//use override keyword to ensure parameters match
cout << "B's func" << endl;
}
};
class C :public B {
public:
void func() { //automatically virtual
//use override keyword to ensure parameters match
cout << "C's func" << endl;
}
};
void main() {
//pointer of class type A points to
//object of grandchild class C
A* a = new C;
a->func(); //calls C's func
} 24

You might also like