#include <iostream>
usingnamespace 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.
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