Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
81 views
in Technique[技术] by (71.8m points)

Pointer to array of objects in C++

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;
}
question from:https://stackoverflow.com/questions/65651060/pointer-to-array-of-objects-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...