Answer to Question #208639 in C++ for Angela

Question #208639

Create a C++ program for Classes and Pointers similar to the given example.



1
Expert's answer
2021-06-20T08:11:11-0400
#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();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS