我目前无法弄清楚在main中打出一个阵列中所有5个流动站的语法,我现在可以获得所有Rover数据并将其存储到每个Rover但我无法弄清楚如何制作他们的数组。如果有人可以提供帮助,那将非常值得赞赏。
class Rover{
private:
string name;
int xpos;
int ypos;
string direction; //Use Cardinal Directions (N,S,E,W)
int speed; //(0-5 m/sec)
public:
//Constructors
Rover();
Rover(string,int,int,string,int);
//Get functions
string getName()const;
int getXpos()const;
int getYpos()const;
string getDirect()const;
int getSpeed()const;
void getRoverData();
//Set functions
void setName(string);
void setXpos(int);
void setYpos(int);
void setDirect(string);
void setSpeed(int);
};
//Constructor function
Rover::Rover()
{
xpos=0;
ypos=0;
direction="N";
speed=0;
}
Rover::Rover(string nme,int xp, int yp, string dir, int sp) :
name(nme), xpos(xp), ypos(yp), direction(dir), speed(sp)
{
}
Rover readRover()
{
string nme;
int xp;
int yp;
string dir;
int sp;
cout<< "Please enter the Name of the Rover: ";
cin>> nme;
cout << "Please enter the starting X-position: ";
cin >> xp;
cout << "Please enter the starting Y-position: ";
cin >> yp;
cout << "Please enter the starting direction (N,S,E,W): ";
cin >> dir;
cout << "Please enter the starting speed (0-5): ";
cin >> sp;
// Construct an object with the user input data and return it.
return Rover(nme, xp, yp, dir, sp);
}
//Getter functions
string Rover::getName()const
{
return name;
}
int Rover::getXpos()const
{
return xpos;
}
int Rover::getYpos()const
{
return ypos;
}
string Rover::getDirect()const
{
return direction;
}
int Rover::getSpeed()const
{
return speed;
}
void Rover::getRoverData()
{
cout<<name;
cout<<xpos;
cout<<ypos;
cout<<direction;
cout<<speed;
}
//Setter functions
void Rover::setName(string nme)
{
cin>>nme;
name=nme;
}
void Rover::setXpos(int x)
{
cin>>x;
xpos=x;
}
void Rover::setYpos(int y)
{
cin>>y;
ypos=y;
}
void Rover::setDirect(string direct)
{
cin>>direct;
direction=direct;
}
void Rover::setSpeed(int spd)
{
cin>> spd;
speed=spd;
}
int main(int argc, char** argv) {
int MAX=5;
Rover Rovers[MAX];
cout<<"Please enter Rover 1's information"<<endl;
Rover r1=readRover();
cout<<endl;
cout<<"Please enter Rover 2's information"<<endl;
Rover r2=readRover();
cout<<endl;
cout<<"Please enter Rover 3's information"<<endl;
Rover r3=readRover();
cout<<endl;
cout<<"Please enter Rover 4's information"<<endl;
Rover r4=readRover();
cout<<endl;
cout<<"Please enter Rover 5's information"<<endl;
Rover r5=readRover();
cout<<endl;
r1.getRoverData();
答案 0 :(得分:0)
您可以尝试以下内容:
int main() {
int max = 5;
Rover rovers[max];
for (int i = 0; i < max; ++i) {
std::cout << "Please enter Rover " << i + 1 << "'s information" << endl;
rovers[i] = readRover();
}
}