0% found this document useful (0 votes)
34 views

304 OOPS Unit - 3

oops polymorphism notes

Uploaded by

Kevin Bollywood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

304 OOPS Unit - 3

oops polymorphism notes

Uploaded by

Kevin Bollywood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Polymorphism

Polymorphism

Polymorphism means many forms. It is an object-oriented programming concept that refers to the
ability of a variable, function, or object to take on multiple forms, meaning the behavior of the same
object or function can vary in different contexts.

Polymorphism can occur within a class and when multiple classes are related by inheritance.

What is Polymorphism in C++?

Polymorphism is a word that combines "poly," meaning many, with "morphs," meaning forms,
which together means many forms. In C++, polymorphism refers to the concept where the behavior
of the same object or function is different in different contexts.

Let's take a real-world example to illustrate this concept:

 "I was right." In this sentence, the word "right" means "correct."
 "Please take a right turn." In this sentence, the word "right" refers to the "right
direction."

Types of Polymorphism in C++


1. Compile-Time Polymorphism (Static Polymorphism)
2. Run-Time Polymorphism (Dynamic Polymorphism)

1. CompileTimePolymorphism
2. RuntimePolymorphism

1. Compile-Time Polymorphism (Static Polymorphism)


Compile-time polymorphism is done by overloading an operator or function. It is also known as "static" or
"early binding".

Why is it Called Compile-Time Polymorphism?

Akansha Srivastav Page 1


Polymorphism

Compile-time polymorphism, also known as static polymorphism, occurs when the decision about which
function or method to call is made at compile time. This is possible because the C++ compiler has access to
information about function signatures and types before the program runs. The compiler uses this
information to determine which function to call based on the data types and number of parameters
provided. Since the choice is made during the compilation phase, it is known as compile-time
polymorphism.
Types of Compile-Time Polymorphism in C++

There are two primary types of compile-time polymorphism in C++:

1. Function Overloading

Definition: Function overloading allows multiple functions with the same name but
different parameter lists (different number or types of parameters) to be defined. The
appropriate function is selected based on the arguments provided in the function call.

// Program for compile time polymorphism

#include<conio.h>
#include<iostream.h>
//1.function with one argument
void area(int x)
{
float ar;
ar=3.14*x*x;
cout<<"\nArea of a circle is"<<ar;
}
//2.function with two argument
void area(int x,int y)
{
float ar;
ar=x*y;
cout<<"\nArea of a rectangle is"<<ar;
}
//3.function with two argument different datatype
void area(float x,int y)
{
int ar;
ar=x*y;
cout<<"\nArea of a rectangle is"<<ar;
}
void main()
{
int m;
clrscr();
Akansha Srivastav Page 2
Polymorphism

area(5);//first area called


area(4,6); //second area called
area(2.5,3); //third area called
cout<<m;
getch();
}
The area functions are overloaded and demonstrate different usages based on the types and
number of arguments.

2. Operator Overloading

Definition: Operator overloading allows you to define custom behavior for operators (e.g., +, -, *, /)
when they are used with user-defined types (classes). The appropriate operator function is selected
based on the types of operands involved.

When an operator is updated to be used for user-defined data types(objects etc.), this is known as
operator overloading.To use operator overloading, at least one operand must be a user-defined
data type.

Note:

 A user-defined type must be present in at least one operand.


 ".", "::", typeid, size, "*.", and C++'s single ternary operator, "?:, are among the
operators that cannot be overloaded.

These are some of the operators that can be overloaded in C++:

Arithmetic Operators: -, +, /, *, % and -=, +=, /=, *=, %=

Boolean Algebra: !=, ==, >, <, >=, <=, &&, ||

Bit Manipulation: &, |, ^, <<, >> and |=, &=, >>=, <<=, ^=

Memory Management: new[], new, delete[], delete

Note:

 New and Delete operators can be overloaded globally, or they can be overloaded for specific
classes. If these operators are overloaded using the member function for a class, they are
overloaded only for that specific class.

Example: (Unary)
#include<iostream.h>
#include<conio.h>

Akansha Srivastav Page 3


Polymorphism

class Simple
{
int x,y,z;
public:
void getdata()
{
x=5;
y=6;
z=12;
}
void dispdata();
void operator++()
{
x++;
y++;
z++;
}
};
void Simple::dispdata()
{
cout<<"\n Values of Simple";
cout<<"\nx="<<x;
cout<<"\ny="<<y;
cout<<"\nz="<<z;
}
void main()
{
Simple s1;
clrscr();
s1.getdata();
s1.dispdata();
++s1;
s1.dispdata();
getch();
}

Example : (Binary)
#include<iostream.h>
#include<conio.h>
Class test
{
int x;
Public:
test()
{}
test(int a)
Akansha Srivastav Page 4
Polymorphism

{
x=a;
}
test operator +(test t2)
{
test t3;
t3.x=x+t2.x;
return(t3);
}
void display()
{
cout<<x<<endl;
}
};
void main()
{
clrscr();
test t1,t2,t3;
t1=test(2);
t2=test(3);
t3=t1+t2;
t1.display();
t2.display(); Cout<<”sum:”<<endl;
t3.display();
getch();
}

Runtime Polymorphism
Runtime polymorphism occurs when functions are resolved at runtime rather than compile time
when a call to an overridden method is resolved dynamically at runtime rather than compile time.
It's also known as late binding or dynamic binding.

Runtime polymorphism is achieved using a combination of function overriding and virtual


functions.

Akansha Srivastav Page 5


Polymorphism

The following sections will show an example of overriding with and without the virtual keyword.

Example of Runtime Polymorphism

To illustrate runtime polymorphism, consider the following examples:

1. Without Using virtual Keyword

In this example, the function show in the base class is not marked as virtual. As a result,
the function call is determined at compile time, and runtime polymorphism does not occur.

#include <iostream.h>

class Base {
public:
void show() {
cout << "Base class show function" << endl;
}
};

class Derived : public Base {


public:
void show() {
cout << "Derived class show function" <<endl;
}
};

void main()
{
Base* bptr;
Derived d;
bptr = &d;
bptr->show(); // Calls Base class show function due to non-virtual function
}
Output:
Base class show function

2. Using virtual Keyword

In this example, the function show in the base class is marked as virtual. This allows runtime
polymorphism, meaning the function call is determined at runtime based on the type of object that the
base class pointer actually points to.
#include <iostream.h>

class Base {
public:
virtual void show() {
cout << "Base class show function" << endl;
}
};

Akansha Srivastav Page 6


Polymorphism

class Derived : public Base {


public:
void show() override {
cout << "Derived class show function" << endl;
}
};
void main() {
Base* bptr;
Derived d;
bptr = &d;

bptr->show(); // Calls Derived class show function due to virtual function


}

Explanation

 Without virtual: The show function in the Base class is not marked as virtual. As a
result, the function call is bound at compile time. Even though bptr is pointing to a Derived
object, the Base class version of show is called.
 With virtual: The show function in the Base class is marked as virtual. This enables
runtime polymorphism. When bptr->show() is called, the program determines at runtime
that bptr actually points to a Derived object, so the Derived class version of show is
executed.

This demonstrates how the use of the virtual keyword allows for dynamic method dispatch,
enabling more flexible and extensible code in C++.

Rules for Virtual Functions

 The virtual function must be a member of the base class.


 They cannot be static members.
 They are accessed by using object pointers.
 The prototype of the base class function and derived class function must be the same.
 The virtual function in the base class must be defined even though it is not used.
 A virtual function can be a friend function of another class.
 We cannot have virtual constructors.
 If a virtual function is declared in the base class, it need not be necessarily redefined in the
derived class.
 A pointer to a base class can point to any object of a derived class, but the reverse is not
true.
 When a base pointer points to a derived class, incrementing and decrementing it will not
make it point to the next object of the derived class.

Akansha Srivastav Page 7


Polymorphism

Pure Virtual Functions

A pure virtual function is a virtual function in C++ for which we need not to write any function
definition and only we have to declare it. It is declared by assigning 0 in the declaration.

An abstract class is a class in C++ which have at least one pure virtual function.

 Abstract class can have normal functions and variables along with a pure virtual function.
 Abstract class cannot be instantiated, but pointers and references of Abstract class type can be created.
 Abstract classes are mainly used for Upcasting, so that its derived classes can use its interface.
 If an Abstract Class has derived class, they must implement all pure virtual functions, or else they will become
Abstract too.
 We can’t create object of abstract class as we reserve a slot for a pure virtual function in Vtable, but we don’t put any
address, so Vtable will remain incomplete.

#include<iostream.h>
class B
{
public:
virtual void s() = 0; // Pure Virtual Function
};

class D:public B
{
public:
void s() {
cout << "Virtual Function in Derived class\n";
}
};

void main()
{
B *b;
D dobj;
b = &dobj;
b->s();
}

Akansha Srivastav Page 8


Polymorphism

Compile-Time Polymorphism Vs. Run-Time Polymorphism

Conclusion

 Polymorphism in C++ is when the behavior of the same object or function is different in different
contexts. It has two types: Compile-time Polymorphism and Runtime Polymorphism.
 In Compile-time Polymorphism, the function to be invoked is decided at compile time only. It is
achieved using function or operator overloading.
 In Runtime Polymorphism, the function invoked is decided at run time. It is achieved using function
overriding and virtual functions.
 The virtual function must be declared using the keyword virtual in the base class.
 A Pure Virtual Function is a virtual function declared in the base class without a definition.

Akansha Srivastav Page 9


Polymorphism

Friend Function:
Que: In which circumstances can a function be made a friend? Write the advantage of a friend function.
Demonstrate one example of a friend function.

 A friend function is a special type of function that is declared inside the class. A friend function can
access the private, protected, and public data of the class.
 The keyword friend is used before the return type of the function declaration/prototype.
 If a function is defined as a friend function in C++, then the protected and private data of a class can
be accessed using the function.
 By using the keyword friend, the compiler knows that the given function is a friend function.
 For accessing the data, the declaration of a friend function should be done inside the body of a
class, starting with the keyword '

Declaration of Friend Function in C++

class class_name {
// Friend function declaration
friend data_type function_name(argument/s); // Syntax of friend function
};

In the above declaration, the friend function is preceded by the keyword friend. The
function can be defined anywhere in the program, just like a normal C++ function.

The function definition does not use either the keyword friend or the scope resolution
operator (::).

Characteristics of a Friend Function:

 The function is not in the scope of the class to which it has been declared as a friend.
 It cannot be called using the object, as it is not in the scope of that class.
 It can be invoked like a normal function without using the object.
 It cannot access the member names directly and has to use an object name and dot membership
operator with the member name.
 It can be declared either in the private or the public part of the class.

C++friend function Example

#include <iostream.h>
#include <conio.h>

class Sample {
private:
int a, b;

Akansha Srivastav Page 10


Polymorphism

public:
void getData() {
a = 25;
b = 40;
}

// Friend function declaration


friend float mean(Sample s);
};

// Friend function definition


float mean(Sample s) {
return float(s.a + s.b) / 2.0;
}

int main() {
Sample x;
x.getData();
cout << "Mean value = " << mean(x) << endl;

getch();

return 0;
}

USING FRIEND FUNCTION TO FIND MINIMUM FROM TWO DIFFERENT CLASSES

// Program for friend function


#include<conio.h>
#include<iostream.h>
class B;
class A
{
int x;
public:
A()
{
x=100;
}
friend void min(A,B);
};
class B
{
int y;

Akansha Srivastav Page 11


Polymorphism

public:
B()
{
y=20;
}
friend void min(A,B);
};
void min(A a,B b)
{
if(a.x<b.y)
cout<<"\n\n\n"<<a.x<<"is smaller";
else
cout<<"\n\n\n"<<b.y<<"is smaller";
}
void main()
{
clrscr();
A a;
B b;
min(a,b);
getch();

USING FRIEND FUNCTION TO SWAP FROM TWO DIFFERENT CLASSES

#include<iostream.h>
#include<conio.h>
class B;
class A
{
int x;
public:
A()
{
x=20;
}
void putA()
{
cout<<"\n Value of A x:"<<x;
}
friend void swap(A &,B &);
};
class B
{
int y;
public:
B()

Akansha Srivastav Page 12


Polymorphism

{
y=30;
}
void putB()
{
cout<<"\n Value of B y:"<<y;
}
friend void swap(A &,B &);
};
void swap(A &a,B &b)
{
int temp;
cout<<"Before swaping";
cout<<"\n Value of A x:"<<a.x;
cout<<"\n Value of B y:"<<b.y;
temp=a.x;
a.x=b.y;
b.y=temp;
cout<<"\n After After swaping";
cout<<"\n Value of A x:"<<a.x;
cout<<"\n Value of B y:"<<b.y;
}
void main()
{
A a;
B b;
clrscr();
swap(a,b);
a.putA();
b.putB();
getch();
}

Explain Virtual Base class with example


When two or more objects are derived from a common base class, we can prevent multiple copies
of the base class being present in an object derived from those objects by declaring the base class as
virtual when it is being inherited.

Such a base class is known as a virtual base class. This can be achieved by preceding the base
class’ name with the word virtual.

Akansha Srivastav Page 13


Polymorphism

The duplication of inherited members due to these multiple paths can be avoided by making the
common base class (ancestor class) as virtual base class.

Akansha Srivastav Page 14


Polymorphism

Example:
#include <iostream.h>

class Student {
protected:
int roll_number;
public:
void get_number(int a) {
roll_number = a;
}
void put_number() const {
cout << "Roll no: " << roll_number << "\n";
}
};

class Test : virtual public Student {


protected:
float part1, part2;
public:
void get_marks(float x, float y) {
part1 = x;
part2 = y;
}
void put_marks() const {
cout << "Marks obtained: " << "part1 = " << part1 << "\n" << "part2 = " << part2 << "\n";
}
};

class Sports : virtual public Student {


protected:
float score;
public:
void get_score(float s) {
score = s;
}
void put_score() const {
cout << "Sports: " << score << "\n";
}
};

class Result : public Test, public Sports {


public:

Akansha Srivastav Page 15


Polymorphism

float total;
void display();
};

void Result::display() {
total = part1 + part2 + score;
put_number();
put_marks();
put_score();
cout << "Total Score = " << total << "\n";
}

int main() {
Result stu;
stu.get_number(123);
stu.get_marks(27.5, 33.0);
stu.get_score(6.0);
stu.display();
return 0;
}

Explain This Pointer


C++ uses unique keyword called this to represent an object that invokes a member function.

this is a pointer that points to the object for which this function was called.

This unique pointer is automatically passed to a member function when it is called. The pointer this acts
as an implicit argument to all the member functions.

class abc
{
int a;
public:
void display()
{
this->a=123;
cout<<a;
}
};
void main()
{
clrscr();

Akansha Srivastav Page 16


Polymorphism

abc a;
a.display();
getch();
}

// Abstract class
#include <iostream.h>
class Shape {
public:
// Pure virtual function
virtual void draw() const = 0;

// Non-pure virtual function


void describe() const {
cout << "I am a shape." << endl;
}
};

// Derived class
class Circle : public Shape {
public:
void draw() const override {
cout << "Drawing a circle." << endl;
}
};

Akansha Srivastav Page 17


Polymorphism

int main() {
// Shape shape; // This line would cause a compilation error because Shape is abstract

Circle circle;
circle.draw(); // Calls the overridden draw function in Circle
circle.describe(); // Calls the non-pure virtual function from Shape
return 0;
}

Akansha Srivastav Page 18

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy