class arrays

How do you declare an object of a class that is an array. This is my code to try to get across what I'm asking.

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
#include <iostream>
using namespace std;

class jeff
{
      private:
              int x;
              int y;
      public:
              jeff( int x1, int x2 );
              int add();
};

jeff::jeff( int x1, int y1 )
{
	x = x1;
	y = y1;
}

int jeff::add( )
{
     return ( x+y );
}

int main()
{
    jeff one[2];
    one[0] = ( 3, 4 );
    one[1] = ( 5, 6 );
    cout << one[0].add();
    cout << one[1].add();
    return 0;
}


I know this won't work but I hope it illustrates my point. I want a variable 'one' that is of type 'jeff' that is an array. How do I use a constructor to initialize the values of each element in the array so that when I call the function 'add()' it will return the correct answer? Thanks guys.
Last edited on
closed account (D80DSL3A)
Your constructor is fine. You can declare and initialize an array of objects like so:
jeff one[] = { jeff(3,4), jeff(5,6) };// creates an array of 2 jeffs
That totally makes sense, thanks man!
Topic archived. No new replies allowed.