Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // insertion operator overloading
- // normal way to display the data members of object
- #include <iostream>
- using namespace std;
- class complex
- {
- private:
- int real;
- int img;
- public:
- complex(int a=0 , int b=0 ){ // non parameterized constructor
- real = a;
- img = b;
- }
- void display(){
- cout<<real<<" + i"<<img<<endl;
- }
- };
- int main()
- {
- complex c1(8,5);
- c1.display();
- return 0;
- }
- // now with operator<< that is insertion operator overloading
- #include <iostream>
- using namespace std;
- class complex
- {
- private:
- int real;
- int img;
- public:
- complex(int a=0 , int b=0 ){ // non parameterized constructor, if no value passed default it will be zero
- real = a;
- img = b;
- }
- friend ostream & operator<<(ostream &suresh , complex &c);
- };
- ostream & operator<<(ostream &suresh , complex &c)
- {
- suresh<<c.real<<" + i"<<c.img<<endl;
- }
- int main()
- {
- complex c(8,5);
- cout<<c; // same as operator<<(cout,c);
- return 0;
- }
- /*
- 8 + i5
- */
Add Comment
Please, Sign In to add comment