Unit-III Oop RBK
Unit-III Oop RBK
POLYMORPHISM
Definition
The word polymorphism means having
many forms.
The ability of a message to be displayed in
characteristics.
Same person posses different behaviour in
different situations.
This is called polymorphism.
Types of Polymorphism
Polymorphism
Compile Time
Polymorphism Run time Polymorphism
Output
30
55
//Program:Function overlodding
#include<iostream>
using namespace std;
int mul(int,int);
float mul(float,int);
int mul(int a,int b)
{
return a*b;
}
float mul(double x, int y)
{
return x*y;
}
int main()
{
int r1 = mul(6,7);
float r2 = mul(0.2,3);
std::cout << "r1 is : " <<r1<< std::endl;
std::cout <<"r2 is : " <<r2<< std::endl;
return 0;
}
30 55
Output
r1 is : 42
r2 is : 0.6
Operator Overloading:
#include <iostream>
using namespace std;
class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};
int main() {
Derived derived1;
derived1.print();
return 0;
Output
Derived Function
// C++ program to access overridden function
// in main() using the scope resolution operator ::
#include <iostream>
using namespace std;
class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};
int main() {
Derived derived1, derived2;
derived1.print();
return 0;
Output:
Derived Function
Base Function
Virtual Function
A virtual function is a member function which is declared
within a base class and is re-defined (overridden) by a
derived class. When you refer to a derived class object
using a pointer or a reference to the base class, you can
call a virtual function for that object and execute the
derived class’s version of the function.
Assignment operator =
function call operator ()
subscriping operator []
class member access operator ->
The process of overloading involves the
following steps :
1) Create a class that defines the data type
that is to be used in the overloading
operation.
2) Declare the operator function operator
op() in the public part of the class. It may
be either a member function or a friend
function.
3) Define the operator function to implement
the required operations.
Operator functions must be either member
functions (non-static) or friend functions.
Examples:
operator+
Return_type operator op(argument-list);
operator-
operator*
29
Syntax
Definition Syntax of operator function :
function.
Overloading unary
operator
Unary operator: are operators that act upon
a single operand to produce a new value.
decrement(- -)
NOT(!)
Addressof operator(&)
sizeof()
Overloading unary
operator
Overloading Unary Operator: Let us
consider to overload (-) unary operator. In
unary operator function, no arguments
should be passed. It works only with one
class objects. It is a overloading of an
operator operating on a single operand.
Overloading Binary
operator
A binary operator is an operator that
operates on two operands and manipulates
them to return a result.
You use binary operators very frequently
Example
Conversion constructor
class classname
{ private:
public:
classname(basic data type)
{
//Conversion code
}
};
// C++ program type- void Display()
conversion {
cout << "Time = " <<
#include <iostream> hour
using namespace std; << " hrs and "
<< mins << " mins\
class Time {
n";
int hour; }
int mins; };
public:
Time() int main()
{ {
Time T1;
hour = 0;
int dur = 95;
mins = 0;
} T1 = dur;
Time(int t) T1.Display();
{
hour = t / 60; return 0;
}
mins = t % 60;
Output:
} Time = 1 hrs and 35 mins
Conversion from user-defined
data type to basic data type
Conversion from user-defined type of basic
data type is done by overloading the cast
operator of basic type as a member
function.
Operator function is defined as an
Operator Basicdatatype()
{
return
}
1. Implicit Casting
2. Explicit Casting
Implicit Casting
Implicit conversions do not require any operator.
They are automatically performed when a value is
copied to a compatible type.
It is done by compiler and there is no loss of
information
For example:
float a=20.10;
int b;
b=a;
class A {};
class B {
public: B (A a) {}
};
A a;
B b=a;
#include <iostream>
using namespace std;
int main() {
int a=9;
double b;
b=a;
return 0;
Output
a= 9
b= 9
Example 2: Automatic Conversion from double to int
//Working of Implicit type-conversion
#include <iostream>
using namespace std;
int main() {
int a;
double b=9.99;
a=b;
cout<<"a= "<<a<< endl;
cout<<“b= "<<b<< endl;
return 0;
}
Output
a=9
#include<iostream>
using namespace std;
class Demo
{
private:
int data;
public:
Demo(int i):data(i)
{
}
void Display()
{
cout<<" data = "<<data<<endl;
}
};
int main()
{
Demo obj(6);
obj.Display();
obj = 27; // implicit conversion occurs here.
obj.Display();
return 0;
Output
data = 6
data = 27
Explicit Casting
For explicit conversion cast operator is used.
A cast is a special operator that forces one data type to be
compilers is as follows −
Where type is the desired data type.
(type) expression
e.g.
float a=20.20;
int b;
b =(int) a;
example
Case1: int a=10, b=3;
float c=a/b; output:3.0
int main() {
double pi= 3.56;
cout << “pi= " << pi<< endl;
return 0;
}
Output:
pi= 3.56
num1= 3
Pitfalls of operator overloading and conversion
way.
If number of overloaded operators are too large
mutable keyword.
Example
#include <iostream>
using namespace std;
class Test {
public:
int x;
mutable int y;
Test() { x = 4; y = 10; }
};
int main()
{
Test t1;
t1.y = 20;
cout << t1.y;
return 0;
}
Output:
20
#include <iostream>
using namespace std;
class Test {
public:
int x;
mutable int y;
Test() { x = 4; y = 10; }
};
int main()
{
const Test t1;
t1.x = 8;
cout << t1.x;
return 0;
}
Output:
int main()
{
demo d(10);
d.output();
return 0;
Pointers to base class
Pointer is a data type that stores the
address of other data types.
The pointer of Base Class pointing different
assigning 0 in declaration.
Syntax:-
virtual void show() = 0;
Example:-
Similarity between Virtual
Function & Pure Virtual Function
These are the concepts of Run-time
polymorphism.
Prototype i.e. Declaration of both the
Base class having virtual function can be Base class having pure virtual function
instantiated i.e. its object can be made. becomes abstract i.e. it cannot be
instantiated.
Important Points related to
pure virtual function
1) A class is abstract if it has at least one
pure virtual function.
2)We can have pointers and references of
abstract class type.
3) If we do not override the pure virtual
function in derived class, then derived class
also becomes abstract class.
4) An abstract class can have constructors.
Virtual Destructor
Deleting a derived class object using a
pointer of base class type that has a non-
virtual destructor results in undefined
behaviour.
To correct this situation, the base class
Class A
{
Public:
Virtual void show()=0;
};
int main()
#include <iostream> {
using namespace std; A* b;
class A B d;
{ d.disp();
public: d.show();
virtual void show()=0; b=&d;
void disp() b->show();
{ b->disp();
cout<<"class base"<<endl; return 0;
} }
};
class B:public A
{
public:
void show()
{
cout<<"class derived"<<endl;
}
};
Output:
class base
class derived
class derived
class base
Thank You