omargamal8

return by refrence

Mar 21st, 2022
825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3.                               Online C++ Compiler.
  4.                Code, Compile, Run and Debug C++ program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. #include <iostream>
  10. using namespace std;
  11.  
  12. class MyInt{
  13.     public:
  14.         int my_int;
  15.         MyInt():my_int(0){}
  16.         MyInt(int i): my_int(i){}
  17. };
  18.  
  19. MyInt& returnInt(MyInt& my_int){
  20.     return my_int;
  21. };
  22.  
  23. int& returnPureInt(int& my_int){
  24.     ++my_int;
  25.     return my_int;
  26. };
  27.  
  28. int main()
  29. {
  30.     MyInt my_10 = MyInt(10);
  31.     MyInt my_11 = returnInt(my_10);
  32.     // MyInt& my_11 = my_10;
  33.     my_11.my_int = 11;
  34.    
  35.     cout<<"Hello World "<<my_10.my_int<<" "<<my_11.my_int;
  36.    
  37.     // int pure_int = int(100);
  38.     // int pure_int_2 = returnPureInt(pure_int);
  39.     // ++pure_int_2;
  40.     // cout <<"Hello world " << pure_int <<" "<<pure_int_2;
  41.     return 0;
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment