copy constructor

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <iomanip>
using namespace std;

class NUMBERS
{
    private :
        int x, y;
    public :
        NUMBERS( ){};

    void setinput(int m, int n)
    {
        x=m;
        y=n;
    }

    friend int addition(NUMBERS);

    friend int subtract(NUMBERS);

    friend int max_min(NUMBERS);
};

int main()
{
    NUMBERS object;
    int x, y;

    cout << "enter 1st number : ";
    cin >> x;

    cout << "enter 2nd number : ";
    cin >> y;

    object.setinput(x,y);
    addition(object);
    subtract(object);
    max_min(object);

    cout << "addition    : " << addition(object) << endl;
    cout << "subtract    : " << subtract(object) << endl;
    cout << "max         : " << max_min(object) << endl;

    return 0;

}

    int addition (NUMBERS object)
    {
        int add;
        add = (object.x + object.y);
        return add;
    }

    int subtract (NUMBERS object)
    {
        int sub;
        sub = (object.x - object.y);
        return sub;
    }

    int max_min (NUMBERS object)
    {
        int max;
        if (object.x > object.y)
            max=object.x;
        else
            max=object.y;
        return max;
    }
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <iomanip>
using namespace std;

class NUMBERS
{
    private :
        int x, y;
    public :
        NUMBERS( ){};
	NUMBERS(const NUMBERS & NUM):x(NUM.x),y(NUM.y) //this just use initialization list to set the x,y of your NUMBERS object
{};

    void setinput(int m, int n)
    {
        x=m;
        y=n;
    }

    friend int addition(NUMBERS);

    friend int subtract(NUMBERS);

    friend int max_min(NUMBERS);
};

int main()
{
    NUMBERS object;
    int x, y;

    cout << "enter 1st number : ";
    cin >> x;

    cout << "enter 2nd number : ";
    cin >> y;

    object.setinput(x,y);

    cout << "addition    : " << addition(object) << endl;
    cout << "subtract    : " << subtract(object) << endl;
    cout << "max         : " << max_min(object) << endl;

    return 0;

}

    int addition (NUMBERS object)
    {
        int add;
        add = (object.x + object.y);
        return add;
    }

    int subtract (NUMBERS object)
    {
        int sub;
        sub = (object.x - object.y);
        return sub;
    }

    int max_min (NUMBERS object)
    {
        int max;
        if (object.x > object.y)
            max=object.x;
        else
            max=object.y;
        return max;
    }
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;
}


1
2
3
4
    ...

    cout << "sum : " << object.getsum() << endl;
    ....


Andy

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.
Last edited on
the functions dont need all that extra code.

1
2
3
4
5
6
7
int NUMBERS::getsum()
{
    return x+ y;
}


thanks a lot guys!!
solved my prob already..
:)
Topic archived. No new replies allowed.