plz chk my code its giving error at line 45,49.also does it satisfies the requirement
Problem 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()
                     {
                             cout<<"please enter the speed of the train:";
                             cin >>ms;
                             cout << "so the speed of the given train is "<<ms;
                             }
                             train(int);
                             
                                       void setMaxSpeed();
                                       int getMaxSpeed();
                                       };
                                                                          
      void train::setMaxSpeed()
      {
           cout<<"please enter the speed of the train:";
           cin >>ms;
           }
           int train::getMaxSpeed()
           {
               return ms;
               }
      
      
      
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(int);
                  break;
}
}

Recommended Answers

All 6 Replies

You cant call the constructor of the train class like that. Since you already instantiated the t1 object you want to do something like this

t1 = train()

if you wanted to make a new train title myNewTrain you could do

train myNewTrain = train(4)

I would also like to note that while not required it is usually good practice to make class names capitalized so it would be Train not train.

Hope that helps.

also you do not have the definition of train(int) anywhere. your compiler doesn't know what to do because you didn't write the code for it.

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.·

1-> You have not defined the overloaded constructor body.
2-> Are you sure you want to put all those couts in ctor? In my view for the default constructor you should simply initialize 'ms' to 250 and for the overloaded constructor, you should ask the user for the speed as input and pass it to the ctor.
3->Use ctor initialization list to initialize the values, it might not make much difference with an int but it is a good habit to develop.

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. ·

1->the setMaxSpeed method should have an int argument in it's signature which is the value to be set on 'ms'

Display the value through default constructor and overloaded constructor.·
Display setter value through main function

I really don't understand what kind of a requirement is this

train t1;
t1 = train()

Well I would never do that. I'm not sure what the standard says about this, and may be somebody else would confirm that, but I don't think it is a good habit to do this. You would be better off declaring t1 inside the switch case or declare it as a pointer and then initialize it later.

nw chk my code plz. i have worked hard to do this

#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();}#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();
}

nw chk my code plz. i have worked hard to do

Working hard is good but working smart is better !! There were a few more comments in the posts above which you didn't consider. setMaxSpeed should take an int argument like i mentioned above and not have couts to ask for input inside it. The overloaded ctor does not make sense if you ask for input speed inside the ctor. You should do that in main and pass the user given value to the ctor while creating the object. And again I would suggest that you do not declare t1 above and then overwrite it with another object, by calling the ctor manually. If you have to decide at runtime what kind of object to make use operator 'new' and declare t1 as a pointer.

plz chk my code when i want to print the speed through main it also calls constructor how can i avoide that

#include <iostream>
using namespace std;



class train
{
      private:
              
       //------------attributes----------------//       
              int ms;
              
              
              public:
      // ----------default constructor------------//
                        train()
                     { 
                             cout <<"constructor formed\n";
                             ms=250;
                             cout <<"the speed of the train is 250km/h\n";
                             }
                             
      //-----------overloaded constructor---------//
                             train(int a)
                             {
                                       cout<<"constructor formed\n\n";
      ms=a;
                             cout << "so the speed of the given train is "<<ms<<"\n\n";
                             }
                             
          //------------member functions----------//                   
                                       void setMaxSpeed(int maxSpeed);
                                       int getMaxSpeed();
                                       void displaySpeed();
                       ~train()
                       {
                               cout<<"\nconstructor destroyed";
                               }
                                       };
                       
         //-------------setter-------------//                                                                 
      void train::setMaxSpeed(int maxSpeed)
      {
           ms=maxSpeed;
           }
        //--------------getter---------------//   
           int train::getMaxSpeed()
           {
               return ms;
               }
               
      //------------------display--------------//
      void train::displaySpeed()
      {
           cout <<"so the maximum speed of train is :"<<getMaxSpeed();
      }
      
      
main()
{
      
      
      cout<<"please select 1 if u want to display the speed through default constructor\n\n";
      cout<<"please select 2 if u want to display the speed through overloaded constructor\n\n";
      cout <<"please select 3 if u want to display the speed from the main function\n\n";
      cout<<"please enter your choice:";
      int choice;
      cin>>choice;
      switch (choice)
      {
             case 1:         //display through default constructor
             train();
             break;
             
             case 2:          //display through overloaded constructor
                      
                  train(250);
                  break;
                  case 3:      //display through maain function 
                       
                  
                  train t1;
                  t1.displaySpeed();
                  break;
                  }
}
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.