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

OOP 3rd Lect 2024-2025

Uploaded by

ad007654321
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)
13 views

OOP 3rd Lect 2024-2025

Uploaded by

ad007654321
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/ 13

Object-Oriented Programming

(OOP)

Dr. Waleed Dahea


2024-2025
[email protected]
Operator Overloading
■ A Operator overloading is one of the most
exciting features of Object-Oriented
Programming. It can transform complex,
obscure program listings into intuitively
obvious ones.
■ Can specify meaning of operator whose
operands are one or more user-defined types
through process known as operator
overloading.
Overloading Unary Operators
■ unary operators act on only one operand. (An
operand is simply a variable acted on by an
operator.) Examples of unary operators are the
increment and decrement operators ++ and --,
and the unary minus…etc.
■ The operator Keyword.
• void operator ++ ()
Overloading Unary Operators
// increment counter variable with ++ operator
#include <iostream>
int main()
using namespace std; {
class Counter Counter c1, c2; //define and initialize
{
private: cout << “\nc1=” << c1.get_count(); //display
cout << “\nc2=” << c2.get_count();
int count; //count
public: ++c1; //increment c1
Counter():count(0) //constructor ++c2; //increment c2
{ } ++c2; //increment c2
int get_count() //return count
cout << “\nc1=” << c1.get_count(); //display
{ return count; }
again
void operator ++() //increment(prefix) cout << “\nc2=” << c2.get_count() << endl;
{ return 0;
++count; }
}
};
Overloading Unary Operators
• Operator Return Values
// increment counter variable with ++ operator
int main()
{
#include <iostream>
Counter c1, c2; //define and initialize
using namespace std;
class Counter
cout << “\nc1=” << c1.get_count(); //display
{
cout << “\nc2=” << c2.get_count();
private:
int count; //count
++c1; //c1=1
public:
c2 = ++c1;
Counter():count(0) //constructor
cout << “\nc1=” << c1.get_count(); //display
{ } again
int get_count() //return count cout << “\nc2=” << c2.get_count() << endl;
{ return count; } return 0;
Counter operator ++ () //increment count }
{
++count; //increment count
Counter temp; //make a temporary Counter
temp.count = count; //give it same value as this obj
return temp; //return the copy
} };
Overloading Unary Operators
• Operator Return Values
// increment counter variable with ++ operator
int main()
{
#include <iostream>
Counter c1, c2; //define and initialize
using namespace std;
class Counter
cout << “\nc1=” << c1.get_count(); //display
{
cout << “\nc2=” << c2.get_count();
private:
int count; //count
++c1; //c1=1
public:
c2 = ++c1;
Counter():count(0) //constructor
cout << “\nc1=” << c1.get_count(); //display
{ } again
Counter(int c) : count(c) //constructor, one arg cout << “\nc2=” << c2.get_count() << endl;
{} return 0;
int get_count() //return count }
{ return count; }
Counter operator ++ () //increment count
{
++count; // increment count, then return
return Counter(count); // an unnamed temporary object } //
initialized to this count
} };
Overloading Unary Operators
• Operator Return Values(Postfix Notation)
// increment counter variable with ++ operator
int main()
{
#include <iostream>
Counter c1, c2; //define and initialize
using namespace std;
class Counter
cout << “\nc1=” << c1.get_count(); //display
{
cout << “\nc2=” << c2.get_count();
private:
int count; //count
C1++; //c1=1
public:
c2 = c1++;
Counter():count(0) //constructor
cout << “\nc1=” << c1.get_count(); //display
{ } again
Counter(int c) : count(c) //constructor, one arg cout << “\nc2=” << c2.get_count() << endl;
{} return 0;
int get_count() //return count }
{ return count; }
Counter operator ++ (int) //increment count
{
count ++; // increment count, then return
return Counter(count); // an unnamed temporary object } //
initialized to this count
} };
Overloading Binary
Operators
■ Binary operators can be overloaded just as
easily as unary operators. We’ll look at
examples that overload arithmetic operators,
comparison operators, and arithmetic
assignment operators.
■ Ex:
• c3=c1+c2; // as objects
Overloading Binary
Operators
• Arithmetic Operators
// overloaded ‘+’ operator adds two Distances Distance Distance::operator + (Distance d2) const //return
#include <iostream>
sum
using namespace std;
{
class Distance //English Distance class
int f = feet + d2.feet; //add the feet
{
private:
float i = inches + d2.inches; //add the inches
int feet;
if(i >= 12.0) //if total exceeds 12.0,
float inches; { //then decrease inches
public: //constructor (no args) i -= 12.0; //by 12.0 and
Distance() : feet(0), inches(0.0) f++; //increase feet by 1
{ } //constructor (two args) } //return a temporary Distance
Distance(int ft, float in) : feet(ft), inches(in) return Distance(f,i); //initialized to sum
{ } }
void getdist() //get length from user
{
cout << “\nEnter feet: “; cin >> feet;
cout << “Enter inches: “; cin >> inches;
}
void showdist() const //display distance
{ cout << feet << “\’-” << inches << ‘\”’; }

Distance operator + ( Distance ) const; //add 2 distances };


Overloading Binary
Operators
int main()
• Arithmetic Operators
{
Distance dist1, dist3, dist4; //define distances
dist1.getdist(); //get dist1 from user

Distance dist2(11, 6.25); //define, initialize dist2

dist3 = dist1 + dist2; //single ‘+’ operator

dist4 = dist1 + dist2 + dist3; //multiple ‘+’ operators


//display all lengths
cout << “dist1 = “; dist1.showdist(); cout << endl;
cout << “dist2 = “; dist2.showdist(); cout << endl;
cout << “dist3 = “; dist3.showdist(); cout << endl;
cout << “dist4 = “; dist4.showdist(); cout << endl;
return 0; }
Overloading Binary
Operators
• Arithmetic Operators
// overloaded ‘+’ operator adds two Distances bool Distance::operator < (Distance d2) const //return sum
#include <iostream>
{
using namespace std;
float bf1 = feet + inches/12;
class Distance //English Distance class
float bf2 = d2.feet + d2.inches/12;
{
private:
return (bf1 < bf2) ? true : false;
int feet;
}
float inches;
public: //constructor (no args)
Distance() : feet(0), inches(0.0)
{ } //constructor (two args)
Distance(int ft, float in) : feet(ft), inches(in)
{ }
void getdist() //get length from user
{
cout << “\nEnter feet: “; cin >> feet;
cout << “Enter inches: “; cin >> inches;
}
void showdist() const //display distance
{ cout << feet << “\’-” << inches << ‘\”’; }

bool operator < (Distance) const; //compare distances };


Overloading Binary
Operators
int main()
• Arithmetic Operators
{
Distance dist1; //define Distance dist1
dist1.getdist(); //get dist1 from user

Distance dist2(6, 2.5); //define and initialize dist2


//display distances
cout << “\ndist1 = “; dist1.showdist();
cout << “\ndist2 = “; dist2.showdist();

if( dist1 < dist2 ) //overloaded ‘<’ operator


cout << “\ndist1 is less than dist2”;
else
cout << “\ndist1 is greater than (or equal to) dist2”;
cout << endl;
return 0;
}
Thank You!
Next Lecture is INHERITANCE

Dr. Waleed Dahea


2024-2025
[email protected]

You might also like