the program take 2 input, and display the add, minus, and max of the 2 number entered.
>>get input
>>send values to class
>>call friend function (add, minus, max)
>>display the values
now i need to create another object using copy constructor and repeat the 3rd and 4th step.
any idea how? i refer to some examples but i seem cant understand.
pls help guys.
tqvm
To use copy constructor just create a constructor with a NUMBERS as argument:
1 2 3
NUMBERS(const NUMBERS & NUM)
:x(NUM.x),y(NUM.y) //this just use initialization list to set the x,y of your NUMBERS object
{};
I would also suggest not to use all-capital manes (like NUMBERS) for your classes and objects since that is considered reserved for MACROS (it's just a convention but it helps to maintain it)
While your program is ok from a C++ syntax point of view, it is not good o-o design.
In particular, friends should be avoided whenever possible, as they break encapsulation. An object should act on its own data. Not get someone else (a friend!) to do the work for them!
Maybe something like
1 2 3 4 5 6 7 8 9 10 11 12 13
class NUMBERS
{
private :
int x, y;
public :
NUMBERS( );
NUMBERS(const NUMBERS & NUM);
void setinput(int m, int n);
int getsum();
...
};
1 2 3 4 5 6
int NUMBERS::getsum()
{
int sum; // could prob do without temp?
sum = (x + y);
return sum;
}
P.S. I changed "addition" to "getsum" as the result of addition is the sum, and it helps to think in terms of nouns/verbs when doing o-o programming: the objects/classes = nouns; when they do (their methods) = verbs. There's more to the "noun-verb metaphor" approach when writing o-o code, but it's a useful starting point.