-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path65IOstream_Operator_Overloading.cpp
More file actions
41 lines (34 loc) · 1.11 KB
/
65IOstream_Operator_Overloading.cpp
File metadata and controls
41 lines (34 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;
/*
The phrase iostream stands for 2 classes:
Ostream & istream classes.
The "<<" stream extraction opertator is a object of the class Ostream which is also called console output
The ">>" stream insertion operator is a object of the class Istream which is also called console input
NOTE: it can't be class member function, you have to define it as the friend function of your class
NOTE 2: istream and ostream classes already have the overloaing of intergers or strings or other data type but not the class.
*/
class Test{
int x;
public:
Test(){
x = 0;
}
friend istream &operator >>(istream &input, Test &obj);
friend ostream &operator <<(ostream &output, Test &obj);
};
//Insertiion and extraction operator overloading
istream &operator >>(istream &input, Test &obj){
input>> obj.x;
return input;
}
ostream &operator <<(ostream &output, Test &obj){
output<< obj.x<<endl;
return output;
}
int main(){
Test t;
cin>>t;
cout<<t; //if we dont use cin here, then the cout will give out default value 0.
return 0;
}