Help on assignment

One part of my assignment is to implement one class to get the values and set the values.

The 5 parameters are
1st Parameter : sunType, String
2nd Parameter : noOfEarthLikePlanets, integer
3rd Parameter : noOfEarthLikeMoons, integer
4th Parameter : aveParticulateDensity, float
5th Parameter : avePlasmaDensity, float


Class Name : LocationData

Constructors :

1st Constructor –
no parameters. Initialize all String variables to empty string, all int and float variables to 0.

2nd Constructor –
5 parameters. Initialize all attribute variables to the parameter’s values respectively.

then there is another method computeCivicIndex() which calculates the civilization based on the 5 values which are gotten from user input from the main menu.

My question is how do i get the values from the user input and call the values in the computeCivicIndex() method.
I used a couple of get and set methods to get the values

Example

void locationData::setSunType(string sun)
{
sunType=sun;

}
string locationData::getSunType(string sunT)
{
return sunT;
}
Ask the user for the input, call the set methods accordingly (or create a dynamic instance, passing the inputs to the constructor parameter) then, presuming computerCivicIndex is a function of that calls, call it to perform the functionality needed. If it's in the same class, it'll be aware of the variables, which will have been populated in the prior steps.
The inputs will be keyed in via a main menu that i will be creating. It has been done.

what i am unsure of is that how am i supposed to call the methods inside another function call.

should it be this way?

example

locationData::computeCivicIndex(string sun)
{
locationData::sunType=sun;

}
So the computeCivicIndex is in the locationData class, yes?

If it calculates the population, is it to return an integer number?
Yes, it is in the locationData class
it is to return a float. using a specific formulae that has been given
Okay, so it'll be something along the lines of:
1
2
3
4
5
float locationData::computeCivicIndex()
{
   // forumla here
   // return the result
}


If it's in the same class as the five values it uses for the formula, you can just reference them in the function.

When you call the function, you'd just store the result in a float:
1
2
float result;
result = data.computeCivicIndex();
Last edited on
Okay is it possible to pass in 5 arguments into the function?

for example

float locationData::computeCivicIndex(string a,int b,int c,float d,float e)
{


}

and how do i go about referencing the functions?

should it be something like this

locationData::getSunType()=a;?

If it's part of the class that has the values for the parameters stored in it already there's no real need to pass anything into it. Consider this small example (note, I've made everything public so I can easily assign values for example purposes):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class MyClass
{
public:
   int a, b, c;
   int Add();  // Function to add three elements
}

int MyClass::Add()  // No need for params, function is aware of a,b and c.
{
   return a+b+c;
}

// in main...
MyClass thing;
thing.a = 1;
thing.b = 2;
thing.c = 3;

int result;
result = thing.Add();
cout << result; // result = 6 
Last edited on
Okay thanks alot =)
Topic archived. No new replies allowed.