Computer Science, asked by bhabanishankar, 9 months ago

write a c++ program to swap two values without using third variable.
Implement the program with the help of default parametrized copy and
dyanamic constrcutor also use destructor to destory all the object​

Answers

Answered by rajansharma46
0

Answer:

using namespace std;

class Swap {

// Declare the variables of Swap Class

int temp, a, b;

public:

// Define the parametrized constructor, for inputs

Swap(int a, int b)

{

this->a = a;

this->b = b;

}

// Declare the friend function to swap, take arguments

// as call by reference

friend void swap(Swap&);

};

// Define the swap function outside class scope

void swap(Swap& s1)

{

// Call by reference is used to passed object copy to

// the function

cout << "\nBefore Swapping: " << s1.a << " " << s1.b;

// Swap operations with Swap Class variables

s1.temp = s1.a;

s1.a = s1.b;

s1.b = s1.temp;

cout << "\nAfter Swapping: " << s1.a << " " << s1.b;

}

// Driver Code

int main()

{

// Declare and Initialize the Swap object

Swap s(4, 6);

swap(s);

return 0;

}

INPUT 2 , 3

OUTPUT. 3 , 2

Explanation:

MARK ME BRAINLIEST ♥️

Answered by Anonymous
2

Explanation:

#include <iostream>

using namespace std;

int main()

{

int a=5, b=10;

cout<<"Before swap a= "<<a<<" b= "<<b<<endl;

a=a*b; //a=50 (5*10)

b=a/b; //b=5 (50/10)

a=a/b; //a=10 (50/5)

cout<<"After swap a= "<<a<<" b= "<<b<<endl;

return 0;

}

Output:

Before swap a= 5 b= 10

After swap a= 10 b= 5

Program 2: Using + and -

Let's see another example to swap two numbers using + and -.

#include <iostream>

using namespace std;

int main()

{

int a=5, b=10;

cout<<"Before swap a= "<<a<<" b= "<<b<<endl;

a=a+b; //a=15 (5+10)

b=a-b; //b=5 (15-10)

a=a-b; //a=10 (15-5)

cout<<"After swap a= "<<a<<" b= "<<b<<endl;

return 0;

}

Output:

Before swap a= 5 b= 10

After swap a= 10 b= 5

Similar questions