Write a program to implement pointer to object in a class TRAIN. Train_Number , Train_Name, Arrival_Hr ,Arrival_Min, TimeDiff() are members in class. Member function TimeDiff() is used to find the time difference between Arrival Time (Arrival_Hr and Arrival_Min) and Reached Time ( New given Hr and Min for late arrival) and display the calculated time difference of the specified train. Use pointer to array of objects for different trains.
Is this program the correct answer to the question? I'm asking because I don't know what "Use a pointer to array of objects for different trains" means.
This is the code that I came up with:
#include <iostream>
using namespace std;
class TRAIN{
public: int Train_Number;
public: string Train_Name;
int Arrival_Hr;
int Arrival_Min;
TRAIN(int Hr1,int Mn1, int Hr2, int Mn2) {
Arrival_Hr = Hr2-Hr1;
Arrival_Min = Mn2-Mn1;
cout<<Arrival_Hr<<"Hr"<<Arrival_Min<<"min is the difference";
}
};
int main(){
TRAIN* ptr[10];
ptr[0] = new TRAIN(2,30,4,40);
ptr[1] = new TRAIN(1,20,5,30);
ptr[0]->Train_Number = 100;
ptr[0]->Train_Name = "Jansadabti";
cout<<ptr[0]->Train_Number;
cout<<ptr[0]->Train_Name;
return 0;
}
TRAIN* ptr[10];, but I think you are supposed to to use a pointer to a dynamically allocated array likeTrain *ptr = new TRAIN[10];