Unit-3 OOPS (1)
Unit-3 OOPS (1)
OOPS_UNIT-1_CSE_MRIIRS
Polymorphism
Polymorphism in C++ refers to the ability of objects to take
on multiple forms based on their context or usage. It allows
different objects to respond to the same message differently.
Compile-Time Polymorphism
Runtime Polymorphism
OOPS_UNIT-1_CSE_MRIIRS
Compile Time Polymorphism
Compile-time Polymorphism refers to the mechanism in C++
where the compiler determines which function to call during
compilation based on the function’s signature and the
context in which it is invoked. This is typically achieved
through function overloading and operator overloading.
A. Function Overloading
Function overloading occurs when multiple functions have
the same name but different parameters.
This enables the same function name to perform various
tasks based on the parameters provided, which can vary in
number or type.
It’s a feature of object-oriented programming that allows for
creating multiple functions with the same name but different
parameter lists.
OOPS_UNIT-1_CSE_MRIIRS
Compile Time Polymorphism
//Example:
#include <iostream>
using namespace std;
class Overload {
public:
void print(int num) {
cout << "Printing integer: " << num << endl; }
void print(double num) {
cout << "Printing double: " << num << endl; }
void print(string text) {
cout << "Printing string: " << text << endl; }
};
int main() {
Overload obj;
obj.print(5);
obj.print(10.5);
obj.print("Hello World");
return 0;}
OOPS_UNIT-1_CSE_MRIIRS
Compile Time Polymorphism
B. Operator Overloading
OOPS_UNIT-1_CSE_MRIIRS
Compile Time Polymorphism
//Example:
#include <iostream>
#include <string>
using namespace std;
class StringConcat {
private:
string str;
public:
StringConcat() : str("") {}
StringConcat(string s) : str(s) {}
StringConcat operator+(const StringConcat& obj) {
StringConcat result;
result.str = this->str + obj.str;
return result; }
void display() {
cout << "Concatenated String: " << str << endl; }
};
int main() {
StringConcat str1("Hello");
StringConcat str2("World");
StringConcat result = str1 + str2;
result.display();
return 0; } OOPS_UNIT-1_CSE_MRIIRS
Compile Time Polymorphism
There are many operators available that work on built-in types,
like int and double.
Operator overloading -- is the creation of new versions of these
operators for use with user-defined types.
Some things to note:
An operator in C++ is just a function that is called with special
notation. Overloading an operator simply involves writing a
function.
C++ already does some operator overloading implicitly on built-in
types. Consider the fact that the + operator already works for
ints, floats, doubles, and chars. There is really a different
version of the + operator for each type.
Operator overloading is done for the purpose of using familiar
operator notation on programmer-defined types (classes).
OOPS_UNIT-1_CSE_MRIIRS
Compile Time Polymorphism
Some rules regarding operator overloading
OOPS_UNIT-1_CSE_MRIIRS
Compile Time Polymorphism
friend functions vs. member functions
Some operators can be written as member functions of a class
Some operators can be written as stand-alone functions -- it's common
to use friend on these
Some operators can be written either way
A binary operator has two operands
Written as a stand-alone function, both operands would be sent as
parameters, so it would be a function with two parameters
Written as a member function, the first operand would be the calling
object, and the other would be sent as a parameter (i.e. a function with
one parameter)
A unary operator has one operand
As a stand-alone function, the operand is sent as a parameter
As a member function, one calling object, no parameters
OOPS_UNIT-1_CSE_MRIIRS
Compile Time Polymorphism
Format
An operator is just a function. This means that it must be created
with a return type, a name, and a parameter list
The rules above give some restrictions on the parameter list
The name of an operator is always a conjunction of the keyword
operator and the operator symbol itself. Examples:
operator+
operator++
operator<<
operator==
So the format of an operator overload declaration is just like that
of a function, with the keyword operator as part of the name:
returnType operatorOperatorSymbol (parameterList);
OOPS_UNIT-1_CSE_MRIIRS
Compile Time Polymorphism
//binary operator+
#include <iostream>
using namespace std;
class complx {
double real, imag;
public:
complx( double real = 0., double imag = 0.); // default constructor
complx operator+(const complx&) const; // operator+()
};
complx::complx( double r, double i ) // define constructor
{
real = r; imag = i;
}
complx complx::operator+ (const complx& c) const // define overloaded + (plus) operator
{
complx result;
result.real = (this->real + c.real);
cout<<result.real<<”+”<<endl;
result.imag = (this->imag + c.imag);
cout<<result.img<<”+”<<endl;
return result;
}
int main()
{
complx x(4,4);
complx y(6,6);
complx z = x + y; // calls complx::operator+()
OOPS_UNIT-1_CSE_MRIIRS
}
Compile Time Polymorphism
//Unary operator++ and --
#include<iostream>
using namespace std;
class Rectangle {
public:
int length;
int width;
Rectangle(int len, int wid) {
length = len;
width = wid; }
void area() {
cout << length * width << endl }
Rectangle operator++(int) { // Overloading ++ operator.
length++;
width++; return *this;}
Rectangle operator--(int) { // Overloading -- operator.
length--;
Width--; return *this; }
};
int main() {
Rectangle r(3, 2);
r.area();
r++;
r.area();
r--;
r1.area();
return 0;
OOPS_UNIT-1_CSE_MRIIRS
}
Compile Time Polymorphism
When the operator takes as arguments only members of the class being are
designed, encapsulation votes for using a member function.
Example: addition of two objects:
class A {
...
A operator + (const class A& other); // naturally a member function
...
};
In the opposite, when the member of the class you are writing is the second
argument of the operator, you can only use a friend function:
std::outstream& operator << (std::outstream& out, const class
A& a);
class A {
...
friend std::outstream& operator << (std::outstream& out,
const class A& a); // must be friend here
};
OOPS_UNIT-1_CSE_MRIIRS