-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 19
1/20
Object-Oriented Programming
(OOP)Lecture No. 19
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 19
2/20
Stream Insertion operatorOften we need to display the dataon the screen
Example:
int i=1, j=2;
cout
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 19
3/20
Stream Insertion operator
Complex c1;
cout
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 19
4/20
Stream Insertion operatorclass Complex{
public:
void operator
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 19
5/20
Stream Insertion operatorint main(){
Complex c1;
cout
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 19
6/20
Stream Insertion operatorclass Complex{
public:
void operator
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 19
7/20
Stream Insertion operator
void Complex::operator
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 19
8/20
Stream Insertion operatorclass Complex{
...
friend ostream & operator
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 19
9/20
Stream Insertion operator// we want the output as: (real, img)
ostream & operator
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 19
10/20
Stream Insertion operatorComplex c1(1.01, 20.1),
c2(0.01, 12.0);
cout
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 19
11/20
Stream Insertion operatorOutput:
( 1.01 , 20.1 )
( 0.01 , 12.0 )
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 19
12/20
Stream Insertion operatorcout
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 19
13/20
Stream Extraction OperatorOverloading >>operator:
class Complex{
...
friend istream & operator
>> (istream & i, Complex &c);
};Note: this objectis NOT const
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 19
14/20
Stream Extraction Operatoristream & operator > c.real;
in >> c.img;
return in;}
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 19
15/20
Stream Extraction OperatorMain Program:Complex c1(1.01, 20.1);
cin >> c1;
// suppose we entered// 1.0025 for c1.real and// 0.0241 for c1.img
cout
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 19
16/20
Stream Extraction Operator
Output:( 1.0025 , 0.0241 )
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 19
17/20
Other inary operatorsOverloading comparison operators:
class Complex{
public:
bool operator == (const Complex & c);//friend bool operator == (const//Complex & c1, const Complex & c2);
bool operator != (const Complex & c);
//friend bool operator != (const//Complex & c1, const Complex & c2);
};
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 19
18/20
Other inary operatorsbool Complex::operator ==(constComplex & c){
if((real == c.real) &&
(img == c.img)){
return true;
}
elsereturn false;
}
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 19
19/20
Other inary operatorsbool operator ==(const
Complex& lhs, const Complex& rhs){
if((lhs.real == rhs.real) &&
(lhs.img == rhs.img)){
return true;
}
else
return false;
}
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 19
20/20
Other inary operatorsbool Complex::operator !=(constComplex & c){
if((real != c.real) ||
(img != c.img)){
return true;
}
elsereturn false;
}