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

Unit-3 OOPS (1)

Doneeeeeeer

Uploaded by

roshankuma0467
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)
6 views

Unit-3 OOPS (1)

Doneeeeeeer

Uploaded by

roshankuma0467
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/ 15

Manav Rachna International Institute of

Research and Studies


(Deemed to be University)
School of Engineering & Technology
Department of Computer Science & Engineering
OBJECT ORIENTED PROGRAMMING
UNIT-III

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.

This feature is integral to Object-Oriented Programming and


enhances code flexibility and reusability.

Types of polymorphism In C++

In C++, two primary types of Polymorphism are:

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

In C++, operator overloading empowers


operators to have special meanings for
specific data types. For instance, we can
redefine the addition operator (+) for the
string class to concatenate two strings. This
flexibility allows us to extend the functionality
of operators beyond their conventional
usage.

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

Overloading an operator cannot change its precedence.

Overloading an operator cannot change its associativity.

Overloading an operator cannot change its "arity" (i.e.


number of operands)

It is not possible to create new operators -- only new


versions of existing ones.

Operator meaning on the built-in types cannot be changed.

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

In C++, stream insertion operator “<<” is used for


output and extraction operator “>>” is used for
input.
We must know the following things before we
start overloading these operators.
1) cout is an object of ostream class and cin is an
object of istream class
2) These operators must be overloaded as a
global function. And if we want to allow them to
access private data members of the class, we
must make them friend.
OOPS_UNIT-1_CSE_MRIIRS
Compile Time Polymorphism
#include <iostream>
using namespace std;
class Complex
{
private:
int real, imag;
public:
Complex(int r = 0, int i =0)
{ real = r; imag = i; }
friend ostream & operator << (ostream &out, const Complex &c);
friend istream & operator >> (istream &in, Complex &c);
};
ostream & operator << (ostream &out, const Complex &c) {
out << c.real;
out << "+i" << c.imag << endl;
return out;}
istream & operator >> (istream &in, Complex &c){
cout << "Enter Real Part ";
in >> c.real;
cout << "Enter Imaginary Part ";
in >> c.imag;
return in; }
int main() {
Complex c1;
cin >> c1;
cout << "The complex object is ";
cout << c1;
return 0; OOPS_UNIT-1_CSE_MRIIRS
}
Operator Functions as Class Members
versus Friend Functions

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

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