diff options
-rw-r--r-- | oop2w6/Capacitor.cpp | 18 | ||||
-rw-r--r-- | oop2w6/Capacitor.h | 16 | ||||
-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 | 12 | ||||
-rw-r--r-- | oop2w6/Inductor.cpp | 19 | ||||
-rw-r--r-- | oop2w6/Inductor.h | 16 | ||||
-rw-r--r-- | oop2w6/Resistor.cpp | 18 | ||||
-rw-r--r-- | oop2w6/Resistor.h | 16 | ||||
-rw-r--r-- | oop2w6/ShowVisitor.cpp | 24 | ||||
-rw-r--r-- | oop2w6/ShowVisitor.h | 10 | ||||
-rw-r--r-- | oop2w6/Visitor.h | 14 | ||||
-rw-r--r-- | oop2w6/main.cpp | 20 | ||||
l--------- | oop2w6/makefile | 1 |
15 files changed, 208 insertions, 0 deletions
diff --git a/oop2w6/Capacitor.cpp b/oop2w6/Capacitor.cpp new file mode 100644 index 0000000..a298ce2 --- /dev/null +++ b/oop2w6/Capacitor.cpp @@ -0,0 +1,18 @@ +#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; +} + +void Capacitor::accept(Visitor& visitor) { + visitor.visit(*this); +} diff --git a/oop2w6/Capacitor.h b/oop2w6/Capacitor.h new file mode 100644 index 0000000..11efe23 --- /dev/null +++ b/oop2w6/Capacitor.h @@ -0,0 +1,16 @@ +#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(); + virtual void accept(Visitor&); +}; + 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..d48ad85 --- /dev/null +++ b/oop2w6/IComponent.h @@ -0,0 +1,12 @@ +#pragma once + +#include "Visitor.h" + +class IComponent { +public: + IComponent() {} + virtual ~IComponent() {} + virtual double getValue() = 0; + virtual void accept(Visitor&) = 0; +}; + diff --git a/oop2w6/Inductor.cpp b/oop2w6/Inductor.cpp new file mode 100644 index 0000000..6bb59a7 --- /dev/null +++ b/oop2w6/Inductor.cpp @@ -0,0 +1,19 @@ +#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; +} + +void Inductor::accept(Visitor& visitor) { + visitor.visit(*this); +} + diff --git a/oop2w6/Inductor.h b/oop2w6/Inductor.h new file mode 100644 index 0000000..179ba2b --- /dev/null +++ b/oop2w6/Inductor.h @@ -0,0 +1,16 @@ +#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(); + virtual void accept(Visitor&); +}; + diff --git a/oop2w6/Resistor.cpp b/oop2w6/Resistor.cpp new file mode 100644 index 0000000..9299db9 --- /dev/null +++ b/oop2w6/Resistor.cpp @@ -0,0 +1,18 @@ +#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; +} + +void Resistor::accept(Visitor& visitor) { + visitor.visit(*this); +} diff --git a/oop2w6/Resistor.h b/oop2w6/Resistor.h new file mode 100644 index 0000000..abeea40 --- /dev/null +++ b/oop2w6/Resistor.h @@ -0,0 +1,16 @@ +#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(); + virtual void accept(Visitor&); +}; + diff --git a/oop2w6/ShowVisitor.cpp b/oop2w6/ShowVisitor.cpp new file mode 100644 index 0000000..f30b701 --- /dev/null +++ b/oop2w6/ShowVisitor.cpp @@ -0,0 +1,24 @@ +#include <iostream> + +#include "Resistor.h" +#include "Capacitor.h" +#include "Inductor.h" +#include "ShowVisitor.h" + +void ShowVisitor::visit(Resistor& resistor) { + std::cout << "resistor" << std::endl; + std::cout << " value " << resistor.getValue() << std::endl; + std::cout << " tolerance " << resistor.getTolerance() << std::endl; +} + +void ShowVisitor::visit(Capacitor& capacitor) { + std::cout << "capacitor" << std::endl; + std::cout << " value " << capacitor.getValue() << std::endl; + std::cout << " electrolitic " << (capacitor.isElectrolitic() ? "yes" : "no") << std::endl; +} + +void ShowVisitor::visit(Inductor& inductor) { + std::cout << "inductor" << std::endl; + std::cout << " value " << inductor.getValue() << std::endl; + std::cout << " saturable " << ( inductor.isSaturable() ? "yes" : "no" ) << std::endl; +} diff --git a/oop2w6/ShowVisitor.h b/oop2w6/ShowVisitor.h new file mode 100644 index 0000000..a77d199 --- /dev/null +++ b/oop2w6/ShowVisitor.h @@ -0,0 +1,10 @@ +#pragma once + +#include "Visitor.h" + +class ShowVisitor : public Visitor { +public: + virtual void visit(Resistor&); + virtual void visit(Capacitor&); + virtual void visit(Inductor&); +}; diff --git a/oop2w6/Visitor.h b/oop2w6/Visitor.h new file mode 100644 index 0000000..31188d3 --- /dev/null +++ b/oop2w6/Visitor.h @@ -0,0 +1,14 @@ +#pragma once + +class Resistor; +class Capacitor; +class Inductor; +class Visitor { +public: + Visitor() {} + virtual ~Visitor() {} +public: + virtual void visit(Resistor&) {} + virtual void visit(Capacitor&) {} + virtual void visit(Inductor&) {} +}; diff --git a/oop2w6/main.cpp b/oop2w6/main.cpp new file mode 100644 index 0000000..47047ca --- /dev/null +++ b/oop2w6/main.cpp @@ -0,0 +1,20 @@ +#include <iostream> + +#include "Circuit.h" +#include "Resistor.h" +#include "Capacitor.h" +#include "Inductor.h" +#include "ShowVisitor.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)); + + 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 |