Skip to content

Commit 03106e9

Browse files
committed
-> Declared the trivial destructors for all classes
-> Fully formatted toString methods for all classes -> Added test cases in the main method for testing the implementations -> Added validation on value for engine sizes in the Car class
1 parent f9cd890 commit 03106e9

File tree

9 files changed

+98
-15
lines changed

9 files changed

+98
-15
lines changed

Bicycle.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
#include "Bicycle.h"
66

77

8-
Bicycle::Bicycle(int gearCount) {
8+
Bicycle::Bicycle(string brand, string model, int gearCount) {
9+
setBrand(brand);
10+
setModel(model);
911
setGearCount(gearCount);
1012
}
1113

14+
Bicycle::~Bicycle() = default;
15+
1216
int Bicycle::getGearCount() {
1317
return myGearCount;
1418
}
@@ -24,5 +28,7 @@ double Bicycle::mileageEstimate(double time) {
2428
}
2529

2630
string Bicycle::toString() {
27-
return getBrand() + " " + getModel();
31+
string s = "-> Bicycle\n\t";
32+
return "-> Bicycle\n" + Vehicle::toString() + "\n\tGears: " +
33+
to_string(myGearCount);
2834
}

Bicycle.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ class Bicycle : public Vehicle {
1313
int myGearCount;
1414

1515
public:
16-
explicit Bicycle(int gearCount = 1);
16+
explicit Bicycle(string brand, string model, int gearCount = 1);
17+
18+
virtual ~Bicycle();
1719
int getGearCount();
1820
void setGearCount(int gearCount);
1921
virtual double mileageEstimate(double time);

Car.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,33 @@
44

55
#include "Car.h"
66

7-
87
Car::Car() {
98
myEngineSize = "unknown";
9+
setBrand("Custom");
10+
setModel("VTx");
1011
}
1112

12-
Car::Car(string engineSize) {
13+
Car::Car(string brand, string model, string fuelType, string engineSize) {
14+
setBrand(brand);
15+
setModel(model);
16+
setFuelType(fuelType);
1317
setEngineSize(engineSize);
1418
}
1519

20+
Car::~Car() = default;
21+
1622
string Car::getEngineSize() {
1723
return myEngineSize;
1824
}
1925

2026
void Car::setEngineSize(string engineSize) {
21-
myEngineSize = engineSize;
27+
if (engineSize == "unknown" || engineSize == "small" ||
28+
engineSize == "medium" || engineSize == "grande") {
29+
myEngineSize = engineSize;
30+
} else {
31+
myEngineSize = "unknown";
32+
}
33+
2234
}
2335

2436
double Car::mileageEstimate(double time) {
@@ -27,4 +39,9 @@ double Car::mileageEstimate(double time) {
2739
mileage += mileage * 0.05;
2840
}
2941
return mileage;
42+
}
43+
44+
string Car::toString() {
45+
return "-> Car\n" + PoweredVehicle::toString() + "\n\tEngine Size: " +
46+
getEngineSize();
3047
}

Car.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,18 @@ class Car : public PoweredVehicle {
1111

1212
private:
1313
string myEngineSize;
14+
1415
public:
1516
Car();
1617

17-
explicit Car(string engineSize);
18+
explicit Car(string brand, string model, string fuelType,
19+
string engineSize);
1820

21+
virtual ~Car();
1922
string getEngineSize();
20-
2123
void setEngineSize(string engineSize);
22-
2324
virtual double mileageEstimate(double time);
24-
2525
virtual string toString();
26-
2726
};
2827

2928

PoweredVehicle.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
//
22
// Created by Esteban Parra on 9/5/19.
33
//
4+
#include "PoweredVehicle.h"
45

6+
7+
PoweredVehicle::PoweredVehicle() {
8+
setFuelType("unknown");
9+
}
10+
11+
PoweredVehicle::~PoweredVehicle() = default;
12+
13+
string PoweredVehicle::getFuelType() {
14+
return fuelType;
15+
}
16+
17+
void PoweredVehicle::setFuelType(string fuel) {
18+
fuelType = fuel;
19+
}
20+
21+
string PoweredVehicle::toString() {
22+
return Vehicle::toString() + "\n\tFuelType: " + getFuelType();
23+
}

PoweredVehicle.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ protected :
1414
public:
1515
PoweredVehicle();
1616

17+
virtual ~PoweredVehicle();
1718
virtual string toString();
18-
1919
virtual double mileageEstimate(double time) = 0;
2020

21+
string getFuelType();
22+
23+
void setFuelType(string fuel);
2124
};
2225

2326

Vehicle.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Vehicle::Vehicle(string brand, string model) {
99
setModel(model);
1010
}
1111

12+
Vehicle::~Vehicle() = default;
13+
1214
string Vehicle::getBrand() {
1315
return myBrand;
1416
}
@@ -26,5 +28,5 @@ void Vehicle::setModel(string model) {
2628
}
2729

2830
string Vehicle::toString() {
29-
return getBrand() + " " + getModel();
31+
return "\tBrand= " + getBrand() + "\n\tModel= " + getModel();
3032
}

Vehicle.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ private :
1616

1717
public:
1818
explicit Vehicle(string brand = "unknown", string model = "unknown");
19+
20+
virtual ~Vehicle();
1921
string getBrand();
2022
void setBrand(string brand);
2123
string getModel();
2224
void setModel(string model);
23-
2425
virtual string toString();
2526
virtual double mileageEstimate(
2627
double time) = 0; // Method that computes how many miles can be traversed by the vehicle in the given amount of time. The time parameter is given in minutes

main.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,40 @@
11
#include <iostream>
2+
#include "Car.h"
3+
#include "Bicycle.h"
4+
5+
void printVehiclesRoster(Vehicle **vehicles, int size);
26

37
int main() {
4-
std::cout << "Hello, World!" << std::endl;
8+
std::cout << "Driving simulator" << std::endl;
9+
int size = 6;
10+
int capacity = 10;
11+
Vehicle **vehiclesArray = new Vehicle *[capacity];
12+
13+
vehiclesArray[0] = new Car();
14+
vehiclesArray[1] = new Bicycle("eTAP", "P5X");
15+
vehiclesArray[2] = new Bicycle("R&A", "Dogma F8", 5);
16+
vehiclesArray[3] = new Car("Tesla", "T2", "electricity", "large");
17+
vehiclesArray[4] = new Bicycle("Mizuno", "Wave", 10);
18+
vehiclesArray[5] = new Car("BMW", "X5", "diesel", "grande");
19+
20+
printVehiclesRoster(vehiclesArray, size);
21+
22+
if (vehiclesArray != 0) { // If it is not a null pointer
23+
// do not use nullptr. It is not supported on linprog
24+
for (int i = 0; i < size; i++) {
25+
delete vehiclesArray[i];
26+
}
27+
delete[] vehiclesArray;
28+
}
529
return 0;
30+
}
31+
32+
void printVehiclesRoster(Vehicle **vehicles, int size) {
33+
double simulatedDistance = 130;
34+
for (int i = 0; i < size; i++) {
35+
cout << i << " " << vehicles[i]->toString() << endl;
36+
cout << "\tWould travel: "
37+
<< vehicles[i]->mileageEstimate(simulatedDistance) << " miles in "
38+
<< simulatedDistance << " seconds" << endl;
39+
}
640
}

0 commit comments

Comments
 (0)