diff options
-rw-r--r-- | oop2w6/Capacitor.cpp | 3 | ||||
-rw-r--r-- | oop2w6/Capacitor.h | 1 | ||||
-rw-r--r-- | oop2w6/IComponent.h | 3 | ||||
-rw-r--r-- | oop2w6/Inductor.cpp | 4 | ||||
-rw-r--r-- | oop2w6/Inductor.h | 1 | ||||
-rw-r--r-- | oop2w6/Resistor.cpp | 3 | ||||
-rw-r--r-- | oop2w6/Resistor.h | 1 | ||||
-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 | 30 |
11 files changed, 69 insertions, 25 deletions
diff --git a/oop2w6/Capacitor.cpp b/oop2w6/Capacitor.cpp index ccb70bf..a298ce2 100644 --- a/oop2w6/Capacitor.cpp +++ b/oop2w6/Capacitor.cpp @@ -13,3 +13,6 @@ bool Capacitor::isElectrolitic() { return bIsElectrolitic; } +void Capacitor::accept(Visitor& visitor) { + visitor.visit(*this); +} diff --git a/oop2w6/Capacitor.h b/oop2w6/Capacitor.h index de1e830..11efe23 100644 --- a/oop2w6/Capacitor.h +++ b/oop2w6/Capacitor.h @@ -11,5 +11,6 @@ public: Capacitor(double capacity, bool isElectrolitic); virtual double getValue(); virtual bool isElectrolitic(); + virtual void accept(Visitor&); }; diff --git a/oop2w6/IComponent.h b/oop2w6/IComponent.h index 9ec31ca..d48ad85 100644 --- a/oop2w6/IComponent.h +++ b/oop2w6/IComponent.h @@ -1,9 +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 index 6bf1fd2..6bb59a7 100644 --- a/oop2w6/Inductor.cpp +++ b/oop2w6/Inductor.cpp @@ -13,3 +13,7 @@ bool Inductor::isSaturable() { return bIsSaturable; } +void Inductor::accept(Visitor& visitor) { + visitor.visit(*this); +} + diff --git a/oop2w6/Inductor.h b/oop2w6/Inductor.h index 7bb25c4..179ba2b 100644 --- a/oop2w6/Inductor.h +++ b/oop2w6/Inductor.h @@ -11,5 +11,6 @@ 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 index 4f64bbd..9299db9 100644 --- a/oop2w6/Resistor.cpp +++ b/oop2w6/Resistor.cpp @@ -13,3 +13,6 @@ double Resistor::getTolerance() { return tolerance; } +void Resistor::accept(Visitor& visitor) { + visitor.visit(*this); +} diff --git a/oop2w6/Resistor.h b/oop2w6/Resistor.h index 133004d..abeea40 100644 --- a/oop2w6/Resistor.h +++ b/oop2w6/Resistor.h @@ -11,5 +11,6 @@ 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 index 1bfb20e..47047ca 100644 --- a/oop2w6/main.cpp +++ b/oop2w6/main.cpp @@ -4,37 +4,17 @@ #include "Resistor.h" #include "Capacitor.h" #include "Inductor.h" +#include "ShowVisitor.h" int main() { Circuit circuit; - // ShowVisitor visitor; + 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 ); + for (IComponent* component : circuit) + component->accept(visitor); return 0; } |