Write a member function to swap two 'DATE' objects using pass by reference mechanism. code example
Example 1: passing reference in c++
#include <iostream>
using namespace std;
struct demo
{
int a;
};
int main()
{
int x = 5;
int y = 6;
demo d;
int *p;
p = &x;
p = &y;
int &r = x;
r = y;
p = NULL;
p++;
r++;
cout << &p << " " << &x << endl;
cout << &r << " " << &x << endl;
demo *q = &d;
demo &qq = d;
q->a = 8;
qq.a = 8;
cout << p << endl;
cout << r << endl;
return 0;
}
Example 2: calling by reference and pointers c++
#include <iostream>
using namespace std;
void swap(int*, int*);
int main()
{
int a = 1, b = 2;
cout << "Before swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
swap(&a, &b);
cout << "\nAfter swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}
void swap(int* n1, int* n2) {
int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;
}