-3

I wish to create a constructor which takes input value from user, i.e value of a and b and then I wish to use these values in another function swap. This is more of general example. I wish to know where my concept is wrong.

#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;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    Please read the [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Also read the [help section](http://stackoverflow.com/help) on dos and donts when asking a question. Other than that, welcome to SO. –  May 06 '17 at 15:57
  • Please read this [how-to-ask](http://stackoverflow.com/help/how-to-ask) to follow the guideline and refine your question with sufficient information such as error message if any. – thewaywewere May 06 '17 at 16:40
  • Please revise the title to describe the specific problem. "What is wrong with this code" is too vague and describes pretty much every question on this site. – Raymond Chen May 11 '17 at 03:51

2 Answers2

0
#include <iostream>
using namespace std;
class swap_value
{
private:
    int a;
    int 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 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;
}
Jonas
  • 6,915
  • 8
  • 35
  • 53
tgw
  • 1
0
#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 &param_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.