The program is a good start, but as-is it does not correctly address the things the question asks for.
You are missing the TimeDiff()
method. What you have put into the TRAIN
constructor is supposed to be in the TimeDiff()
method instead.
The question asks for a “pointer to array of objects”, but you are using an “array of pointers to objects” instead.
Try something more like this:
#include <iostream>
using namespace std;
class TRAIN{
public:
int Train_Number;
string Train_Name;
int Arrival_Hr;
int Arrival_Min;
int TimeDiff(int Reached_Hr, int Reached_Min) const {
int a_mins = (Arrival_Hr * 60) + Arrival_Min;
int r_mins = (Reached_Hr * 60) + Reached_Min;
return r_mins - a_mins;
}
};
int main() {
TRAIN* trains = new TRAIN[10];
trains[0].Train_Number = 100;
trains[0].Train_Name = "Jansadabti";
trains[0].Arrival_Hr = 2;
trains[0].Arrival_Min = 30;
// populate other trains as needed...
cout << trains[0].Train_Number << " " << trains[0].Train_Name << ", Time Difference: " << trains[0].TimeDiff(4, 40) << " minute(s)";
delete[] trains;
return 0;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…