From 5785ac99c5199b8d737659457c72c2aecb1b4333 Mon Sep 17 00:00:00 2001 From: Sashkachick Date: Wed, 15 Jun 2022 13:31:48 +0300 Subject: [PATCH 1/3] animals --- animals/animal.cpp | 13 ++++++- animals/animal.h | 92 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 98 insertions(+), 7 deletions(-) diff --git a/animals/animal.cpp b/animals/animal.cpp index 6745215..53886a5 100644 --- a/animals/animal.cpp +++ b/animals/animal.cpp @@ -3,5 +3,16 @@ using namespace std; int main() { + Cat kity(10.5, 9, 5.5); + kity.setWeight(9); + int weigth = kity.getWeight(); + Shark sharky(550, 40, 350); + sharky.setJawSize(200); + sharky.setSize(700); + Giraffe giraffy (300.56, 15, 1500); + giraffy.setpregnancyDuration(10); + cout << sharky.getJawSize() << "\t" << sharky.getSize(); return 0; -} \ No newline at end of file +}; + + diff --git a/animals/animal.h b/animals/animal.h index 83a1944..7830f5d 100644 --- a/animals/animal.h +++ b/animals/animal.h @@ -1,18 +1,98 @@ #pragma once #include +#include +#include +using namespace std; -class Animal { +class Animal +{ public: - float weight; // kg + float getWeight() const { return weight; } + void setWeight(float weight) { this->weight = weight; } + virtual std::string about() const { return (std::stringstream() << "weight =" << weight).str(); } +private: + float weight; +protected: + Animal() // + { + weight = 0.; + } + Animal(float weight) + { + this->weight = weight; + } }; -class Mammal : public Animal { +class Mammal : public Animal +{ +private: +int pregnancyDuration; public: - float pregnancyDuration; // days + int getPregnancyDuration() const { return pregnancyDuration; } + void setpregnancyDuration(int period) { pregnancyDuration = period; } + virtual std::string about() const { return (std::stringstream() << Animal::about() << ", " << "pregnancyDuration =" << pregnancyDuration).str(); } +protected: + Mammal() { + pregnancyDuration = 0; + } + Mammal(float weight, int period) :Animal(weight) { pregnancyDuration = period; } + + +}; + +class Cat : public Mammal +{ +private: + float vibrissaLength; +public: + float getVibrissaLength() const { return vibrissaLength; } + void setvibrissaLength(int length) { vibrissaLength = length; } + Cat(float weight, int period, float length) :Mammal(weight, period) { vibrissaLength = length; } + virtual std::string about() const { return (std::stringstream() << Animal::about() << ", " << Mammal::about() << ", " << "vibrasaLength =" << vibrissaLength).str(); } + Cat() { + vibrissaLength = 0; + } }; -class Cat : public Mammal { +class Giraffe : public Mammal +{ +private: + int height; public: - float vibrissaLength; // meters + int getHeight() const { return height; } + void setHeight(int height) { this->height = height; } + Giraffe(float weight, int period, int height) :Mammal(weight, period) { setHeight(height); } + virtual std::string about() const { return (std::stringstream() << Animal::about() << ", " << Mammal::about() << ", " << "heigth = " << height).str(); } + Giraffe() { height = 0; } }; + +class Fish : public Animal +{ +private: + int size; +public: + int getSize() const { return size; } + void setSize(int size) { this->size = size; } + virtual std::string about() const { return (std::stringstream() << Animal::about() << ", " << "size =" << size).str(); } +protected: + Fish(float weight, int size) :Animal(weight) { setSize(size); } + Fish() { + size = 0; + } + +}; + +class Shark : public Fish +{ +private: + int jawSize;// +public: + int getJawSize() const { return jawSize; } + void setJawSize(int size) { jawSize = size; } + Shark(float weight, int size, int jawSize) :Fish(weight, size) {setJawSize(jawSize);} + Shark() { jawSize = 0; } + virtual std::string about() const { return (std::stringstream() << Animal::about() << ", " << Fish::about() << ", " << "jawSize =" << jawSize).str();} + +}; + From c736b83e8fa6c477eed3cc3d0306752daf456965 Mon Sep 17 00:00:00 2001 From: Sashkachick Date: Wed, 15 Jun 2022 15:58:37 +0300 Subject: [PATCH 2/3] memhacks --- memhacks/memhacks.cpp | 34 ++++++++++++++++++++++++++++++---- memhacks/memhacks.h | 20 ++++++++++---------- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/memhacks/memhacks.cpp b/memhacks/memhacks.cpp index 514c868..4d40918 100644 --- a/memhacks/memhacks.cpp +++ b/memhacks/memhacks.cpp @@ -3,9 +3,11 @@ using namespace std; -B::B() : b_s("It's b!") { +A::A() : a_s("It's--a"), foo(0) {} + +B::B() : b_s("It's--b") { for (auto i = 0; i < sizeof(data) / sizeof(data[0]); i++) - data[i] = i * 2; + data[i] = (float)i * 2; } /// @@ -13,12 +15,15 @@ B::B() : b_s("It's b!") { /// Можно модифицировать для собственных отладочных целей. /// /// Изучаемый объект -void printInternals(const B& b) { + +void printInternals(B& b) { const A* a = &b, * a2 = a + 1; cout << "Address of b is 0x" << &b << ", address of b.a_s is 0x" << &b.a_s << ", address of b.b_s is 0x" << &b.b_s << endl; cout << "Size of A is " << sizeof(A) << ", size of B is " << sizeof(B) << endl; cout << "B string is '" << b.getBString() << "'" << endl; //cout << "B data: "; b.printData(cout); cout << endl; + cout << "B data,using printData: "; b.printData(cout); cout << endl; + cout << "B data,using printData2: "; b.printData2(cout); cout << endl; } /// @@ -27,9 +32,23 @@ void printInternals(const B& b) { /// /// Значение B::b_s std::string A::getBString() const { - // TODO + return *((const string*)(this + 1)); +} + +std::string B::getBString() const { + return b_s; +} + +float A::getData(int idx) const { + return ((float*)(this + 2))[idx]; } +float B::getData(int idx) const { + return data[idx]; +} + + + /// /// Извлекает значения , и /// из текущего объекта и выводит их в текстовом виде в указанный выходной поток @@ -38,6 +57,8 @@ std::string A::getBString() const { /// void A::printData(std::ostream& os) { // TODO + os << "A string is '" << a_s << "', B string is '" << getBString() << "'" << endl; + for (int i = 0; i < 7; ++i) os << getData(i) << " "; } /// @@ -47,8 +68,13 @@ void A::printData(std::ostream& os) { /// void A::printData2(std::ostream& os) { // TODO + B b = *((B*)(this)); + os << "A string is '" << a_s << "', B string is '" << b.getBString() << "'" << endl; + for (int i = 0; i < 7; ++i) os << b.getData(i) << " "; } + + int main() { B b; diff --git a/memhacks/memhacks.h b/memhacks/memhacks.h index 51076b5..3046324 100644 --- a/memhacks/memhacks.h +++ b/memhacks/memhacks.h @@ -1,30 +1,30 @@ #pragma once - #include #include - class B; // чтобы можно было объявить printInternals() как friend в классе A - class A { std::string a_s; int foo; - friend void printInternals(const B&); + friend void printInternals(B& b); public: - std::string getBString() const; + A(); + virtual std::string getBString() const; + virtual float getData(int idx) const; + void printData(std::ostream& os); void printData2(std::ostream& os); }; class B : public A { std::string b_s; - float data[7]; - - friend void printInternals(const B&); - + float data[7]{}; + friend void printInternals(B& b); public: B(); + float getData(int idx) const override; + std::string getBString() const override; }; -void printInternals(const B& b); +void printInternals( B& b); From 53feedb1915cc6f1301e4ed4974e82c12ee095e7 Mon Sep 17 00:00:00 2001 From: Sashkachick Date: Wed, 15 Jun 2022 16:38:00 +0300 Subject: [PATCH 3/3] electricity --- electricity/electricity.cpp | 86 ++++++++++++++++++++++++++++++++++++- electricity/electricity.h | 33 +++++++++++--- 2 files changed, 113 insertions(+), 6 deletions(-) diff --git a/electricity/electricity.cpp b/electricity/electricity.cpp index 9f1c017..f47f69c 100644 --- a/electricity/electricity.cpp +++ b/electricity/electricity.cpp @@ -6,15 +6,32 @@ using namespace std; bool Object::isConnectedTo(const Object& other) const { // TODO + for (size_t i = 0; i < getPoleCount(); i++) + { + if (getPole(i)->connectedObject == &other) return true; + } + return false; } bool Object::connect(const std::string& poleName, const Object& other, const std::string& otherPoleName) { // TODO - return false; + if (getPole(poleName) == nullptr || getPole(poleName)->name == otherPoleName) return false; + getPole(poleName)->connectedObject = &other; + getPole(poleName)->connectedObjectPole = otherPoleName; + return true; } + + +bool Object::disconnect(const std::string& poleName) +{ + getPole(poleName)->connectedObject = nullptr; + getPole(poleName)->connectedObjectPole = nullptr; + + return true; +} Switch::Switch(const std::string& name) : Object(name) , a1("A1") @@ -34,6 +51,62 @@ const Pole* Switch::getPole(const string& name) const const Pole* Switch::getPole(size_t idx) const { // TODO + if (idx == 0) { + return &a1; + } + else if (idx == 1) { + return &a2; + } + else { + return nullptr; + } +} + +const Pole* Lamp::getPole(const string& name) const +{ + if (name == a1.name) + return &a1; + if (name == a2.name) + return &a2; + return nullptr; +} + +const Pole* Lamp::getPole(size_t idx) const +{ + if (idx == 0) { + return &a1; + } + else if (idx == 1) { + return &a2; + } + else { + return nullptr; + } +} + +const Pole* Generator::getPole(const string& name) const +{ + if (name == p.name) //phase + return &p; + if (name == n.name) //neutral + return &n; + if (name == e.name) //earth + return &e; + return nullptr; +} + +const Pole* Generator::getPole(size_t idx) const +{ + if (idx == 0) { + return &p; + } + else if (idx == 1) { + return &n; + } + else if (idx == 2) { + return &e; + } + return nullptr; } @@ -43,6 +116,17 @@ int main() sw.connect("A2", sw2, "A1"); cout << "is " << (sw.isConnectedTo(sw2) ? "" : "not ") << "connected" << endl; + Generator g; Lamp l; Switch s; + + g.connect("Phase", s, "A1"); + s.connect("A2", l, "A1"); + l.connect("A2", g, "Neutral"); + + + cout << "is " << (g.isConnectedTo(s) ? "" : "not ") << "connected" << endl; + cout << "is " << (s.isConnectedTo(l) ? "" : "not ") << "connected" << endl; + cout << "is " << (l.isConnectedTo(g) ? "" : "not ") << "connected" << endl; + // TODO: создать цепь из генератора, выключателя и светильника return 0; diff --git a/electricity/electricity.h b/electricity/electricity.h index f7df29c..515aab0 100644 --- a/electricity/electricity.h +++ b/electricity/electricity.h @@ -24,7 +24,7 @@ struct Pole { /// два разных полюса. /// Значение nullptr означает, что к данному полюсу ничего не подключено. /// - Object* connectedObject; + const Object* connectedObject ; /// /// Полюс устройства, к которому подключен данный полюс. @@ -50,8 +50,7 @@ class Object { /// /// Индекс полюса, от 0 до значения, возвращаемого . /// Полюс с указанным индексом, или nullptr, если такой полюс не существует. - Pole* getPole(size_t idx) { /* TODO */ return nullptr; } - + Pole* getPole(size_t idx) { const_cast(const_cast(this)->getPole(idx)); } /// /// Возвращает полюс по внутреннему индексу устройства. /// @@ -63,7 +62,7 @@ class Object { virtual ~Object() {} const std::string& getName() const { return name; } - void getName(const std::string &newName) { name = newName; } + void setName(const std::string& newName) { name = newName; } /// /// Возвращает полюс по имени. @@ -87,7 +86,7 @@ class Object { /// /// Устройство, наличие прямой связи с которым проверяется. /// true если устройства связаны напрямую, false в противном случае. - bool isConnectedTo(const Object& other) const; + bool isConnectedTo(const Object& other) const; /// /// Соединяет указанные полюса текущего и указанного устройства. @@ -102,6 +101,7 @@ class Object { /// для которого вызывается этот метод. /// bool connect(const std::string& poleName, const Object& other, const std::string& otherPoleName); + bool disconnect(const std::string& poleName); }; /// @@ -113,6 +113,19 @@ class Switch : public Object { Switch(const std::string& name = ""); + virtual size_t getPoleCount() const { return 2; } + virtual const Pole* getPole(const std::string& name) const; + +protected: + virtual const Pole* getPole(size_t idx) const; +}; + +class Lamp : public Object { +public: + Pole a1, a2; + + Lamp(const std::string& name = "") : Object(name), a1("A1"), a2("A2") {}; + virtual size_t getPoleCount() const { return 2; } virtual const Pole* getPole(const std::string& name) const; @@ -122,5 +135,15 @@ class Switch : public Object { }; // TODO: класс светильника с двумя полюсами +class Generator : public Object { +public: + Pole p, n, e; + + Generator(const std::string& name = "") : Object(name), p("Phase"), n("Neutral"), e("Earth") {}; + virtual size_t getPoleCount() const { return 3; } + virtual const Pole* getPole(const std::string& name) const; +protected: + virtual const Pole* getPole(size_t idx) const; +}; // TODO: класс генератора с тремя полюсами (фаза, нейтраль, земпя).