-1

The following is how I was taught to use constructors, and it seems to work for one variable, but when I use a few it seems to act odd.

I'm not too sure what to do to fix this, but I would like some direction. Thanks in advance!

#include <iostream>
#include <string>

using namespace std;

class Numbers
{
public:
    Numbers (int a, int b, int c)
    {
        setNum (a);
        setNum (b);
        setNum (c);
    }

    void setNum (int x, int y, int z)
    {
        numbers = x;
        digits = y;
        numerals = z;
    }

    int getNum ()
    {
        return numbers;
        return digits;
        return numerals;
    }

    int add (int x, int y, int z)
    {
        int answer = x + y + z;
        return answer;
    }

private:
    int numbers;
    int digits;
    int numerals;
};

int main ()
{
    Numbers numbersobject (12,13,14);
    cout << numbersobject.getNum () << endl;

    return 0;
}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
METEORITES
  • 43
  • 1
  • 6

3 Answers3

2

Odd meaning compile error?!

In the constructor, look at your setNum, it must take 3 parameters. You probably need

setNum(a,b,c);

And you CANNOT return 3 elements in getNum, using 3 return statements. If you need to return more than 2 elements, use std::tuple, or std::pair for 2 elements.

vsoftco
  • 55,410
  • 12
  • 139
  • 252
0

You could change your constructor to use a member initialization list

Numbers (int a, int b, int c)
: numbers {a}, digits {b}, numerals {c} {}

Or the older version

Numbers (int a, int b, int c)
{
    numbers = a;
    digits = b;
    numerals = c;
}
Community
  • 1
  • 1
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

I suggest you simplify your constructor

class Numbers
{
  public:  
    Numbers(int a, int b, int c)
    : numbers(a), digits(b), numerals(c) // Initialization list
    { ; }
    //...
};

In your simple class, there is no need for the constructor to call a setter function.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154