I have to use inheriantce and polymorphism to calculate the force required to pull (at a constant velocity) and the weight of material that can be carried by a train consisting of many different types of cars.
The weight ( W ) and rolling friction coefficient (f) can be used to compute the force (F = fW).
Consider flat bed (B), box (X), and tank (T) cars.
The volume for each type of car is B (10m³), X (20m³), T (15m³).
Read the type of car and density (in kN/m³) of material being carried.

A sample data file (train.dat) is .

B 5.3 X 6.7 X 5.6 X 7.3 T 9.8 T 8.4 T 10.3 B 3.2 B 4.2

I'm sooooo confused can someone please help me?

Yes. What are you confused about?

Note that you're not using inheritance or polymorphism to calculate anything at all. Instead you're using them to organise your program. This distinction should be made clear, as you can calculate it without using either of the concepts in the exact same way.

Edited 2 Years Ago by Hiroshe

This is what I have so far: ( I keep getting error message)

#include <iostream>
using namespace std;
class train
`{`
public: 
`   train();`
class bed
{
public:
    bed();
    static const bed(B);
    void display();
class box
{
public:
    box();
    static const box(X);
    void display();
class tank
{
public:
    tank(); 
    static const tank(T);
void display(); 
 int train_car;

int main()
{
    cout << "B    \n" << endl;
    cout << "X      \n" << endl;
    cout <<"T        \n" << endl;
    {
    system("PAUSE");
    return 0;
    }

1>------ Build started: Project: Week 9, Configuration: Debug Win32 ------
1> Week 9.cpp
1>c:\users\XXXX\documents\visual studio 2010\projects\week 9\week 9\week 9.cpp(36): fatal error C1075: end of file found before the left brace '{' at 'c:\users\XXXX\documents\visual studio 2010\projects\week 9\week 9\week 9.cpp(28)' was matched
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Your code is littered with odd symbols. Like the one at the satrt of line 4. No idea what they're doing there. Get rid of them.

In your code, every time you have one of these { you're going to need one of these }. You're missing lots. At the end of class definitions, at the end of functions.

What are those static consts for?

Ok I rewrote the code it works great, but I don't know how to put the train.txt file with in it to get the data off it. Any suggestions on how I add: A sample data file (train.dat) is:

B 5.3 X 6.7 X 5.6 X 7.3 T 9.8 T 8.4 T 10.3 B 3.2 B 4.2

#include <iostream>
using namespace std;
class train{
public:
virtual void show(){cout << "train" << endl;}
};
class bed : public train{
void show(){cout << "B" << endl;}
};
class box : public train{
void show(){cout << "X" << endl;}
};
class tank : public train{
void show(){cout << "T" << endl;}
};
int main(void){
train* arr[4];
arr[0] = new train; arr[1] = new bed; arr[2] = new box; arr[3] = new tank;
for(int i = 0; i < 4; i++)
arr[i]->show();
system("PAUSE");
return 0;
}

1eebace83dc6d661d2fa37767bf24c341eebace83dc6d661d2fa37767bf24c34

This article has been dead for over six months. Start a new discussion instead.