1

I have this function:

void fraction::init()
{
  cout<<"enter the values for the numerator and denominator\n";
  cin>>num;
  cin>>denom;
}

and I want these two numbers to be used in another function that manipulate them. how do I take them to this other function? NOTE:both functions are of the same classs.

Life of Madness
  • 784
  • 1
  • 5
  • 21
Samuel
  • 612
  • 2
  • 9
  • 25
  • Honestly, this question shows that you are lacking basis about how classes work and should be used for, try to get a good book. – Antonio Jan 10 '14 at 11:10

3 Answers3

5

Simply define them as members of the class:

class fraction {
private:
  int num;
  int denom;

public:
   void init();          // has access to num and denom
   void otherMethod();   // also has access to num and denom
};

You should also not rely on init() being called to initialize the variables with a default value, and you should also not rely on the various default initializations provides by C++, see Default variable value.

Instead, add a constructor which sets default values for the variables to make sure that the variables have reasonable values after an object of the class has been created. Then, you can still call init() to set whatever values you want (and probably you should rename init() to something like readValues() to reflect what the method really does).

Community
  • 1
  • 1
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
-1

It depends on how you will be using those functions. Since both belong to the same class, you can make num and denom (private) members of the class. But you can also use references if those values come from outside the class. This will work regardless of the functions belonging to the same class or not:

class fraction
{
public:
  void init(int& num, int& denom) const;
  void otherFunction(int num, int denom);
}

void fraction::init(int& num, int& denom) const
{
  cout<<"enter the values for the numerator and denominator\n";
  cin>>num;
  cin>>denom;
}

// Define otherFunction() appropriately

Then use those functions in another piece of code:

// ...
int aNum, aDenom;
fraction fr;
fr.init(aNum, aDenom);
fr.otherFunction(aNum, aDenom);
// ...
Gorpik
  • 10,940
  • 4
  • 36
  • 56
  • The final use you suggest might be correct, but it's extremely dangerous, it's really the case to suggest member variables in this specific case (a fraction once initialized has to own its numerator and denominator) – Antonio Jan 10 '14 at 11:08
  • @Antonio: If `fraction` is really a fraction, `num` and `denom` being its numerator and denominator, then it is clear that they should be data members. But, in this case, the question is just about the absolute basics of object oriented programming. So I preferred not to take into account the possible meanings of those names and consider that it was just an ill-devised example. – Gorpik Jan 10 '14 at 15:41
  • 1
    "The absolute basics of object oriented programming" generally state that Init functions are bad news. – Lightness Races in Orbit Feb 17 '14 at 13:57
-1

Either make them members of the class, as so many suggest, or you could try passing them when needed. If you want their values to reflect changes, pass them by reference.

datatype function1(int *a, int *b)

And then when you call them from init() function,

function1(&num, &denom)

Hope this helps

Apoorv Ashutosh
  • 3,834
  • 7
  • 23
  • 24
  • 2
    This way of passing by reference using pointers is C-like, check the examples from previous answers to know how to properly do it in C++ – Antonio Jan 10 '14 at 11:05