diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-12-14 18:32:14 +0100 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-12-14 18:32:14 +0100 |
commit | 083b0f690ce69371473433cce7f238360188238d (patch) | |
tree | 3a1c12d0589a7ffb8bdb89ad5b3cffa270c73e0d | |
parent | 1ed8db0234f9a273e07b7350e53ad230c5b62ce6 (diff) |
week 6 deel 1 klaar
-rw-r--r-- | oop2w6/Capacitor.cpp | 15 | ||||
-rw-r--r-- | oop2w6/Capacitor.h | 15 | ||||
-rw-r--r-- | oop2w6/Circuit.cpp | 6 | ||||
-rw-r--r-- | oop2w6/Circuit.h | 18 | ||||
-rw-r--r-- | oop2w6/IComponent.cpp | 0 | ||||
-rw-r--r-- | oop2w6/IComponent.h | 9 | ||||
-rw-r--r-- | oop2w6/Inductor.cpp | 15 | ||||
-rw-r--r-- | oop2w6/Inductor.h | 15 | ||||
-rw-r--r-- | oop2w6/Resistor.cpp | 15 | ||||
-rw-r--r-- | oop2w6/Resistor.h | 15 | ||||
-rw-r--r-- | oop2w6/main.cpp | 40 | ||||
l--------- | oop2w6/makefile | 1 |
12 files changed, 164 insertions, 0 deletions
diff --git a/oop2w6/Capacitor.cpp b/oop2w6/Capacitor.cpp new file mode 100644 index 0000000..ccb70bf --- /dev/null +++ b/oop2w6/Capacitor.cpp @@ -0,0 +1,15 @@ +#include "Capacitor.h" + +Capacitor::Capacitor(double capacity, bool isElectrolitic) { + this->capacity = capacity; + this->bIsElectrolitic = isElectrolitic; +} + +double Capacitor::getValue() { + return capacity; +} + +bool Capacitor::isElectrolitic() { + return bIsElectrolitic; +} + diff --git a/oop2w6/Capacitor.h b/oop2w6/Capacitor.h new file mode 100644 index 0000000..de1e830 --- /dev/null +++ b/oop2w6/Capacitor.h @@ -0,0 +1,15 @@ +#pragma once + +#include "IComponent.h" + +class Capacitor : public IComponent { +private: + double capacity; + bool bIsElectrolitic; // electrolytic but misspelled + +public: + Capacitor(double capacity, bool isElectrolitic); + virtual double getValue(); + virtual bool isElectrolitic(); +}; + diff --git a/oop2w6/Circuit.cpp b/oop2w6/Circuit.cpp new file mode 100644 index 0000000..3df84c8 --- /dev/null +++ b/oop2w6/Circuit.cpp @@ -0,0 +1,6 @@ +#include "Circuit.h" + +void Circuit::add(IComponent* pComponent) { + components[size++] = pComponent; +} + diff --git a/oop2w6/Circuit.h b/oop2w6/Circuit.h new file mode 100644 index 0000000..cc406d0 --- /dev/null +++ b/oop2w6/Circuit.h @@ -0,0 +1,18 @@ +#pragma once + +#include <iterator> + +#include "IComponent.h" + +class Circuit { +private: + unsigned size = 0; + IComponent* components[16] = { nullptr }; + +public: + virtual void add(IComponent* pComponent); + using iterator = IComponent* const *; + iterator begin() const { return &components[0]; } + iterator end() const { return &components[size]; } +}; + diff --git a/oop2w6/IComponent.cpp b/oop2w6/IComponent.cpp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/oop2w6/IComponent.cpp diff --git a/oop2w6/IComponent.h b/oop2w6/IComponent.h new file mode 100644 index 0000000..9ec31ca --- /dev/null +++ b/oop2w6/IComponent.h @@ -0,0 +1,9 @@ +#pragma once + +class IComponent { +public: + IComponent() {} + virtual ~IComponent() {} + virtual double getValue() = 0; +}; + diff --git a/oop2w6/Inductor.cpp b/oop2w6/Inductor.cpp new file mode 100644 index 0000000..6bf1fd2 --- /dev/null +++ b/oop2w6/Inductor.cpp @@ -0,0 +1,15 @@ +#include "Inductor.h" + +Inductor::Inductor(double inductance, bool isSaturable) { + this->inductance = inductance; + this->bIsSaturable = isSaturable; +} + +double Inductor::getValue() { + return inductance; +} + +bool Inductor::isSaturable() { + return bIsSaturable; +} + diff --git a/oop2w6/Inductor.h b/oop2w6/Inductor.h new file mode 100644 index 0000000..7bb25c4 --- /dev/null +++ b/oop2w6/Inductor.h @@ -0,0 +1,15 @@ +#pragma once + +#include "IComponent.h" + +class Inductor : public IComponent { +private: + double inductance; + bool bIsSaturable; + +public: + Inductor(double inductance, bool isSaturable); + virtual double getValue(); + virtual bool isSaturable(); +}; + diff --git a/oop2w6/Resistor.cpp b/oop2w6/Resistor.cpp new file mode 100644 index 0000000..4f64bbd --- /dev/null +++ b/oop2w6/Resistor.cpp @@ -0,0 +1,15 @@ +#include "Resistor.h" + +Resistor::Resistor(double value, double tolerance) { + this->resistance = value; + this->tolerance = tolerance; +} + +double Resistor::getValue() { + return resistance; +} + +double Resistor::getTolerance() { + return tolerance; +} + diff --git a/oop2w6/Resistor.h b/oop2w6/Resistor.h new file mode 100644 index 0000000..133004d --- /dev/null +++ b/oop2w6/Resistor.h @@ -0,0 +1,15 @@ +#pragma once + +#include "IComponent.h" + +class Resistor : public IComponent { +private: + double resistance; + double tolerance; + +public: + Resistor(double value, double tolerance); + virtual double getValue(); + virtual double getTolerance(); +}; + diff --git a/oop2w6/main.cpp b/oop2w6/main.cpp new file mode 100644 index 0000000..1bfb20e --- /dev/null +++ b/oop2w6/main.cpp @@ -0,0 +1,40 @@ +#include <iostream> + +#include "Circuit.h" +#include "Resistor.h" +#include "Capacitor.h" +#include "Inductor.h" + +int main() { + Circuit circuit; + // ShowVisitor visitor; + circuit.add(new Resistor(50,0.05)); + circuit.add(new Capacitor(5.0e-6,false)); + circuit.add(new Inductor(1.0e-6,true)); + + Resistor* resistor; + Capacitor* capacitor; + Inductor* inductor; + + for ( IComponent* component : circuit ) { + if ( (resistor=dynamic_cast<Resistor*>(component)) != nullptr ) { + std::cout << "resistor" << std::endl; + std::cout << " value " << resistor ->getValue() << std::endl; + std::cout << " tolerance " << resistor ->getTolerance() << std::endl; + } + if ( (capacitor=dynamic_cast<Capacitor*>(component)) != nullptr ) { + std::cout << "capacitor" << std::endl; + std::cout << " value " << capacitor->getValue() << std::endl; + std::cout << " electrolitic " <<(capacitor->isElectrolitic()?"yes":"no")<<std::endl; + } + if ( (inductor=dynamic_cast<Inductor*>(component)) != nullptr ) { + std::cout << "inductor" << std::endl; + std::cout << " value " << inductor->getValue() << std::endl; + std::cout << " saturable " << (inductor->isSaturable()?"yes":"no") << + std::endl; + } + } + // for (IComponent* component : circuit) + // component->accept( visitor ); + return 0; +} diff --git a/oop2w6/makefile b/oop2w6/makefile new file mode 120000 index 0000000..a4e84c6 --- /dev/null +++ b/oop2w6/makefile @@ -0,0 +1 @@ +../week.mk
\ No newline at end of file |