Object Oriented Programming (CS2133)
Object Oriented Programming (CS2133)
(CS2133)
By
Dr. Muhammad Azhar Iqbal
Example:
+ - * / % ^ & | ~ ! =
< > += -= *= /= %= ^= &= |= <<
>> >>= <<= == != <= >= && || ++ --
->* , -> [] () new delete
Operators That Cannot Be Overloaded
?: . .* ::
Overloading (Prefix) Unary Operators
class Counter
{ void main() {
private: Counter c1, c2;
int count;
public: cout<<c1.getCount();
Counter() : count(0) cout<<c2.getCount();
{ } ++c1;
int getCount( ) ++c2;
{ return count; } ++c2;
void operator++( )
{ ++count; } cout<<c1.getCount();
}; cout<<c2.getCount();
}
Operators Return Value
class Counter
{
private: void main() {
int count; Counter c1, c2;
public:
Counter() : count(0) cout<<c1.getCount();
{ } cout<<c2.getCount();
int getCount( ) ++c1;
{ return count; } c2 = ++c1;
Counter operator++( )
{
++count; cout<<c1.getCount();
Counter temp; cout<<c2.getCount();
temp.count = count; }
return temp;
}
};
Operators Return Value – Nameless Temporary Objects
More convenient way to return nameless (anonymous) temporary objects
from functions and overloaded operators.
class Counter
{ void main() {
private: Counter c1, c2;
int count;
public: cout<<c1.getCount();
Counter() : count(0) cout<<c2.getCount();
{ }
Counter(int c):count(c) ++c1;
{ } c2 = ++c1;
int getCount( )
{ return count; } cout<<c1.getCount();
Counter operator++( ) cout<<c2.getCount();
{ }
++count;
return Counter(count);
}
};
Overloading (Postfix) Unary Operators
class Counter
{ void main() {
private: Counter c1, c2;
int count;
public: cout<<c1.getCount();
Counter() : count(0) cout<<c2.getCount();
{ }
Counter(int c):count(c) ++c1;
{ } c2 = ++c1;
int getCount( )
{ return count; } cout<<c1.getCount();
Counter operator++( ) cout<<c2.getCount();
{
++count; c2 = c1++;
return Counter(count);
} cout<<c1.getCount();
Counter operator++(int) cout<<c2.getCount();
{
return Counter(count++); }
}
};
Overloading Binary Operators (Arithmetic Operators)
class Counter
{ void main() {
private: Counter c1(10, 20);
int count1, count2; Counter c2(5,10);
public: Counter c3;
Counter() : count1(0), count2(0)
{ } c1.showCount();
Counter(int n1, int n2) : count1(n1), count2(n2) c2.showCount();
{ } c3.showCount();
void showCount( )
{ cout<<count1<<“ ”<<count2; } c3 = c1 + c2;
Counter operator + (Counter c)
{ c1.showCount();
int temp1 = count1 + c.count1; c2.showCount();
int temp2 = count2 + c.count2; c3.showCount();
return Counter(temp1, temp2);
} }
};
Overloading Binary Operators
(Arithmetic Assignment Operators)
class Counter
{ void main() {
private: Counter c1(10, 20);
int count1, count2; Counter c2(5,10);
public:
Counter() : count1(0), count2(0) c1.showCount();
{ } c2.showCount();
Counter(int n1, int n2) : count1(n1), count2(n2)
{ } c1 += c2;
void showCount( ) // c1 = c1 + c2;
{ cout<<count1<<“ ”<<count2; }
void operator += (Counter c) c1.showCount();
{ c2.showCount();
count1 += c.count1;
count2 += c.count2; }
}
};
Precedence and Associativity
C++ allows you to overload the stream insertion operator (<<) for
sending an object to cout and overload the stream extraction
operator (>>) for reading values from cin. Overloading these two
operators is different from other operators. Since the first
parameter of the << (>>) operator is an instance of ostream
(istream), these two operators are defined in the ostream and
istream classes.
Overloading the << and >> Operators…
The first thing is to notice the type of the operator i.e. whether the
operator is binary or unary. The binary operator has two operands
while unary operators takes one. The number of operands cannot be
changed while overloading it.
Example program
Class Date
Class Matrix
Class Phone
Overloading the >> and << Operator – Example 1
class Date
{
public:
Date( )
{
cout << "\n Parameterless constructor called ...";
month = day = year = 0;
}
~Date ( )
{
cout << "\n Destructor called ...";
}
friend ostream & operator << ( ostream & os, Date &d );
friend istream & operator >> ( istream & is, Date &d );
private:
int month, day, year;
};
Overloading the >> and << Operator – Example 1
ostream & operator << ( ostream & os, Date &d )
{
os << d.day << "." << d.month << "." << d.year;
return os;
}
istream & operator >> ( istream & is, Date& d )
{
cout << "\n\n Enter day of the date: ";
is >> d.day;
cout << " Enter month of the date: ";
is >> d.month;
cout << " Enter year of the date: ";
is >> d.year;
return is;
}
main(void) {
Date date1, date2;
cout << "\n\n Enter two dates";
cin >> date1 >> date2;
cout << "\n Entered date1 is: " << date1 << "\n Entered date2 is: " << date2;
}
Overloading the >> and << Operator – Example 2
class Matrix{
private:
float Element[3][3];
int nRows, nCols;
public:
Matrix(int rows = 0, int cols = 0)
{
nRows = rows;
nCols = cols;
}
output<<endl;
}
return output;
}
void main(){
Matrix matrix(3,3);
cout<<"Enter 9 values to initialize Matrix \n";
cin>>matrix;
cout<<"output in Matrix form is \n";
cout<<matrix;
}
Overloading the >> and << Operator – Example 3
#include <iostream>
using std::ostream;
using std::istream;
#include <string>
using std::string;
class PhoneNumber
{
private:
string areaCode; // 3-digit area code
string exchange; // 3-digit exchange
string line; // 4-digit line
#include <iomanip>
using std::setw;
#include "PhoneNumber.h"
output << "(" << number.areaCode << ") " << number.exchange << "-" << number.line;
input.ignore(); // skip (
input >> setw( 3 ) >> number.areaCode; // input area code
input.ignore( 2 ); // skip ) and space
input >> setw( 3 ) >> number.exchange; // input exchange
input.ignore(); // skip dash (-)
input >> setw( 4 ) >> number.line; // input line
return input; // enables cin >> a >> b >> c;
int main()
{
PhoneNumber phone; // create object phone
cout << "Enter phone number in the form (123) 456-7890:" << endl;
// cin >> phone invokes operator>> by implicitly issuing the global function call operator>>( cin, phone )
// cout << phone invokes operator<< by implicitly issuing the global function call operator<<( cout, phone )
return 0;
} // end main
Exercise