Apples and bananas for the objects can be declared using the following method add this to the public section of your class.
1 2 3 4 5 6 7 8 9 10 11
void addApplesBananas()
{
for(int i=0;i<9;i++)
{
fruits[i].apples = 10;
fruits[i].bananas = 100;
}
}
//That will make each object fruits in the array has the number of apples = 10 and number
//Of bananas = 100.
class First // not Class
{
public:
int apples;
int bananas;
}; // semi-colon goes here
class Second
{
public:
First fruits[10];
};
int main()
{
Second s;
s.fruits[0].apples = 3;
s.fruits[0].bananas = 4;
return 0;
}
#include <iostream>
usingnamespace std;
class First
{
public:
int apples;
int bananas;
};
class Second
{
public:
First fruits[10];
void addApplesBananas()
{
for(int i=0;i<10;i++)
{
fruits[i].apples = 10;
fruits[i].bananas = 100;
}
}
};
int main()
{
Second x;
x.addApplesBananas();
for(int i=0;i<10;i++)
{
cout << "Apples in " << i+1 << " th element = " << x.fruits[i].apples << endl;
cout << "Bananas in " << i+1 << " th element = " << x.fruits[i].bananas << endl << endl << endl;
}
}
Outputs:
Apples in 1 th element = 10
Bananas in 1 th element = 100
Apples in 2 th element = 10
Bananas in 2 th element = 100
Apples in 3 th element = 10
Bananas in 3 th element = 100
Apples in 4 th element = 10
Bananas in 4 th element = 100
Apples in 5 th element = 10
Bananas in 5 th element = 100
Apples in 6 th element = 10
Bananas in 6 th element = 100
Apples in 7 th element = 10
Bananas in 7 th element = 100
Apples in 8 th element = 10
Bananas in 8 th element = 100
Apples in 9 th element = 10
Bananas in 9 th element = 100
Apples in 10 th element = 10
Bananas in 10 th element = 100