Create a C++ program for Classes and Pointers similar to the given example.
#include<iostream>
using namespace std;
//This program uses this pointer
class Swapper{
private:
int first, second;
public:
Swapper(int a, int b){
first = a;
second = b;
}
void Swap(){
//Using this pointer (this->)
int temp;
temp = this-> first;
first = this->second;
this->second= temp;
cout<<"Swapped correctly\nThe first number is:\t"<<this->first<<"\nThe second number is:\t"<<this->second<<endl;
}
};
int main(){
int n, z;
cout<<"Enter the first number:\n"<<endl;
cin>>n;
cout<<"Enter the second number\n"<<endl;
cin>>z;
Swapper y(n,z);
y.Swap();
}
Comments
Leave a comment