Having truoble with derived classed

I've been trying to understand this project for a while now, but keep running into road blocks. Now I'm being told that all of my derived classes are actually abstract classes. We're supposed to create a base class of railroad_car and derived classes tank_car, box_car, and Refrigerator_car. input will be read from a file.I know its not perfect but here's what I've got

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#ifndef RailroadCar_hpp
#define RailroadCar_hpp
#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <cmath>



class Railroad_Car {
private:
    double length;
    
public:
    //Constructor
    Railroad_Car(double = 0);
    //Calculate the volume of a boxcar
    virtual size_t volume() = 0;
    
    
    //Setter and getter for length of the boxcar
    virtual void setLength(size_t l) = 0;
    virtual size_t getLength() const = 0;
};

class Tank_Car : public Railroad_Car {
private:
    double radius;
    
public:
    // Default constructor
    Tank_Car(double = 0);
    size_t volume(double);
};


class Box_Car : public Railroad_Car {
protected:
    double width, height;
    
public:
    //Default constructor
    Box_Car(double = 0, double = 0, double = 0);
    size_t volume(double, double);
};


// Refrigerator car is derrived from the box car class
class Refigerator_Car : public Box_Car {
private:
    int temperature;
    
public:
    Refigerator_Car(double = 0, double = 0, int = 0);
};

#endif /* RailroadCar_hpp */




//.cpp

#include "RailroadCar.hpp"


// Implimentation of the volume member function

Railroad_Car::Railroad_Car(double l) {
    length = l;
}

void Railroad_Car::setLength(size_t l) {
    length = l;
}

size_t Railroad_Car::getLength() const{
    return length;
}

// Default constructor for Tank_Car
Tank_Car::Tank_Car(double r) {
    radius = r;
    
    return;
}
size_t Tank_Car::volume(double r) {
    size_t volume;
    const double PI = (atan(1) * 4);
    volume = (r * r * PI * getLength());
    
    return volume;
}
//Default constructor for Box_Car
Box_Car::Box_Car(double l, double h, double w) : Railroad_Car(){
    height = h;
    width = w;
    
    return;
}
size_t Box_Car::volume(double w, double h) {
    size_t volume;
    volume = getLength() * w * h;
    
    return volume;
}

// Default constructor for the Refrigerator car class
Refigerator_Car::Refigerator_Car(double h, double w, int t) {
    height = h;
    width = w;
    temperature = t;
}



//Main

#include <iostream>
#include <fstream>
#include <string>
#include "RailroadCar.hpp"
#include "RailroadCar.cpp"

using namespace std;




int main(){
    
    ifstream fin("/Users/MoKK/School/CPT 182/LAB 5/input.txt");         // Create file variables and
    ofstream fout("/Users/MoKK/School/CPT 182/LAB 5/output.txt");       // test to make sure no errors
    // in loading file
    if(fin.fail()) {
        cout << "Error in opening files." << endl;
        system("pause");
    }
    
    
    
    string type;   // Declare variable to hold the type of car being considered

    while (fin >> type) {
        vector <double> dimensions;     //Vector to store the dimensions of each car
        
        Railroad_Car* car1 = nullptr;

        if (type == "Tank") {
            fin >> dimensions[0] >> dimensions[1];
            //Railroad_Car* Tank1 = nullptr;
            Tank_Car Tank1;         //Declare new tank car object
            fout << Tank1.volume();
        }
        
        
        else if (type == "Box") {
            fin >> dimensions[0] >> dimensions[1] >> dimensions[2];
            //Railroad_Car* Box1 = nullptr;
            Box_Car Box1;//(dimensions[0], dimensions[1], dimensions[2]);
            fout << Box1.volume();
        }
        
        
        else {              //The only other option is a refigerator car
            fin >> dimensions[0] >> dimensions[1] >> dimensions[2] >> dimensions[3];
            //Railroad_Car* Ref1 = nullptr;
            Refigerator_Car Ref1;
            fout << Ref1.volume();
        }
    }
    
    
    
    fin.close();
    fout.close();
    

    
    return 0;
}



I know my logic is probably off. I haven't been able to compile anything to check... I'm sure once I get the virtual issue resolved I'm going to have that damn clang linker error again... My textbook is barely any help and my instructor is almost impossible to meet with outside of lectures
Last edited on
Complain to the school board (department chair) if your instructor is not making time to meet with students. Most (quality) universities make it part of the professor's contract to maintain office hours sufficient to be available to all students.

[edit]
Make sure you make good effort to schedule time to meet with your professor first. No sense in setting bridges on fire if you can avoid it.

Also, you should have a help center where someone familiar with coding can help.
[/edit]

To make the derived classes non-abstract, you must implement all the base class's virtual abstract functions, and you must declare them in the class definition. See lines 36..37:

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Railroad_Car {
private:
    double length;
    
public:
    //Constructor
    Railroad_Car(double = 0);
    //Calculate the volume of a boxcar
    virtual size_t volume() = 0;
    

    //Setter and getter for length of the boxcar
    virtual void setLength(size_t l) = 0;
    virtual size_t getLength() const = 0;
};

class Tank_Car : public Railroad_Car {
private:
    double radius;
    
public:
    // Default constructor
    Tank_Car(double = 0);
    size_t volume(double);

    virtual void setLength(size_t l);
    virtual size_t getLength() const;
};
1
2
3
4
5
6
7
void Railroad_Car::setLength(size_t l) {
    length = l;
}

size_t Railroad_Car::getLength() const{
    return length;
}

I have not analyzed your code any further than that.

Hope this helps.
Last edited on
Topic archived. No new replies allowed.