bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_2 / eduincs / pgrm_011.cpp

pgrm_011.cpp (1525B)


      1 /* call by value, call by address, call by reference */
      2 
      3 #include<iostream>
      4 using namespace std;
      5 class Swap {
      6 private: 
      7     int a, b;
      8 public: 
      9     void getData() {
     10         cout << "Enter two numbers: ";
     11         cin >> a >> b;
     12     }
     13 
     14     // call by value
     15     void swapValue(int x, int y) {
     16         int temp = x;
     17         x = y;
     18         y = temp;
     19         cout << "Inside swapValue(): " << x << " " << y << endl;
     20     }
     21 
     22     // call by address
     23     void swapAddress(int *x, int *y) {
     24         int temp = *x;
     25         *x = *y;
     26         *y = temp;
     27         cout << "Inside swapAddress(): " << *x << " " << *y << endl;
     28     }
     29 
     30     // call by reference
     31     void swapReference(int &x, int &y) {
     32         int temp = x;
     33         x = y;
     34         y = temp;
     35         cout << "Inside swapValue(): " << x << " " << y << endl;
     36     }
     37 
     38     void display() {
     39         cout << "Current values: " << a << " " << b << endl;
     40     }
     41 
     42     void test() {
     43         cout << "\nOriginal values: " << endl;
     44         display();
     45 
     46         cout << "\n== Call by Value ==" << endl;
     47         swapValue(a, b);
     48         cout << "After swapValue(): " << endl;
     49         display();
     50 
     51         cout << "\n== Call by Address ==" << endl;
     52         swapAddress(&a, &b);
     53         cout << "After swapAddress(): " << endl;
     54         display();
     55 
     56         cout << "\n== Call by Reference ==" << endl;
     57         swapReference(a, b);
     58         cout << "After swapReference(): " << endl;
     59         display();
     60     }
     61     ~Swap() {}
     62 };
     63 
     64 int main() {
     65     Swap obj;
     66     obj.getData();
     67     obj.test();
     68     return 0;
     69 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror