please chk my code. it works well but is its logic correct?
Statement: Suppose we have a class named as Train it has two constructors, two methods, and a single attribute each of which described as follows:·
One attribute:maxSpeed – an int value representing the maximum speed of the Train object.
Note: Train speed of modern Trains may be something like 250 km per hour, so a value you could use for testing purposes is 250.· Two constructors:
A default constructor: A constructor that initializes the attribute ms where ms is the maxSpeed.
An overloaded constructor: A constructor that takes ms as an argument, where ms is the maxSpeed.·
Two methods:
() – return the maxSpeed value associated with the Train object.
setMaxSpeed(ms) – change the maxSpeed value associated with the Train object, where ms is the new value. ·
Display the value through default constructor and overloaded constructor.·
Display setter value through main function.

#include <iostream>
using namespace std;



class train
{
      private:
              int ms;
              public:
                     train()
                     {
                             ms=250;
                             cout <<"the speed of the train is 250km/h";
                             }
                             train(int)
                             {
                                        cout<<"please enter the speed of the train:";
                             cin >>ms;
                             cout << "so the speed of the given train is "<<getMaxSpeed();
                             }
                                       void setMaxSpeed();
                                       int getMaxSpeed();
                                       void displaySpeed();
                                       };
                                                                          
      void train::setMaxSpeed()
      {
           cout<<"please enter the speed of the train:";
           cin >>ms;
           }
           int train::getMaxSpeed()
           {
               return ms;
               }
      void train::displaySpeed()
      {
           cout <<"so the speed is :"<<getMaxSpeed();
      }
      
      
main()
{
      int choice;
      train t1;
      cout<<"please select 1 if u want to display the speed through default constructor";
      cout<<"please selext 2 if u want to display the speed through overloaded constructor";
      cin>>choice;
      switch (choice)
      {
             case 1:
             t1=train();
             break;
             
             case 2:
                  t1=train(250);
                  break;
}
t1.displaySpeed();
}
Member Avatar for iamthwee

We're not here to fix your homework so you can turn it in and get an A . . .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.