aboutsummaryrefslogtreecommitdiff
path: root/week03/Exercise5.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'week03/Exercise5.cpp')
-rw-r--r--week03/Exercise5.cpp96
1 files changed, 96 insertions, 0 deletions
diff --git a/week03/Exercise5.cpp b/week03/Exercise5.cpp
new file mode 100644
index 0000000..28721a4
--- /dev/null
+++ b/week03/Exercise5.cpp
@@ -0,0 +1,96 @@
+#include <cstring>
+
+struct Car {
+private:
+ char* model;
+ unsigned int doors;
+ unsigned int seats;
+
+public:
+ Car() {
+ model = nullptr;
+ doors = 0;
+ seats = 0;
+ }
+ Car(const char* model, unsigned int doors, unsigned int seats) {
+ this->model = nullptr;
+ setModel(model);
+ setDoors(doors);
+ setSeats(seats);
+ }
+ ~Car() {
+ delete[] model;
+ }
+
+ const char* getModel() {
+ return model;
+ }
+ void setModel(const char* newModel) {
+ delete[] model;
+ model = new char[strlen(newModel) + 1];
+ strcpy(model, newModel);
+ }
+
+ unsigned int getDoors() {
+ return doors;
+ }
+ void setDoors(unsigned int newDoorsCount) {
+ doors = newDoorsCount;
+ }
+
+ unsigned int getSeats() {
+ return seats;
+ }
+ void setSeats(unsigned int newSeatsCount) {
+ seats = newSeatsCount;
+ }
+
+ void CopyDataTo(Car& destination) {
+ destination.setModel(model);
+ destination.setDoors(doors);
+ destination.setSeats(seats);
+ }
+};
+
+struct TrafficJam {
+private:
+ Car* cars;
+ int size;
+ int lastIndex;
+
+ void resize() {
+ Car* newCars = new Car[size * 2];
+ for (int i = 0; i < size; i++) {
+ cars[i].CopyDataTo(newCars[i]);
+ }
+ delete[] cars;
+ cars = newCars;
+ size *= 2;
+ }
+
+public:
+ TrafficJam() {
+ size = 3;
+ cars = new Car[size];
+ lastIndex = 0;
+ }
+ ~TrafficJam() {
+ delete[] cars;
+ }
+
+ void AddCar(Car& car) {
+ if (lastIndex == size)
+ resize();
+ car.CopyDataTo(cars[lastIndex++]);
+ }
+};
+
+int main() {
+ TrafficJam tj;
+
+ Car c1("Toyota", 4, 5);
+ Car c2("Ferarri", 2, 2);
+
+ tj.AddCar(c1);
+ tj.AddCar(c2);
+}