Problem Statement: Suppose we have a class named as Train it has two constructor
ID: 3616252 • Letter: P
Question
Problem Statement:
Suppose we have a class named as Train it has two constructors,two methods, and a single attribute each of which described asfollows:
maxSpeed – an int value representing themaximum speed of the Train object.
Note: Train speed of modern Trains may be something like 250 kmper hour, so a value you could use for testing purposes is 250.
A default constructor:
A constructor that initializes theattribute ms where ms is the maxSpeed.
An overloaded constructor:
A constructor that takes ms as anargument, where ms is the maxSpeed.
getMaxSpeed() – return the maxSpeed valueassociated withthe Train object.
setMaxSpeed(ms) – change the maxSpeedvalue associated with the Train object, where ms is the newvalue.
Explanation / Answer
int main()
{Train train,train2(250);
int max;
cout<<"Enter a new train speed for train: ";
cin>>max;
train.setMaxSpeed(max);
cout<<"Enter a new train speed for train2: ";
cin>>max;
train2.setMaxSpeed(max);
cout<<"new maxSpeed train:"<<train.getMaxSpeed()<<endl;
cout<<"new maxSpeed train2:"<<train2.getMaxSpeed()<<endl;
system("pause");
return 0;
}
Train::Train()
{
maxSpeed=100;
cout<<"default: maxSpeed: "<<maxSpeed<<endl;
}
Train::Train(int max)
{
maxSpeed=max;
cout<<"overload: maxSpeed: "<<maxSpeed<<endl;
}
int Train::getMaxSpeed()
{
return maxSpeed;
}
void Train::setMaxSpeed(int ms)
{
maxSpeed=ms;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.