From 5079d09876ac2ca96a55fe90de40af4819088d8b Mon Sep 17 00:00:00 2001 From: pwiatr3 Date: Tue, 19 Oct 2021 14:18:13 +0200 Subject: [PATCH 1/2] working on abstraction --- CMakeLists.txt | 2 ++ Car.cpp | 15 +++++++++++++++ Car.hpp | 16 ++++++++++++++++ ElectricCar.cpp | 8 ++++---- ElectricCar.hpp | 11 ++++++----- ElectricEngine.cpp | 4 ++-- ElectricEngine.hpp | 4 ++-- Engine.cpp | 8 ++++++++ Engine.hpp | 9 +++++++++ PetrolCar.cpp | 8 ++++---- PetrolCar.hpp | 11 ++++++----- PetrolEngine.cpp | 2 +- PetrolEngine.hpp | 4 ++-- 13 files changed, 77 insertions(+), 25 deletions(-) create mode 100644 Car.cpp create mode 100644 Car.hpp create mode 100644 Engine.cpp create mode 100644 Engine.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e6ff1b6..2756f54 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) project(Cars) set(SRC_LIST + Car.cpp + Engine.cpp ElectricCar.cpp ElectricEngine.cpp HybridCar.cpp diff --git a/Car.cpp b/Car.cpp new file mode 100644 index 0000000..ba05910 --- /dev/null +++ b/Car.cpp @@ -0,0 +1,15 @@ +#include "Car.hpp" +#include + +Car::Car() {} +// Car::Car(Engine* engine) +// : engine_(engine) +// { +// std::cout << __FUNCTION__ << std::endl; +// } + +Car::~Car() { std::cout << __FUNCTION__ << std::endl; } +void Car::turnLeft() { std::cout << __FUNCTION__ << std::endl; } +void Car::turnRight() { std::cout << __FUNCTION__ << std::endl; } +void Car::brake() { std::cout << __FUNCTION__ << std::endl; } +void Car::accelerate(int) { std::cout << __FUNCTION__ << std::endl; } \ No newline at end of file diff --git a/Car.hpp b/Car.hpp new file mode 100644 index 0000000..ae16253 --- /dev/null +++ b/Car.hpp @@ -0,0 +1,16 @@ +#pragma once +#include "Engine.hpp" + +class Car +{ +public: + Car(); + //Car(Engine* engine); + ~Car(); + void turnLeft(); + void turnRight(); + void brake(); + void accelerate(int speed); + + //Engine* engine_; +}; \ No newline at end of file diff --git a/ElectricCar.cpp b/ElectricCar.cpp index d5e479e..7413056 100644 --- a/ElectricCar.cpp +++ b/ElectricCar.cpp @@ -8,9 +8,9 @@ ElectricCar::ElectricCar(ElectricEngine* engine) } ElectricCar::~ElectricCar() { std::cout << __FUNCTION__ << std::endl; } -void ElectricCar::turnLeft() { std::cout << __FUNCTION__ << std::endl; } -void ElectricCar::turnRight() { std::cout << __FUNCTION__ << std::endl; } -void ElectricCar::brake() { std::cout << __FUNCTION__ << std::endl; } -void ElectricCar::accelerate(int) { std::cout << __FUNCTION__ << std::endl; } +// void ElectricCar::turnLeft() { std::cout << __FUNCTION__ << std::endl; } +// void ElectricCar::turnRight() { std::cout << __FUNCTION__ << std::endl; } +// void ElectricCar::brake() { std::cout << __FUNCTION__ << std::endl; } +// void ElectricCar::accelerate(int) { std::cout << __FUNCTION__ << std::endl; } void ElectricCar::charge() { std::cout << __FUNCTION__ << std::endl; } diff --git a/ElectricCar.hpp b/ElectricCar.hpp index 261bf83..7d1e768 100644 --- a/ElectricCar.hpp +++ b/ElectricCar.hpp @@ -1,14 +1,15 @@ #include "ElectricEngine.hpp" +#include "Car.hpp" -class ElectricCar +class ElectricCar : public Car { public: ElectricCar(ElectricEngine* engine); ~ElectricCar(); - void turnLeft(); - void turnRight(); - void brake(); - void accelerate(int speed); + // void turnLeft(); + // void turnRight(); + // void brake(); + // void accelerate(int speed); void charge(); ElectricEngine* engine_; diff --git a/ElectricEngine.cpp b/ElectricEngine.cpp index 2d686e8..eea367c 100644 --- a/ElectricEngine.cpp +++ b/ElectricEngine.cpp @@ -2,8 +2,8 @@ #include ElectricEngine::ElectricEngine(int power, int batteryCapacity) - : power_(power) - , batteryCapacity_(batteryCapacity) + : Engine(power), + batteryCapacity_(batteryCapacity) { std::cout << __FUNCTION__ << std::endl; } diff --git a/ElectricEngine.hpp b/ElectricEngine.hpp index 697712d..6d97bd4 100644 --- a/ElectricEngine.hpp +++ b/ElectricEngine.hpp @@ -1,11 +1,11 @@ #pragma once +#include "Engine.hpp" -class ElectricEngine +class ElectricEngine : public Engine { public: ElectricEngine(int power, int batteryCapacity); - int power_; // in HP int batteryCapacity_; // in Ah }; diff --git a/Engine.cpp b/Engine.cpp new file mode 100644 index 0000000..faee31a --- /dev/null +++ b/Engine.cpp @@ -0,0 +1,8 @@ +#include "Engine.hpp" +#include + +Engine::Engine(int power) + : power_(power) +{ + std::cout << __FUNCTION__ << std::endl; +} \ No newline at end of file diff --git a/Engine.hpp b/Engine.hpp new file mode 100644 index 0000000..ab30f1b --- /dev/null +++ b/Engine.hpp @@ -0,0 +1,9 @@ +#pragma once + +class Engine +{ +public: + Engine(int power); + + int power_; // in HP +}; \ No newline at end of file diff --git a/PetrolCar.cpp b/PetrolCar.cpp index 56554b0..ba05bde 100644 --- a/PetrolCar.cpp +++ b/PetrolCar.cpp @@ -8,9 +8,9 @@ PetrolCar::PetrolCar(PetrolEngine* engine) } PetrolCar::~PetrolCar() { std::cout << __FUNCTION__ << std::endl; } -void PetrolCar::turnLeft() { std::cout << __FUNCTION__ << std::endl; } -void PetrolCar::turnRight() { std::cout << __FUNCTION__ << std::endl; } -void PetrolCar::brake() { std::cout << __FUNCTION__ << std::endl; } -void PetrolCar::accelerate(int) { std::cout << __FUNCTION__ << std::endl; } +// void PetrolCar::turnLeft() { std::cout << __FUNCTION__ << std::endl; } +// void PetrolCar::turnRight() { std::cout << __FUNCTION__ << std::endl; } +// void PetrolCar::brake() { std::cout << __FUNCTION__ << std::endl; } +// void PetrolCar::accelerate(int) { std::cout << __FUNCTION__ << std::endl; } void PetrolCar::refuel() { std::cout << __FUNCTION__ << std::endl; } diff --git a/PetrolCar.hpp b/PetrolCar.hpp index 4fb7a88..ea376e9 100644 --- a/PetrolCar.hpp +++ b/PetrolCar.hpp @@ -1,14 +1,15 @@ #include "PetrolEngine.hpp" +#include "Car.hpp" -class PetrolCar +class PetrolCar : public Car { public: PetrolCar(PetrolEngine* engine); ~PetrolCar(); - void turnLeft(); - void turnRight(); - void brake(); - void accelerate(int speed); + // void turnLeft(); + // void turnRight(); + // void brake(); + // void accelerate(int speed); void refuel(); PetrolEngine* engine_; diff --git a/PetrolEngine.cpp b/PetrolEngine.cpp index 2aeceb9..59ff830 100644 --- a/PetrolEngine.cpp +++ b/PetrolEngine.cpp @@ -2,7 +2,7 @@ #include PetrolEngine::PetrolEngine(int power, float capacity, int gears) - : power_(power) + : Engine(power) , capacity_(capacity) , gears_(gears) , currentGear_(0) diff --git a/PetrolEngine.hpp b/PetrolEngine.hpp index b222bb9..40d171f 100644 --- a/PetrolEngine.hpp +++ b/PetrolEngine.hpp @@ -1,12 +1,12 @@ #pragma once +#include "Engine.hpp" -class PetrolEngine +class PetrolEngine : public Engine { public: PetrolEngine(int power, float capacity, int gears); void changeGear(int gear); - int power_; // in HP float capacity_; // in ccm int gears_; int currentGear_; From fb6f214f7715f5579c4b266b2583e61ddcba1806 Mon Sep 17 00:00:00 2001 From: pwiatr3 Date: Mon, 8 Nov 2021 13:09:46 +0100 Subject: [PATCH 2/2] Abstaction, Inheritance, Encapsulation fixed --- CMakeLists.txt | 1 - Car.cpp | 2 +- Car.hpp | 10 +++++----- ElectricCar.cpp | 12 +++++++----- ElectricCar.hpp | 11 +++++------ ElectricEngine.cpp | 2 +- ElectricEngine.hpp | 7 ++++--- Engine.cpp | 8 -------- Engine.hpp | 9 --------- HybridCar.cpp | 14 ++++++-------- HybridCar.hpp | 15 +++++---------- PetrolCar.cpp | 12 +++++++----- PetrolCar.hpp | 12 ++++++------ PetrolEngine.cpp | 3 ++- PetrolEngine.hpp | 6 +++--- main.cpp | 31 +++++++++++++++++++------------ 16 files changed, 71 insertions(+), 84 deletions(-) delete mode 100644 Engine.cpp delete mode 100644 Engine.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2756f54..32e0dda 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,6 @@ project(Cars) set(SRC_LIST Car.cpp - Engine.cpp ElectricCar.cpp ElectricEngine.cpp HybridCar.cpp diff --git a/Car.cpp b/Car.cpp index ba05910..e108c36 100644 --- a/Car.cpp +++ b/Car.cpp @@ -8,7 +8,7 @@ Car::Car() {} // std::cout << __FUNCTION__ << std::endl; // } -Car::~Car() { std::cout << __FUNCTION__ << std::endl; } +//Car::~Car() { std::cout << __FUNCTION__ << std::endl; } void Car::turnLeft() { std::cout << __FUNCTION__ << std::endl; } void Car::turnRight() { std::cout << __FUNCTION__ << std::endl; } void Car::brake() { std::cout << __FUNCTION__ << std::endl; } diff --git a/Car.hpp b/Car.hpp index ae16253..39c8e35 100644 --- a/Car.hpp +++ b/Car.hpp @@ -1,16 +1,16 @@ #pragma once -#include "Engine.hpp" class Car { public: Car(); - //Car(Engine* engine); - ~Car(); + //~Car(); void turnLeft(); void turnRight(); void brake(); void accelerate(int speed); - - //Engine* engine_; + virtual void refill() = 0; + virtual ~Car() {}; + // virtual void charge() = 0; + // virtual void refuel() = 0; }; \ No newline at end of file diff --git a/ElectricCar.cpp b/ElectricCar.cpp index 7413056..a0b9ead 100644 --- a/ElectricCar.cpp +++ b/ElectricCar.cpp @@ -8,9 +8,11 @@ ElectricCar::ElectricCar(ElectricEngine* engine) } ElectricCar::~ElectricCar() { std::cout << __FUNCTION__ << std::endl; } -// void ElectricCar::turnLeft() { std::cout << __FUNCTION__ << std::endl; } -// void ElectricCar::turnRight() { std::cout << __FUNCTION__ << std::endl; } -// void ElectricCar::brake() { std::cout << __FUNCTION__ << std::endl; } -// void ElectricCar::accelerate(int) { std::cout << __FUNCTION__ << std::endl; } void ElectricCar::charge() { std::cout << __FUNCTION__ << std::endl; } - +void ElectricCar::changeEngine(ElectricEngine* engine) { + delete engine_; + engine_ = engine; +} +void ElectricCar::refill() { + charge(); +} diff --git a/ElectricCar.hpp b/ElectricCar.hpp index 7d1e768..ea8ffcf 100644 --- a/ElectricCar.hpp +++ b/ElectricCar.hpp @@ -1,17 +1,16 @@ +#pragma once #include "ElectricEngine.hpp" #include "Car.hpp" -class ElectricCar : public Car +class ElectricCar : virtual public Car { public: ElectricCar(ElectricEngine* engine); ~ElectricCar(); - // void turnLeft(); - // void turnRight(); - // void brake(); - // void accelerate(int speed); + void changeEngine(ElectricEngine* engine); + void refill() override; +private: void charge(); - ElectricEngine* engine_; }; diff --git a/ElectricEngine.cpp b/ElectricEngine.cpp index eea367c..f3d7a64 100644 --- a/ElectricEngine.cpp +++ b/ElectricEngine.cpp @@ -2,7 +2,7 @@ #include ElectricEngine::ElectricEngine(int power, int batteryCapacity) - : Engine(power), + : power_(power), batteryCapacity_(batteryCapacity) { std::cout << __FUNCTION__ << std::endl; diff --git a/ElectricEngine.hpp b/ElectricEngine.hpp index 6d97bd4..49d92c1 100644 --- a/ElectricEngine.hpp +++ b/ElectricEngine.hpp @@ -1,11 +1,12 @@ #pragma once -#include "Engine.hpp" -class ElectricEngine : public Engine + +class ElectricEngine { public: ElectricEngine(int power, int batteryCapacity); - +private: + int power_; // in HP int batteryCapacity_; // in Ah }; diff --git a/Engine.cpp b/Engine.cpp deleted file mode 100644 index faee31a..0000000 --- a/Engine.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "Engine.hpp" -#include - -Engine::Engine(int power) - : power_(power) -{ - std::cout << __FUNCTION__ << std::endl; -} \ No newline at end of file diff --git a/Engine.hpp b/Engine.hpp deleted file mode 100644 index ab30f1b..0000000 --- a/Engine.hpp +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -class Engine -{ -public: - Engine(int power); - - int power_; // in HP -}; \ No newline at end of file diff --git a/HybridCar.cpp b/HybridCar.cpp index 25c0a7c..2aad14a 100644 --- a/HybridCar.cpp +++ b/HybridCar.cpp @@ -2,18 +2,16 @@ #include HybridCar::HybridCar(PetrolEngine* petrolEng, ElectricEngine* electricEng) - : petrolEngine_(petrolEng) - , electricEngine_(electricEng) + : ElectricCar(electricEng) + , PetrolCar(petrolEng) { std::cout << __FUNCTION__ << std::endl; } HybridCar::~HybridCar() { std::cout << __FUNCTION__ << std::endl; } -void HybridCar::turnLeft() { std::cout << __FUNCTION__ << std::endl; } -void HybridCar::turnRight() { std::cout << __FUNCTION__ << std::endl; } -void HybridCar::brake() { std::cout << __FUNCTION__ << std::endl; } -void HybridCar::accelerate(int) { std::cout << __FUNCTION__ << std::endl; } -void HybridCar::charge() { std::cout << __FUNCTION__ << std::endl; } -void HybridCar::refuel() { std::cout << __FUNCTION__ << std::endl; } +void HybridCar::refill() { + ElectricCar::refill(); + PetrolCar::refill(); +} diff --git a/HybridCar.hpp b/HybridCar.hpp index f66faa7..5b398d6 100644 --- a/HybridCar.hpp +++ b/HybridCar.hpp @@ -1,19 +1,14 @@ +#pragma once +#include "ElectricCar.hpp" #include "ElectricEngine.hpp" +#include "PetrolCar.hpp" #include "PetrolEngine.hpp" -class HybridCar +class HybridCar : public ElectricCar, public PetrolCar { public: HybridCar(PetrolEngine* petrolEng, ElectricEngine* electricEng); ~HybridCar(); - void turnLeft(); - void turnRight(); - void brake(); - void accelerate(int speed); - void charge(); - void refuel(); - - PetrolEngine* petrolEngine_; - ElectricEngine* electricEngine_; + void refill() override; }; diff --git a/PetrolCar.cpp b/PetrolCar.cpp index ba05bde..272458a 100644 --- a/PetrolCar.cpp +++ b/PetrolCar.cpp @@ -8,9 +8,11 @@ PetrolCar::PetrolCar(PetrolEngine* engine) } PetrolCar::~PetrolCar() { std::cout << __FUNCTION__ << std::endl; } -// void PetrolCar::turnLeft() { std::cout << __FUNCTION__ << std::endl; } -// void PetrolCar::turnRight() { std::cout << __FUNCTION__ << std::endl; } -// void PetrolCar::brake() { std::cout << __FUNCTION__ << std::endl; } -// void PetrolCar::accelerate(int) { std::cout << __FUNCTION__ << std::endl; } void PetrolCar::refuel() { std::cout << __FUNCTION__ << std::endl; } - +void PetrolCar::changeEngine(PetrolEngine* engine) { + delete engine_; + engine_ = engine; +} +void PetrolCar::refill() { + refuel(); +} \ No newline at end of file diff --git a/PetrolCar.hpp b/PetrolCar.hpp index ea376e9..43dea43 100644 --- a/PetrolCar.hpp +++ b/PetrolCar.hpp @@ -1,17 +1,17 @@ +#pragma once #include "PetrolEngine.hpp" #include "Car.hpp" -class PetrolCar : public Car +class PetrolCar : virtual public Car { public: PetrolCar(PetrolEngine* engine); ~PetrolCar(); - // void turnLeft(); - // void turnRight(); - // void brake(); - // void accelerate(int speed); - void refuel(); + void changeEngine(PetrolEngine* engine); + void refill() override; +private: + void refuel(); PetrolEngine* engine_; }; diff --git a/PetrolEngine.cpp b/PetrolEngine.cpp index 59ff830..b44d619 100644 --- a/PetrolEngine.cpp +++ b/PetrolEngine.cpp @@ -2,7 +2,7 @@ #include PetrolEngine::PetrolEngine(int power, float capacity, int gears) - : Engine(power) + : power_(power) , capacity_(capacity) , gears_(gears) , currentGear_(0) @@ -18,3 +18,4 @@ void PetrolEngine::changeGear(int gear) currentGear_ = gear; std::cout << __FUNCTION__ << std::endl; } + diff --git a/PetrolEngine.hpp b/PetrolEngine.hpp index 40d171f..4922948 100644 --- a/PetrolEngine.hpp +++ b/PetrolEngine.hpp @@ -1,12 +1,12 @@ #pragma once -#include "Engine.hpp" -class PetrolEngine : public Engine +class PetrolEngine { public: PetrolEngine(int power, float capacity, int gears); void changeGear(int gear); - +private: + int power_; // in HP float capacity_; // in ccm int gears_; int currentGear_; diff --git a/main.cpp b/main.cpp index f364af4..b603c92 100644 --- a/main.cpp +++ b/main.cpp @@ -1,28 +1,35 @@ #include "PetrolCar.hpp" #include "ElectricCar.hpp" #include "HybridCar.hpp" +#include "Car.hpp" #include +#include int main() { std::cout << std::endl << "OPEL" << std::endl; PetrolCar opel(new PetrolEngine(120, 1800, 6)); - opel.accelerate(50); - opel.brake(); - opel.accelerate(-900); - opel.refuel(); + + Car* car = &opel; + car->accelerate(50); + car->brake(); + car->accelerate(-900); + car->refill(); + std::cout << std::endl << "NISSAN" << std::endl; ElectricCar nissan(new ElectricEngine(130, 650)); - nissan.charge(); - nissan.accelerate(80); - nissan.engine_ = new ElectricEngine(150, 700); // Changing an engine during driving is not safe - nissan.turnLeft(); + car = &nissan; + car->refill(); + car->accelerate(80); + //nissan.engine_ = new ElectricEngine(150, 700); // Changing an engine during driving is not safe + //car->changeEngine(new ElectricEngine(150, 700)); + car->turnLeft(); std::cout << std::endl << "TOYOTA" << std::endl; HybridCar toyota(new PetrolEngine(80, 1400, 5), new ElectricEngine(100, 540)); - toyota.accelerate(100); - toyota.brake(); - toyota.charge(); - toyota.refuel(); + car = &toyota; + car->accelerate(100); + car->brake(); + car->refill(); }