coding a loop inside a class to use in main

I am writing a program where all work is done in the class methods. Main is used to call the methods. I need to know how to get a loop to work without any variables in main that can be used outside the methods.
This is what I have in main:

#include <iostream>
#include <string>
using namespace std;

#include "FerryBoat.h"

int main()
{
//create a constructor for a ferry boat
FerryBoat myBoat('B', 20, 'A');
int unloadVics=0;
char currentPort=' ';
char destPort=' ';
char newPort=' ';


myBoat.setPortAvics();
myBoat.setPortBvics();

myBoat.loadCars(myBoat.getPortAvics());
myBoat.moveToPort(myBoat.getCurrentPort()+1);
myBoat.unloadCars(unloadVics);
myBoat.setPortAcars();

myBoat.loadCars(myBoat.getPortBvics());
myBoat.moveToPort(myBoat.getDestPort()-1);
myBoat.unloadCars(unloadVics);
myBoat.setPortBcars();


return 0;
}
any help would be appreciated. the sooner the better
You could have a function defined in your class, which can iterate through your class content. Or have a class including an array of ferryBoat objects as a member:

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
class ferryBoat
{

//...

};

class ferryBoats
{
public:
void BoatsFunctionWithLoopInside(); // <--------

protected:
std::vector<ferryBoat> boatsVector;
};

void ferryBoats::BoatsFunctionWithLoopInside()
{
for(unsigned int i=0;i<boatsVector.size(); i++)
  {
   // ...
  }

}

int main()
{
ferryBoats myBoats;

//(fill the vector of boats ferryBoats::boatsVector)

//(call the function  ferryBoats::BoatsFunctionWithLoopInside() )

return 0;
}
Last edited on
That is what I needed, and thank you for you quick responce
Great, glad to know! You're very welcome.
Topic archived. No new replies allowed.