Question

How would I test this code thoroughly in a main.cpp file. Locomotive is an abstract base...

How would I test this code thoroughly in a main.cpp file. Locomotive is an abstract base class. steam and dieselElectric are the derived classes.

#ifndef COMMODITY_H
#define COMMODITY_H
#include <iostream>
#include <string>
using namespace std;
class commodity
{
private:
   string name;
   int quantity;
public:
   commodity(commodity *c);
   commodity(string name,int quantity);
   ~commodity();
   string getName() const;
   int getQuantity() const;
};
#endif

#include "commodity.h"

commodity::commodity(commodity *c)
{       this->name=c->getName();
   this->quantity=c->getQuantity();
}

commodity::commodity(string name,int quantity)
{       this->name=name;
   this->quantity=quantity;
}

commodity::~commodity()
{cout<<getName()<<endl;}

string commodity::getName() const
{return name;}

int commodity::getQuantity() const
{return quantity;}


#ifndef REQUEST_H
#define REQUEST_H
#include "commodity.h"
class request
{
private:
      string id;
      double targetDistance;
      commodity** items;
      int numItems;
public:
      request(request *r);
      request(string id,double targetDistance,int numItems);
      ~request();
      int addItem(commodity *c);
      int addItem(string name,int quantity);
      string removeItem(int i);
      double getDistance();
      string getID();
      int getNumItems();
      commodity* getItem(int i);
};
#endif

#include"request.h"

request::request(request *r)
{   this->id=r->getID();
    this->targetDistance=r->getDistance();
    this->numItems=r->getNumItems();
    items=new commodity*[numItems];
    for(int i=0;i<numItems;i++) {this->items[i]=r->getItem(i);}
}

request::request(string id,double targetDistance,int numItems)
{   this->id=id;
    this->targetDistance=targetDistance;
    this->numItems=numItems;
    items=new commodity*[numItems];
}

request::~request()
{delete [] items;}

int request::addItem(commodity *c)
{   int i;
    for(i=0;i<this->numItems;i++)
    {   if(this->items[i]==NULL)
        {   this->items[i]=c;
            break;}
    }
    if(i==this->numItems) {return -1;}
    else {return i;}
}

int request::addItem(string name,int quantity)
{   commodity *c=new commodity(name,quantity);
    return addItem(c);
}

string request::removeItem(int i)
{   string notFound="Commodity Not Found";
    if(i<0 || i>=this->numItems) return notFound;
    commodity *c = this->items[i];
    this->items[i]=NULL;
    string name=c->getName();
    return name;
}

double request::getDistance()
{return targetDistance;}

string request::getID()
{return id;}

int request::getNumItems()
{return numItems;}

commodity* request::getItem(int i)
{if(i<0 || i>this->numItems) {return NULL;}
    return items[i];
}


#ifndef LOCOMOTIVE_H
#define LOCOMOTIVE_H
#include <iostream>
#include <string>
#include "commodity.h"
using namespace std;
class locomotive
{
private:
   string name;
   double topSpeed;
   int maxCars;
   int tonLimit;
   commodity ** cars;
public:
   locomotive();
   locomotive(string name, double topSpeed, int maxCars, int tonLimit);
   string getName();
   void setName(string s);
   const double getTopSpeed();
   const int getMaxCars();
   const int getTonLimit();
   void setTopSpeed(double s);
   void setMaxCars(int s);
   void setTonLimit(int s);
   double getCurrentTopSpeed();
   void printManifest();
   int addCar(commodity *c);
   int removeCar(string s);
   void unassignCars();
   commodity * getFirstCar();
   int getCurrentTons();
   int getCurrentCars();
   virtual ~locomotive();
   virtual string generateID()= 0;
   virtual double calculateRange();
   virtual string getType();
};

#include "locomotive.h"
locomotive::locomotive()
{}

locomotive::locomotive(string name, double topSpeed, int maxCars, int tonLimit)
{ this->name = name;
   this->topSpeed = topSpeed;
   this->maxCars = maxCars;
   this->tonLimit = tonLimit;
   this->cars = new commodity *[maxCars];
   for (int i = 0; i < maxCars; i++) {this->cars[i] = NULL;}
}

locomotive::~locomotive()
{delete [] cars;}

string locomotive::getName()
{return this->name;}

void locomotive::setName(string s)
{name = s;}

double locomotive::getTopSpeed() const
{return topSpeed;}

int locomotive::getMaxCars() const
{return maxCars;}

int locomotive::getTonLimit() const
{return tonLimit;}

void locomotive::setTonLimit(int s)
{tonLimit = s;}

void locomotive::setTopSpeed(double s)
{topSpeed = s;}

void locomotive::setMaxCars(int s)
{ this->maxCars = s;
   if (this->cars == NULL)
   {   this->cars = new commodity *[maxCars];
       for (int i = 0; i < maxCars; i++) {this->cars[i] = NULL;}
   }
}

int locomotive::getCurrentTons()
{ int total_ton=0;
   for (int i = 0; i < this->maxCars; i++)
   {if (this->cars[i] != NULL) {total_ton += this->cars[i]->getQuantity();}}
   return total_ton;
}

int locomotive::getCurrentCars()
{ int total_car = 0;
   for (int i = 0; i < this->maxCars; i++)
   {   if (this->cars[i] != NULL)
       {total_car ++;}
   }
   return total_car;
}

commodity *locomotive :: getFirstCar()
{ for (int i = 0; i < this->maxCars; i++)
   {   if (this->cars[i] != NULL)
       {return this->cars[i];}
   }
   return NULL;
}
double locomotive::calculateRange()
{return 0.0;}
int locomotive::addCar(commodity *c)
{ int i;
   if (getCurrentTons() + c->getQuantity() > getTonLimit()) {return -1;}
       for (i = 0; i < this->maxCars; i++)
       {   if (this->cars[i] == NULL)
           {   this->cars[i] = c;
               break;
           }
       }
       if (i == this->maxCars) {return -1;}
       return i;
}

int locomotive::removeCar(string s)
{ int flag = -1;
   for (int i = 0; i < this->maxCars; i++)
   {   if (this->cars[i] != NULL)
       {   if (this->cars[i]->getName() == s)
           {   commodity * tmp = this->cars[i];
               this->cars[i] = NULL;
               flag = i;
               break;
           }
       }
   }
   return flag;
}

void locomotive::unassignCars()
{
   for (int i = 0; i < this->maxCars; i++)
   {
       if (this->cars[i] != NULL)
       {       commodity * tmp = this->cars[i];
               this->cars[i] = NULL;
}}}

void locomotive:: printManifest()
{ int count=1;
   cout << "Train: " << this->name << endl;
   for (int i = 0; i < this->maxCars; i++)
   {   if (this->cars[i] != NULL)
       {   cout << "Car " << count << ": " << this->cars[i]->getName() << endl;
       count++;
       }}
   cout << "Total Tons: " << getCurrentTons() << endl;
}

double locomotive::getCurrentTopSpeed()
{ double current_speed = (getTopSpeed() - (1.7*getCurrentCars() - (1.01*(getCurrentTons() / 10.0))));
   if (getCurrentTons() > getTonLimit() || current_speed < 0) {return 0.0;}
   return current_speed;
}

string locomotive::getType()
{return "";}


#ifndef DIESELELECTRIC_H
#define DIESELELECTRIC_H
#include "locomotive.h"
#include <sstream>
using namespace std;
class dieselElectric : public locomotive
{
private:
    int fuelSupply;
public:
    int getSupply();
    void setSupply( int s);
    dieselElectric(int f);
    ~dieselElectric();
    string generateID();
    double calculateRange();
    string getType();
};
#endif

#include "dieselElectric.h"
#include<iostream>
#include<string>

int dieselElectric::getSupply()
{return fuelSupply;}

void dieselElectric::setSupply(int s)
{fuelSupply = s;}

dieselElectric::dieselElectric(int f)
{fuelSupply = f;}

dieselElectric::~dieselElectric()
{ cout<<"Diesel Electric Train: "<<this->getName()<<endl;}

string dieselElectric::generateID()
{   string id = getType()+"-";
    commodity *first = getFirstCar();
    if(first->getName().length() <= 3) {id += first->getName();}
    else {id += first->getName().substr(0,3);}
    stringstream ss;
    ss << getCurrentTons();
    string tonnage;
    ss >> tonnage;
    id += "-"+tonnage;
    return id;
}

double dieselElectric::calculateRange()
{   double range = 0.0;
    range = this->fuelSupply*3.5;
    range = range - (5*(this->getCurrentTons()/this->getCurrentCars()));
    if(range < 0) {range = 0;}
    return range;
}

string dieselElectric::getType()
{return "dieselElectric";}


#ifndef STEAM_H
#define STEAM_H
#include "locomotive.h"
#include <sstream>
using namespace std;
class steam : public locomotive
{
private:
   int coal;
   int water;
public:
   int getWater();
   void setWater(int s);
   int getCoal();
   void setCoal(int s);
   steam(int c, int w);
   ~steam();
   string generateID();
   double calculateRange();
   string getType();
};
#endif

#include "steam.h"

int steam::getWater()
{return water;}

void steam::setWater(int s)
{water = s;}

int steam::getCoal()
{return coal;}

void steam::setCoal(int s)
{coal = s;}

steam::steam(int c, int w)
{ coal = c;
water = w;
}

steam::~steam()
{cout<<"Steam Train: "<<this->getName()<<endl;}

string steam::generateID()
{   string id = getType()+"-";
    commodity *first = getFirstCar();
    if(first->getName().length() <= 3) {id += first->getName();}
    else {id += first->getName().substr(0,3);}
    stringstream ss;
    ss << getCurrentTons();
    string tonnage;
    ss >> tonnage;
    id += "-"+tonnage;
    return id;
}

double steam::calculateRange()
{   int baseRange ;
    if((coal%water) == 0)
    {
        if((coal/water) == 2) {baseRange = coal;}
        else {baseRange = 0;}
    }
    else {baseRange = 0;}
    double range = ((double)baseRange) - 1.2*getCurrentCars();
    if(range < 0)
        {range = 0;}
    return range;
}

string steam::getType()
{return "steam";}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In main.cpp

Create some objects of Commudity.

Create DieselElectric class objects and Steam class objects, and point the objects via Locomotive object pointer as shown below.

Locomotive* l1 = new electricDiesel();

Locomotive* l2 = new steam();

Here, locomotive will act as an interface.

Call the methods and check the functionality

Add a comment
Know the answer?
Add Answer to:
How would I test this code thoroughly in a main.cpp file. Locomotive is an abstract base...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • Java Programming Question

    After pillaging for a few weeks with our new cargo bay upgrade, we decide to branch out into a new sector of space to explore and hopefully find new targets. We travel to the next star system over, another low-security sector. After exploring the new star system for a few hours, we are hailed by a strange vessel. He sends us a message stating that he is a traveling merchant looking to purchase goods, and asks us if we would...

  • Please help fix my code C++, I get 2 errors when running. The code should be...

    Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...

  • I need help with this assignment, can someone HELP ? This is the assignment: Online shopping...

    I need help with this assignment, can someone HELP ? This is the assignment: Online shopping cart (continued) (C++) This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class per the following specifications: Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt) Public member functions SetDescription() mutator & GetDescription() accessor (2 pts) PrintItemCost() - Outputs the item name followed by...

  • C++ 15.15 Lab: Abstract base class Base Class The base Character class is located Character.h and...

    C++ 15.15 Lab: Abstract base class Base Class The base Character class is located Character.h and cannot be changed. enum HeroType {WARRIOR, ELF, WIZARD}; const double MAX_HEALTH = 100.0; class Character { protected:    HeroType type;    string name;    double health;    double attackStrength; public:     Character(HeroType type, const string &name, double health, double attackStrength);     HeroType getType() const;     const string & getName() const;     /* Returns the whole number of the health value (static_cast to int). */     int getHealth() const;     void setHealth(double h);     /* Reduces...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • Hello, I have written a code that I now need to make changes so that arrays...

    Hello, I have written a code that I now need to make changes so that arrays are transferred to the functions as pointer variable. Below is my code I already have. #include<iostream> #include<string> #include<iomanip> using namespace std; //functions prototypes void getData(string id[], int correct[], int NUM_STUDENT); void calculate(int correct[], int incorrect[], int score[], int NUM_STUDENT); double average(int score[], int NUM_STUDENT); void Display(string id[], int correct[], int incorrect[], int score[], double average, int NUM_STUDENT); int findHigh(string id[], int score[], int NUM_STUDENT);...

  • This is a simple C++ class using inheritance, I have trouble getting the program to work....

    This is a simple C++ class using inheritance, I have trouble getting the program to work. I would also like to add ENUM function to the class TeachingAssistant(Derived class) which is a subclass of Student(Derived Class) which is also a subclass of CourseMember(Base Class). The TeachingAssistant class uses an enum (a user-defined data type) to keep track of the specific role the TA has: enum ta_role {LAB_ASSISTANT, LECTURE_ASSISTANT, BOTH}; You may assume for initialization purposes that the default role is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT