#include <iostream>
using namespace std;
class swap_value {
int a,b;
public:
swap_value()
{
cout<<"enter two numbers to swap";
cout<<"value of a is:";
cin>>a;
cout<<"value of b is:";
cin>>b;
}
void swap(int &a,int &b)
{
int temp=a;
a=b;
b=temp;
cout<<"value of a is :"<<a;
cout<<"value of b is :"<<b;
}
};
int main() {
swap_value obj;
obj.swap();
return 0;
}
The thing that you should know is that all members of a class are accessible to all the methods of your class. So they don't need to be passed as parameters. The code you wrote doesn't compile (include error messages next time) because SwapValue doesn't have a method called swap() that takes no parameters.
Here is your code that does what you want it to. The change that I made was simply to make SwapValues::swap()
take no parameters. Also, it is common practice to use UpperCamelCase for class names in C++. Indeed, it took me more time than it should have to notice that swap_values(){...} was your constructor for that reason.
#include <iostream>
using namespace std;
class SwapValues {
int a,b;
public:
SwapValues()
{
cout<<"enter two numbers to swap";
cout<<"value of a is:";
cin>>a;
cout<<"value of b is:";
cin>>b;
}
void swap()
{
int temp=a;
a=b;
b=temp;
cout<<"value of a is :"<<a << std::endl;
cout<<"value of b is :"<<b << std::endl;
}
};
int main() {
SwapValues sv;
sv.swap();
return 0;
}
Edit
To respond to the follow-up general question underlying the problem, the reason you are confused is because you are not sure about how the function gets access to a and b.
The short version is that methods of a class have built in access to all the attributes of the class.
The long version is that the instance of the class gets implicitly passed to methods of the class as an implicit parameter (called this
in C++
). So all functions of a class MyClass
like
int MyClass::my_function( --parameters-- )
become
int MyClass::my_function(MyClass* this, --parameters--)
and the compiler, when it compiles code of such a function, sees
some_var = 1234;
will check firs if the function has a local variable or a local static variable or a parameter called some_var, and if there isn't, it will see if
this->some_var = 1234;
that is, it will check if `MyClass has an attribute called some_var.
In your case, in the working code we have
int temp = a;
so the compiler looks: is there a local variable called a
, no local variable, is there a parameter called a
, nope, next, what about this->a
, the class does have an attribute called a
so that's what we use.
So you always pass parameters, is the answer to your question but in the example of your function, that is done automatically. That's good because if you add an attribute to your class, you don't have to alter all the methods of your class to take an extra parameter, all the methods of the class will already have it.
In your first example, in fact, you should run this piece of code. It will show what I mean.
#include <iostream>
using namespace std;
class SwapValues {
public:
int a,b;
SwapValues()
{
a = 1;
b = 2;
}
void wrong_method(int &a, int &b)
{
std::cout << "wrong_method : "
<< "a = " << a
<< ", b = " << b
<< ", this->a = " << this->a
<< ", this->b = " << this->b << std::endl;
}
void right_method(int ¶m_1, int& param_2)
{
std::cout << "right_method : " << "a = " << a
<< ", b = " << b
<< ", param_1 = " << param_1
<< ", param_2 = " << param_2 << std::endl;
}
};
int main() {
SwapValues sv;
int c = 3;
int d = 4;
sv.wrong_method(c,d);
sv.right_method(c,d);
return 0;
}
Gives
wrong_method : a = 3, b = 4, this->a = 1, this->b = 2
right_method : a = 1, b = 2, param_1 = 3, param_2 = 4
This should demonstrate what I meant. The right and wrong because the wrong function has a name clash between the parameters and the attributes of the object. It makes errors possible and reflects bad design.