Advertisement
TechOPGOHIL

Untitled

Dec 3rd, 2023
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. //write a program to unary operator overload with friend function
  2. #include<iostream.h>
  3. #include<conio.h>
  4. class demo
  5. {
  6.     int a,b;
  7.     public:
  8.     demo()
  9.     {
  10.         a=10;
  11.         b=-20;
  12.     }
  13.     friend void operator -(demo &object)     // Changes Are here
  14.     {
  15.          object.a=-object.a;
  16.          object.b=-object.b;
  17.     }              // Changes Done Replace Friend In Above Void And Create Object
  18.     void display()
  19.     {
  20.         cout<<"\nA ="<<a<<endl;
  21.         cout<<"\nB ="<<b<<endl;
  22.     }
  23. };
  24. void main()
  25. {
  26.     clrscr();
  27.     demo object; // object is a object if you know this allredy then excelent.
  28.     object.display();
  29.     -object; // operator overload finding - sign operator in above void main.
  30.     cout<<"\nOperator Overload Sucessfully Using Friend Function... check below\n";
  31.     object.display();
  32.     getch();
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement