178k views
5 votes
Define a class named Train representing the following members:

Data Members:

Train Number
Train Name
Destination
Journey Date
Capacity
Member Functions:
Initialize Members
Input Train Data
Display Data
Write a C++ program to test the Train class that implements the above members.

2 Answers

4 votes

Final answer:

A Train class in C++ with data members for train details and member functions for initializing and displaying train data can be encapsulated in a simple program. Implementation includes member functions InitializeMembers, InputTrainData, and DisplayData.

Step-by-step explanation:

The requested Train class in C++ can be defined with the following members. The Data Members will include train number, train name, destination, journey date, and capacity. The Member Functions will handle the initialization, input, and display of train data. Below is a simple C++ program that demonstrates the implementation of such a class.


class Train {
public:
int trainNumber;
string trainName;
string destination;
string journeyDate;
int capacity;

void InitializeMembers(int number, string name, string dest, string date, int cap) {
trainNumber = number;
trainName = name;
destination = dest;
journeyDate = date;
capacity = cap;
}

void InputTrainData() {
// Code to input train data
}

void DisplayData() {
cout << "Train Number: " << trainNumber << endl;
cout << "Train Name: " << trainName << endl;
cout << "Destination: " << destination << endl;
cout << "Journey Date: " << journeyDate << endl;
cout << "Capacity: " << capacity << endl;
}
};

int main() {
Train myTrain;
myTrain.InitializeMembers(101, "Express", "Gotham", "2023-04-18", 300);
myTrain.DisplayData();
return 0;
}

User Petar Minchev
by
8.1k points
5 votes

Final answer:

You can define a class named Train in C++ with the given members and implement member functions to initialize, input, and display data. Test the Train class by creating an object and calling its member functions in a C++ program.

Step-by-step explanation:

We can define a class named Train in C++ to represent the given members. Here is an example of how the class implementation could look:

class Train {
int TrainNumber;
string TrainName;
string Destination;
string JourneyDate;
int Capacity;
public:
void InitializeMembers();
void InputTrainData();
void DisplayData();
};

void Train::InitializeMembers() {
// Initialize data members
}

void Train::InputTrainData() {
// Get input for train data
}

void Train::DisplayData() {
// Display train data
}

You can then write a C++ program to test the Train class by creating an object of the class and calling its member functions.

User Kaom Te
by
7.5k points