From 6da9363740ec5d9a2b4384f1530adc20654e069f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=9F=D0=BE?= =?UTF-8?q?=D0=BB=D1=8F=D0=BA=D0=BE=D0=B2?= Date: Tue, 22 Mar 2022 11:09:27 +0300 Subject: [PATCH 1/5] =?UTF-8?q?1=20=D0=9F=D1=80=D0=B0=D0=BA=D1=82=D0=B8?= =?UTF-8?q?=D1=87=D0=B5=D1=81=D0=BA=D0=B0=D1=8F=20=D0=9F=D0=BE=D0=BB=D1=8F?= =?UTF-8?q?=D0=BA=D0=BE=D0=B2=20=D0=90.=20=D0=90.=20=D0=9A=D0=9C=D0=91?= =?UTF-8?q?=D0=9E-03-21?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- animals/animal.cpp | 12 ++++++-- animals/animal.h | 73 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 77 insertions(+), 8 deletions(-) diff --git a/animals/animal.cpp b/animals/animal.cpp index 6745215e..a88cb7a3 100644 --- a/animals/animal.cpp +++ b/animals/animal.cpp @@ -1,7 +1,15 @@ #include "animal.h" - +#include +#include using namespace std; int main() { - return 0; + Cat Scottish(5); + Horse Spirit(1234); + Scottish.set_MiceCaughtCounter(7); + Spirit.setkilometresRun(1500); + int res = Scottish.get_MiceCaughtCounter(); + int res2 = Spirit.get_kilometresRun(); + cout << res << " " << res2 << "\n"; + return 0; } \ No newline at end of file diff --git a/animals/animal.h b/animals/animal.h index 83a19442..384fafa6 100644 --- a/animals/animal.h +++ b/animals/animal.h @@ -1,18 +1,79 @@ #pragma once - #include +#include +using namespace std; + class Animal { +private: + bool CanMoveFreely; +public: + Animal() { CanMoveFreely = 0; } + Animal(bool YesOrNo) { CanMoveFreely = YesOrNo; } + void setCanMoveFreely(bool YesOrNo) { CanMoveFreely = YesOrNo; } + bool get_CanMoveFreely() const { return CanMoveFreely; } + virtual string about() const { return (stringstream() << "CanMoveFreely =" << CanMoveFreely).str(); } + +}; + + +class Mammal :public Animal { +private: + bool FeedsWithMilk; public: - float weight; // kg + Mammal() { FeedsWithMilk = 0; } + Mammal(bool FeedOrNot) { FeedsWithMilk = FeedOrNot; } + void setFeedsWithMilk(bool FeedOrNot) { FeedsWithMilk = FeedOrNot; } + bool get_FeedsWithMilk() const { return FeedsWithMilk; } + virtual string about() const { return (stringstream() << Animal::about() << ", " << "FeedsWithMilk =" << FeedsWithMilk).str(); } }; -class Mammal : public Animal { +class Cat :public Mammal { +private: + int MiceCaughtCounter; public: - float pregnancyDuration; // days + Cat(int MiceCaught) { MiceCaughtCounter = MiceCaught; } + void set_MiceCaughtCounter(int MiceCaught) { MiceCaughtCounter = MiceCaught; } + int get_MiceCaughtCounter() const { return MiceCaughtCounter; } + virtual string about() const { return (stringstream() << Animal::about() << ", " << Mammal::about() << ", " << "MiceCaughtCounter =" << MiceCaughtCounter).str(); } }; -class Cat : public Mammal { +class Horse :public Mammal { +private: + int kilometresRun; public: - float vibrissaLength; // meters + Horse(int distance) { kilometresRun = distance; } + void setkilometresRun(int distance) { kilometresRun = distance; } + int get_kilometresRun() const { return kilometresRun; } + virtual string about() const { return (stringstream() << Animal::about() << ", " << Mammal::about() << ", " << "kilometresRun =" << kilometresRun).str(); } }; + + +class Birds : public Animal { +private: + int FeathersCounter; +public: + Birds() { FeathersCounter = 0; } + Birds(int Feathers) { FeathersCounter = Feathers; } + void setFeathersCounter(int Feathers) { FeathersCounter = Feathers; } + int get_FeathersCounter() const { return FeathersCounter; } + virtual string about() const { return (stringstream() << Animal::about() << ", " << "FeathersCounter =" << FeathersCounter).str(); } +}; +class Pigeon :public Birds { +private: + bool CanBegForBread; +public: + Pigeon(int CheekyOrNot) { CanBegForBread = CheekyOrNot; } + bool get_CanBegForBread() const { return CanBegForBread; } + void setCanBegForBread(int CheekyOrNot) { CanBegForBread = CheekyOrNot; } + virtual string about() const { return (stringstream() << Animal::about() << ", " << Birds::about() << ", " << "CanBegForBread =" << CanBegForBread).str(); } +}; +class Caliber :public Birds { +private: + int WingBeatSpeed; +public: + Caliber(int BeatCounter) { WingBeatSpeed = BeatCounter; } + int get_WingBeatSpeed() const { return WingBeatSpeed; } + void setWingBeatSpeed(int BeatCounter) { WingBeatSpeed = BeatCounter; } + virtual string about() const { return (stringstream() << Animal::about() << ", " << Birds::about() << ", " << "WingBeatSpeed =" << WingBeatSpeed).str(); } +}; \ No newline at end of file From 32091cbd4c4b64ca3c23fb0c3b983b12001761a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=9F=D0=BE?= =?UTF-8?q?=D0=BB=D1=8F=D0=BA=D0=BE=D0=B2?= Date: Tue, 22 Mar 2022 13:49:42 +0300 Subject: [PATCH 2/5] =?UTF-8?q?=D0=97=D0=B4=D1=80=D0=B0=D1=81=D1=82=D0=B2?= =?UTF-8?q?=D1=83=D0=B9=D1=82=D0=B5,=20=D0=9F=D0=BE=D0=BB=D1=8F=D0=BA?= =?UTF-8?q?=D0=BE=D0=B2=20=D0=90=D0=90=20=D0=9A=D0=9C=D0=91=D0=9E-03-21.?= =?UTF-8?q?=20=D0=9F=D1=80=D0=B0=D0=BA=D1=82=D0=B8=D1=87=D0=B5=D1=81=D0=BA?= =?UTF-8?q?=D0=B0=D1=8F=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vectors/vector.cpp | 62 ++++++++++++++++++++++++++++++---------------- vectors/vector.h | 8 ++++++ 2 files changed, 49 insertions(+), 21 deletions(-) diff --git a/vectors/vector.cpp b/vectors/vector.cpp index a413ccdf..8e575ce3 100644 --- a/vectors/vector.cpp +++ b/vectors/vector.cpp @@ -1,36 +1,56 @@ +#pragma once #include - +#include using namespace std; - class vector3d { +public: float data[3]; public: vector3d() { data[2] = data[1] = data[0] = 0; } vector3d(float value) { data[2] = data[1] = data[0] = value; } vector3d(float a1, float a2, float a3) { data[0] = a1; data[1] = a2; data[2] = a3; } - float operator[](int idx) const { return data[idx]; } - float& operator[](int idx) { return data[idx]; } - // TODO: operator + (vector3d, vector3d), operator - (vector3d, vector3d), operator * (vector3d, float) - vector3d operator +(const vector3d& other) { return vector3d(/* ... */); } -}; + float operator [](int index) const { return data[index]; } + float& operator [](int index) { return data[index]; } -vector3d operator -(const vector3d& v1, const vector3d& v2) { - return vector3d(/* ... */); -} + vector3d operator +(const vector3d& other) { return vector3d(data[0] + other[0], data[1] + other[1], data[2] + other[2]); } + vector3d operator -(const vector3d& other) { return vector3d(data[0] - other[0], data[1] - other[1], data[2] - other[2]); } + vector3d operator *(const float value) { return vector3d(data[0] * value, data[1] * value, data[2] * value); } + vector3d operator /(const float value) { return vector3d(data[0] / value, data[1] / value, data[2] / value); } + vector3d operator -() { return vector3d(-data[0], -data[1], -data[2]); } + vector3d operator !() + { + if ((data[0] == 0) && (data[1] == 0) && (data[2] == 0)) + { + return vector3d(data[0] = 1, data[1] = 1, data[2] = 1); + } + else return vector3d(data[0] = 0, data[1] = 0, data[2] = 0); + } +}; ostream& operator <<(ostream& os, const vector3d& v) { return os << "{ " << v[0] << ", " << v[1] << ", " << v[2] << " }"; } - -int main(int argc, char** argv) { - vector3d v1, v2(12), v3(1, 3, 8); - v1[2] = 54; - /* TODO implement +, - and * operators first - vector3d v4 = v1 + v2, v5 = v1 - v2, v6 = v1 * 0.5f; - cout << "v4: " << v4 << endl; - cout << "v5: " << v5 << endl; - cout << "v6: " << v6 << endl; - */ - return 0; +bool test_vector3d() +{ + vector3d v1(5, 5, 5); + vector3d v2(1, 1, 1); + vector3d v3; + vector3d v4(0, 0, 0); + v3 = v1 + v2; + if ((v3.data[0] != 6) || (v3.data[1] != 6) || (v3.data[2] != 6)) { cout << " . v1(5,5,5), v2(1,1,1)"; return false; } + v3 = v1 - v2; + if ((v3.data[0] != 4) || (v3.data[1] != 4) || (v3.data[2] != 4)) { cout << " . v1(5,5,5), v2(1,1,1)"; return false; } + v3 = v1 * 3; + if ((v3.data[0] != 15) || (v3.data[1] != 15) || (v3.data[2] != 15)) { cout << " . v1(5,5,5), 3"; return false; } + v3 = v1 / 5; + if ((v3.data[0] != 1) || (v3.data[1] != 1) || (v3.data[2] != 1)) { cout << " . v1(5,5,5), 5"; return false; } + v3 = -v1; + if ((v3.data[0] != -5) || (v3.data[1] != -5) || (v3.data[2] != -5)) { cout << " . v1(5,5,5)"; return false; } + v3 = !v1; + if ((v3.data[0] != 0) || (v3.data[1] != 0) || (v3.data[2] != 0)) { cout << " ! . v4(0,0,0)"; return false; } + v3 = !v4; + if ((v3.data[0] != 1) || (v3.data[1] != 1) || (v3.data[2] != 1)) { cout << " ! . v1(1,1,1)"; return false; } + return true; } + diff --git a/vectors/vector.h b/vectors/vector.h index 6f70f09b..ed20ce74 100644 --- a/vectors/vector.h +++ b/vectors/vector.h @@ -1 +1,9 @@ #pragma once +#include +#include +using namespace std; +int main() { + setlocale(LC_ALL, "rus"); + if (test_vector3d()) cout << " " << 0; + else cout << " " << 1; +} \ No newline at end of file From 305491e2f2f0c1542175b671efd85ba4ec39ae3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=9F=D0=BE?= =?UTF-8?q?=D0=BB=D1=8F=D0=BA=D0=BE=D0=B2?= Date: Tue, 5 Apr 2022 10:46:33 +0300 Subject: [PATCH 3/5] =?UTF-8?q?=D0=92=D0=BD=D0=B5=D1=81=D0=B5=D0=BD=D1=8B?= =?UTF-8?q?=20=D0=B2=D1=81=D0=B5=20=D0=BF=D0=BE=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- animals/animal.cpp | 4 ++-- animals/animal.h | 18 +++++++++--------- vectors/vector.cpp | 17 ++++++++--------- vectors/vector.h | 6 +++--- 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/animals/animal.cpp b/animals/animal.cpp index a88cb7a3..32b925f7 100644 --- a/animals/animal.cpp +++ b/animals/animal.cpp @@ -7,9 +7,9 @@ int main() { Cat Scottish(5); Horse Spirit(1234); Scottish.set_MiceCaughtCounter(7); - Spirit.setkilometresRun(1500); + Spirit.setkilometresRun(1500.5); int res = Scottish.get_MiceCaughtCounter(); - int res2 = Spirit.get_kilometresRun(); + float res2 = Spirit.get_kilometresRun(); cout << res << " " << res2 << "\n"; return 0; } \ No newline at end of file diff --git a/animals/animal.h b/animals/animal.h index 384fafa6..199cbb30 100644 --- a/animals/animal.h +++ b/animals/animal.h @@ -40,11 +40,11 @@ class Cat :public Mammal { class Horse :public Mammal { private: - int kilometresRun; + float kilometresRun; public: - Horse(int distance) { kilometresRun = distance; } - void setkilometresRun(int distance) { kilometresRun = distance; } - int get_kilometresRun() const { return kilometresRun; } + Horse(float distance) { kilometresRun = distance; } + void setkilometresRun(float distance) { kilometresRun = distance; } + float get_kilometresRun() const { return kilometresRun; } virtual string about() const { return (stringstream() << Animal::about() << ", " << Mammal::about() << ", " << "kilometresRun =" << kilometresRun).str(); } }; @@ -70,10 +70,10 @@ class Pigeon :public Birds { }; class Caliber :public Birds { private: - int WingBeatSpeed; + int WingBeatSpeedPerSecond; public: - Caliber(int BeatCounter) { WingBeatSpeed = BeatCounter; } - int get_WingBeatSpeed() const { return WingBeatSpeed; } - void setWingBeatSpeed(int BeatCounter) { WingBeatSpeed = BeatCounter; } - virtual string about() const { return (stringstream() << Animal::about() << ", " << Birds::about() << ", " << "WingBeatSpeed =" << WingBeatSpeed).str(); } + Caliber(int BeatCounter) { WingBeatSpeedPerSecond = BeatCounter; } + int get_WingBeatSpeedPerSecond() const { return WingBeatSpeedPerSecond; } + void setWingBeatSpeedPerSecond(int BeatCounter) { WingBeatSpeedPerSecond = BeatCounter; } + virtual string about() const { return (stringstream() << Animal::about() << ", " << Birds::about() << ", " << "WingBeatSpeed =" << WingBeatSpeedPerSecond).str(); } }; \ No newline at end of file diff --git a/vectors/vector.cpp b/vectors/vector.cpp index 8e575ce3..5f50c544 100644 --- a/vectors/vector.cpp +++ b/vectors/vector.cpp @@ -1,6 +1,5 @@ #pragma once #include -#include using namespace std; class vector3d { public: @@ -38,19 +37,19 @@ bool test_vector3d() vector3d v3; vector3d v4(0, 0, 0); v3 = v1 + v2; - if ((v3.data[0] != 6) || (v3.data[1] != 6) || (v3.data[2] != 6)) { cout << " . v1(5,5,5), v2(1,1,1)"; return false; } + if ((v3.data[0] != 6) || (v3.data[1] != 6) || (v3.data[2] != 6)) { cout << "The addition operation does not work. Check Data v1(5,5,5), v2(1,1,1)"; return 1; } v3 = v1 - v2; - if ((v3.data[0] != 4) || (v3.data[1] != 4) || (v3.data[2] != 4)) { cout << " . v1(5,5,5), v2(1,1,1)"; return false; } + if ((v3.data[0] != 4) || (v3.data[1] != 4) || (v3.data[2] != 4)) { cout << "The subtraction operation does not work. Check Data v1(5,5,5), v2(1,1,1)"; return 1; } v3 = v1 * 3; - if ((v3.data[0] != 15) || (v3.data[1] != 15) || (v3.data[2] != 15)) { cout << " . v1(5,5,5), 3"; return false; } + if ((v3.data[0] != 15) || (v3.data[1] != 15) || (v3.data[2] != 15)) { cout << "The multiplication operation does not work. Check Data v1(5,5,5), 3"; return 1; } v3 = v1 / 5; - if ((v3.data[0] != 1) || (v3.data[1] != 1) || (v3.data[2] != 1)) { cout << " . v1(5,5,5), 5"; return false; } + if ((v3.data[0] != 1) || (v3.data[1] != 1) || (v3.data[2] != 1)) { cout << "The division operation by a scalar does not work. Check Data v1(5,5,5), 5"; return 1; } v3 = -v1; - if ((v3.data[0] != -5) || (v3.data[1] != -5) || (v3.data[2] != -5)) { cout << " . v1(5,5,5)"; return false; } + if ((v3.data[0] != -5) || (v3.data[1] != -5) || (v3.data[2] != -5)) { cout << "The invert operation does not work. Check Data v1(5,5,5)"; return 1; } v3 = !v1; - if ((v3.data[0] != 0) || (v3.data[1] != 0) || (v3.data[2] != 0)) { cout << " ! . v4(0,0,0)"; return false; } + if ((v3.data[0] != 0) || (v3.data[1] != 0) || (v3.data[2] != 0)) { cout << "Operation ! does not work. Check Data v4(0,0,0)"; return 1; } v3 = !v4; - if ((v3.data[0] != 1) || (v3.data[1] != 1) || (v3.data[2] != 1)) { cout << " ! . v1(1,1,1)"; return false; } - return true; + if ((v3.data[0] != 1) || (v3.data[1] != 1) || (v3.data[2] != 1)) { cout << "Operation ! does not work. Check Data v1(1,1,1)"; return 1; } + return 0; } diff --git a/vectors/vector.h b/vectors/vector.h index ed20ce74..ed3b9402 100644 --- a/vectors/vector.h +++ b/vectors/vector.h @@ -1,9 +1,9 @@ #pragma once #include -#include using namespace std; int main() { setlocale(LC_ALL, "rus"); - if (test_vector3d()) cout << " " << 0; - else cout << " " << 1; + if (test_vector3d()) cout << " " << 1; + else cout << " " << 0; + test_vector3d(); } \ No newline at end of file From 068b6edb5c1ffb3a4935aeeb53188d6610fc6ddf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=9F=D0=BE?= =?UTF-8?q?=D0=BB=D1=8F=D0=BA=D0=BE=D0=B2?= Date: Mon, 6 Jun 2022 10:59:37 +0300 Subject: [PATCH 4/5] =?UTF-8?q?=D0=9A=D1=80=D0=B0=D0=B9=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=20=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- electricity/electricity.cpp | 88 ++++++++++++++++++++++++++++++------- electricity/electricity.h | 27 ++++++++++-- memhacks/memhacks.cpp | 33 ++++++++++---- memhacks/memhacks.h | 20 ++++----- memhacks/newhacks.cpp | 40 +++++++++++++++-- memhacks/newhacks.h | 34 ++++++++++++++ 6 files changed, 199 insertions(+), 43 deletions(-) diff --git a/electricity/electricity.cpp b/electricity/electricity.cpp index 62da9453..88eb0071 100644 --- a/electricity/electricity.cpp +++ b/electricity/electricity.cpp @@ -5,20 +5,39 @@ using namespace std; bool Object::isConnectedTo(const Object& other) const { - // TODO - return false; + for (int i = 0; i < getPoleCount(); ++i) { + auto pole = getPole(i); + if (pole != nullptr && pole->connectedObject == &other) + return true; + } } bool Object::connect(const std::string& poleName, Object& other, const std::string& otherPoleName) -{ - // TODO - return false; -} +{ + if (poleName == otherPoleName && &other == this) + return false; + + auto pole = getPole(poleName); + auto otherPole = (Pole*)(other.getPole(otherPoleName)); + + pole->connectedObject = (Object*)(&other); + pole->connectedObjectPole = otherPoleName; + otherPole->connectedObject = this; + otherPole->connectedObjectPole = poleName; + + return true; + } + bool Object::disconnect(const std::string& poleName) -{ - // TODO - return false; +{ auto pole = getPole(poleName); + if (pole->connectedObjectPole == "") + return false; + else { + pole->connectedObjectPole = ""; + pole->connectedObject = nullptr; + return true; + } } Switch::Switch(const std::string& name) @@ -30,26 +49,61 @@ Switch::Switch(const std::string& name) const Pole* Switch::getPole(const string& name) const { - if (name == a1.name) - return &a1; - if (name == a2.name) - return &a2; + if (name == a1.name) return &a1; + if (name == a2.name) return &a2; return nullptr; } const Pole* Switch::getPole(size_t idx) const { - // TODO + return getPole("A" + to_string(idx + 1)); +} +Lamp::Lamp(const string& name) + : Object(name) + , a1("A1") + , a2("A2") +{ +} +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 { + return getPole("A" + to_string(idx + 1)); +} + +Generator::Generator(const string& name) + : Object(name) + , a1("A1") + , a2("A2") + , a3("A3") +{ +} +const Pole* Generator::getPole(const string& name) const { + if (name == a1.name) return &a1; + if (name == a2.name) return &a2; + if (name == a3.name) return &a3; return nullptr; } +const Pole* Generator::getPole(size_t idx) const { + return getPole("A" + to_string(idx + 1)); +} int main() { Switch sw, sw2; sw.connect("A2", sw2, "A1"); cout << "is " << (sw.isConnectedTo(sw2) ? "" : "not ") << "connected" << endl; - - // TODO: создать цепь из генератора, выключателя и светильника - + Generator generator_test; + Lamp lamp_test; + Switch switch_test; + generator_test.connect("A1", lamp_test, "A1"); + lamp_test.connect("A2", switch_test, "A1"); + cout << "is " << (generator_test.isConnectedTo(lamp_test) ? "" : "not ") << "connected" << endl; + cout << "is " << (lamp_test.isConnectedTo(switch_test) ? "" : "not ") << "connected" << endl; + generator_test.disconnect("A1"); + cout << "is " << (generator_test.isConnectedTo(lamp_test) ? "" : "not ") << "connected" << endl; return 0; } diff --git a/electricity/electricity.h b/electricity/electricity.h index cb19597b..6256992a 100644 --- a/electricity/electricity.h +++ b/electricity/electricity.h @@ -50,7 +50,11 @@ class Object { /// /// Индекс полюса, от 0 до значения, возвращаемого . /// Полюс с указанным индексом, или nullptr, если такой полюс не существует. - Pole* getPole(size_t idx) { /* TODO */ return nullptr; } + Pole* getPole(size_t idx) { + if (getPole("A" + std::to_string(idx))) + return getPole("A" + std::to_string(idx)); + return nullptr; + } /// /// Возвращает полюс по внутреннему индексу устройства. @@ -131,7 +135,22 @@ class Switch : public Object { protected: virtual const Pole* getPole(size_t idx) const; }; +class Lamp : public Object { +public: + Pole a1, a2; + Lamp(const std::string& name = ""); + size_t getPoleCount() const override { return 2; } + const Pole* getPole(const std::string& name) const override; +protected: + const Pole* getPole(size_t idx) const override; +}; -// TODO: класс светильника с двумя полюсами - -// TODO: класс генератора с тремя полюсами (фаза, нейтраль, земпя). +class Generator : public Object { +public: + Pole a1, a2, a3; + Generator(const std::string& name = ""); + size_t getPoleCount() const override { return 3; } + const Pole* getPole(const std::string& name) const override; +protected: + const Pole* getPole(size_t idx) const override; +}; \ No newline at end of file diff --git a/memhacks/memhacks.cpp b/memhacks/memhacks.cpp index 514c8689..58b17c0e 100644 --- a/memhacks/memhacks.cpp +++ b/memhacks/memhacks.cpp @@ -2,32 +2,47 @@ #include "memhacks.h" using namespace std; +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; } + /// /// Выводит на экран адреса и размеры объекта типа и его содержимого. /// Можно модифицировать для собственных отладочных целей. /// /// Изучаемый объект -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 printData: "; b.printData(cout); cout << endl; + cout << "B data printData2: "; b.printData2(cout); cout << endl; } - /// /// Извлекает значение из текущего объекта. /// Подразумевается, что текущий объект на самом деле представлено классом . /// /// Значение B::b_s -std::string A::getBString() const { - // TODO +string A::getBString() const { + return *((const string*)(this + 1)); +} + +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]; } /// @@ -37,16 +52,18 @@ std::string A::getBString() const { /// Подразумевается, что текущий объект на самом деле представлено классом . /// void A::printData(std::ostream& os) { - // TODO + os << "A string: " << a_s << ", B string: " << getBString() << ", data: "; + for (int i = 0; i < 7; ++i) os << getData(i) << " "; } - /// /// Извлекает значения , и /// из текущего объекта и выводит их в текстовом виде в указанный выходной поток /// с помощью виртуальных функций, предусмотренных в классе . /// void A::printData2(std::ostream& os) { - // TODO + B b = *((B*)(this)); + os << "A string: " << a_s << "', B string: " << b.getBString() << ", data: "; + for (int i = 0; i < 7; ++i) os << b.getData(i) << " "; } int main() diff --git a/memhacks/memhacks.h b/memhacks/memhacks.h index 51076b55..f90d6621 100644 --- a/memhacks/memhacks.h +++ b/memhacks/memhacks.h @@ -1,30 +1,30 @@ #pragma once - #include #include - +using namespace std; 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 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&); + friend void printInternals(B& b); public: B(); + + virtual std::string getBString() const; + virtual float getData(int idx) const; }; -void printInternals(const B& b); +void printInternals(B& b); \ No newline at end of file diff --git a/memhacks/newhacks.cpp b/memhacks/newhacks.cpp index ba359c62..bdbcb297 100644 --- a/memhacks/newhacks.cpp +++ b/memhacks/newhacks.cpp @@ -1,9 +1,41 @@ #include -#include "memhacks.h" - +#include "newhacks.h" using namespace std; +Foo::Foo() : student(), pupil() { cerr << "Вызван конструктор " << this << endl; } +Foo::~Foo() { cerr << "Вызван деструктор " << this << endl; } +void* Foo::operator new(size_t size) { + void* pVoid = malloc(size); + cerr << "Выполняется оператор new(" << size << ") returns " << pVoid << endl; + return pVoid; +} +void Foo::operator delete(void* memory) { + cerr << "Выполняется оператор delete(" << memory << ")" << endl; + free(memory); +} +Bar::Bar() : clever(), stupid() { cerr << "Вызван конструктор " << this << endl; } +Bar::~Bar() { cerr << "Вызван деструктор" << this << endl; } +void* Bar::operator new(size_t size) { + void* pVoid = malloc(size); + cerr << "Выполняется оператор new(" << size << ") returns " << pVoid << endl; + return pVoid; +} +void Bar::operator delete(void* memory) { + cerr << "Выполняется оператор delete(" << memory << ")" << endl; + free(memory); +} +Buz::Buz() : poltorashka(), shpala() { cerr << "Вызван конструктор" << this << endl; } +Buz::~Buz() { cerr << "Вызван деструктор" << this << endl; } + int main() { - return 0; -} + setlocale(LC_ALL, "Russian"); + Foo foo_test = Foo(); + Bar bar_test = Bar(); + Buz buz_test = Buz(); + Foo* foo_test2 = new Foo(); + Bar* bar_test2 = new Bar(); + delete(foo_test2); + delete(bar_test2); + return 0; +} \ No newline at end of file diff --git a/memhacks/newhacks.h b/memhacks/newhacks.h index 079a3835..85c64008 100644 --- a/memhacks/newhacks.h +++ b/memhacks/newhacks.h @@ -1,2 +1,36 @@ #pragma once +#include +using namespace std; +class Foo { + int student; + double pupil; +public: + Foo(); + ~Foo(); + static void* operator new(size_t size); + static void operator delete(void* memory); +}; +class Bar : public Foo { + int clever; + string stupid; + +public: + Bar(); + ~Bar(); + + static void* operator new(size_t size); + static void operator delete(void* memory); +}; + +class Buz : public Foo { + long poltorashka; + long long shpala; + +public: + Buz(); + ~Buz(); + + static void* operator new(size_t size) = delete; + static void operator delete(void* memory) = delete; +}; From 6c6618a3d223ee975a69771030cba57c8f0a7302 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=9F=D0=BE?= =?UTF-8?q?=D0=BB=D1=8F=D0=BA=D0=BE=D0=B2?= Date: Wed, 8 Jun 2022 21:55:13 +0300 Subject: [PATCH 5/5] =?UTF-8?q?=D0=9F=D0=BE=D1=81=D0=BB=D0=B5=D0=B4=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BF=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- animals/animal.h | 15 ++++++---- electricity/electricity.cpp | 18 +++++++----- memhacks/memhacks.cpp | 46 +++++++++++++++-------------- memhacks/memhacks.h | 10 +++---- vectors/vector.cpp | 58 ++++--------------------------------- vectors/vector.h | 56 ++++++++++++++++++++++++++++++----- 6 files changed, 105 insertions(+), 98 deletions(-) diff --git a/animals/animal.h b/animals/animal.h index 199cbb30..c518c783 100644 --- a/animals/animal.h +++ b/animals/animal.h @@ -7,9 +7,11 @@ using namespace std; class Animal { private: bool CanMoveFreely; -public: +protected: Animal() { CanMoveFreely = 0; } Animal(bool YesOrNo) { CanMoveFreely = YesOrNo; } +public: + void setCanMoveFreely(bool YesOrNo) { CanMoveFreely = YesOrNo; } bool get_CanMoveFreely() const { return CanMoveFreely; } virtual string about() const { return (stringstream() << "CanMoveFreely =" << CanMoveFreely).str(); } @@ -20,9 +22,10 @@ class Animal { class Mammal :public Animal { private: bool FeedsWithMilk; -public: +protected: Mammal() { FeedsWithMilk = 0; } Mammal(bool FeedOrNot) { FeedsWithMilk = FeedOrNot; } +public: void setFeedsWithMilk(bool FeedOrNot) { FeedsWithMilk = FeedOrNot; } bool get_FeedsWithMilk() const { return FeedsWithMilk; } virtual string about() const { return (stringstream() << Animal::about() << ", " << "FeedsWithMilk =" << FeedsWithMilk).str(); } @@ -31,6 +34,7 @@ class Mammal :public Animal { class Cat :public Mammal { private: int MiceCaughtCounter; + public: Cat(int MiceCaught) { MiceCaughtCounter = MiceCaught; } void set_MiceCaughtCounter(int MiceCaught) { MiceCaughtCounter = MiceCaught; } @@ -52,9 +56,10 @@ class Horse :public Mammal { class Birds : public Animal { private: int FeathersCounter; -public: +protected: Birds() { FeathersCounter = 0; } Birds(int Feathers) { FeathersCounter = Feathers; } +public: void setFeathersCounter(int Feathers) { FeathersCounter = Feathers; } int get_FeathersCounter() const { return FeathersCounter; } virtual string about() const { return (stringstream() << Animal::about() << ", " << "FeathersCounter =" << FeathersCounter).str(); } @@ -62,7 +67,7 @@ class Birds : public Animal { class Pigeon :public Birds { private: bool CanBegForBread; -public: +public: Pigeon(int CheekyOrNot) { CanBegForBread = CheekyOrNot; } bool get_CanBegForBread() const { return CanBegForBread; } void setCanBegForBread(int CheekyOrNot) { CanBegForBread = CheekyOrNot; } @@ -71,7 +76,7 @@ class Pigeon :public Birds { class Caliber :public Birds { private: int WingBeatSpeedPerSecond; -public: +public: Caliber(int BeatCounter) { WingBeatSpeedPerSecond = BeatCounter; } int get_WingBeatSpeedPerSecond() const { return WingBeatSpeedPerSecond; } void setWingBeatSpeedPerSecond(int BeatCounter) { WingBeatSpeedPerSecond = BeatCounter; } diff --git a/electricity/electricity.cpp b/electricity/electricity.cpp index 88eb0071..48c1f4da 100644 --- a/electricity/electricity.cpp +++ b/electricity/electricity.cpp @@ -30,14 +30,16 @@ bool Object::connect(const std::string& poleName, Object& other, const std::stri bool Object::disconnect(const std::string& poleName) -{ auto pole = getPole(poleName); - if (pole->connectedObjectPole == "") - return false; - else { - pole->connectedObjectPole = ""; - pole->connectedObject = nullptr; +{ + if (getPole(poleName)->connectedObject != nullptr) + { + getPole(getPole(poleName)->connectedObjectPole)->connectedObject = nullptr; + getPole(getPole(poleName)->connectedObjectPole)->connectedObjectPole = ""; + getPole(poleName)->connectedObject = nullptr; + getPole(poleName)->connectedObjectPole = ""; return true; } + return false; } Switch::Switch(const std::string& name) @@ -99,8 +101,8 @@ int main() Generator generator_test; Lamp lamp_test; Switch switch_test; - generator_test.connect("A1", lamp_test, "A1"); - lamp_test.connect("A2", switch_test, "A1"); + generator_test.connect("A1", switch_test, "A2"); + switch_test.connect("A1", lamp_test, "A2"); cout << "is " << (generator_test.isConnectedTo(lamp_test) ? "" : "not ") << "connected" << endl; cout << "is " << (lamp_test.isConnectedTo(switch_test) ? "" : "not ") << "connected" << endl; generator_test.disconnect("A1"); diff --git a/memhacks/memhacks.cpp b/memhacks/memhacks.cpp index 58b17c0e..c2404c67 100644 --- a/memhacks/memhacks.cpp +++ b/memhacks/memhacks.cpp @@ -16,14 +16,13 @@ B::B() : b_s("It's 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 printData: "; b.printData(cout); cout << endl; - cout << "B data printData2: "; b.printData2(cout); cout << endl; +void printInternals(const B& b) { + std::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 << std::endl; + std::cout << "Size of A is " << sizeof(A) << ", size of B is " << sizeof(B) << std::endl; + std::cout << "B string is '" << b.getBString() << "'" << std::endl; + std::cout << "B data: "; const_cast(&b)->printData2(std::cout); std::cout << std::endl; } + /// /// Извлекает значение из текущего объекта. /// Подразумевается, что текущий объект на самом деле представлено классом . @@ -37,23 +36,22 @@ 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]; -} - /// /// Извлекает значения , и /// из текущего объекта и выводит их в текстовом виде в указанный выходной поток /// с помощью адресной арифметики. /// Подразумевается, что текущий объект на самом деле представлено классом . /// -void A::printData(std::ostream& os) { - os << "A string: " << a_s << ", B string: " << getBString() << ", data: "; - for (int i = 0; i < 7; ++i) os << getData(i) << " "; +void A::printData(ostream& os) { + os << "a_s is " << a_s << std::endl; + os << "b_s is " << getBString() << endl; + float* b_Data = ((float*)(((string*)(this + 1)) + 1)); + os << "data is: "; + for (auto i = 0; i < sizeof(b_Data) - 1; i++) + { + os << b_Data[i] << " "; + } + os << endl; } /// /// Извлекает значения , и @@ -61,9 +59,15 @@ void A::printData(std::ostream& os) { /// с помощью виртуальных функций, предусмотренных в классе . /// void A::printData2(std::ostream& os) { - B b = *((B*)(this)); - os << "A string: " << a_s << "', B string: " << b.getBString() << ", data: "; - for (int i = 0; i < 7; ++i) os << b.getData(i) << " "; + os << "a_s is " << a_s << std::endl; + os << "b_s is " << getBString() << std::endl; + float* b_Data = getBData(); + os << "data is: "; + for (auto i = 0; i < sizeof(b_Data) - 1; i++) + { + os << b_Data[i] << " "; + } + os << std::endl; } int main() diff --git a/memhacks/memhacks.h b/memhacks/memhacks.h index f90d6621..cb36e87b 100644 --- a/memhacks/memhacks.h +++ b/memhacks/memhacks.h @@ -1,7 +1,6 @@ #pragma once #include #include -using namespace std; class B; // чтобы можно было объявить printInternals() как friend в классе A class A { std::string a_s; @@ -10,10 +9,11 @@ class A { public: A(); - virtual string getBString() const; - virtual float getData(int idx) const; + std::string getBString() const; + virtual float getBData(int idx) const; void printData(std::ostream& os); void printData2(std::ostream& os); + virtual float* getBData() = 0; }; class B : public A { std::string b_s; @@ -23,8 +23,8 @@ class B : public A { public: B(); - virtual std::string getBString() const; - virtual float getData(int idx) const; + std::string getBString() const; + virtual float getBData(int idx) const; }; void printInternals(B& b); \ No newline at end of file diff --git a/vectors/vector.cpp b/vectors/vector.cpp index 11ab6230..316819f8 100644 --- a/vectors/vector.cpp +++ b/vectors/vector.cpp @@ -3,55 +3,9 @@ #include "vector.h" using namespace std; -class vector3d { -public: - float data[3]; -public: - vector3d() { data[2] = data[1] = data[0] = 0; } - vector3d(float value) { data[2] = data[1] = data[0] = value; } - vector3d(float a1, float a2, float a3) { data[0] = a1; data[1] = a2; data[2] = a3; } - - float operator [](int index) const { return data[index]; } - float& operator [](int index) { return data[index]; } - - vector3d operator +(const vector3d& other) { return vector3d(data[0] + other[0], data[1] + other[1], data[2] + other[2]); } - vector3d operator -(const vector3d& other) { return vector3d(data[0] - other[0], data[1] - other[1], data[2] - other[2]); } - vector3d operator *(const float value) { return vector3d(data[0] * value, data[1] * value, data[2] * value); } - vector3d operator /(const float value) { return vector3d(data[0] / value, data[1] / value, data[2] / value); } - vector3d operator -() { return vector3d(-data[0], -data[1], -data[2]); } - vector3d operator !() - { - if ((data[0] == 0) && (data[1] == 0) && (data[2] == 0)) - { - return vector3d(data[0] = 1, data[1] = 1, data[2] = 1); - } - else return vector3d(data[0] = 0, data[1] = 0, data[2] = 0); - } -}; - -ostream& operator <<(ostream& os, const vector3d& v) { - return os << "{ " << v[0] << ", " << v[1] << ", " << v[2] << " }"; -} -bool test_vector3d() -{ - vector3d v1(5, 5, 5); - vector3d v2(1, 1, 1); - vector3d v3; - vector3d v4(0, 0, 0); - v3 = v1 + v2; - if ((v3.data[0] != 6) || (v3.data[1] != 6) || (v3.data[2] != 6)) { cout << "The addition operation does not work. Check Data v1(5,5,5), v2(1,1,1)"; return 1; } - v3 = v1 - v2; - if ((v3.data[0] != 4) || (v3.data[1] != 4) || (v3.data[2] != 4)) { cout << "The subtraction operation does not work. Check Data v1(5,5,5), v2(1,1,1)"; return 1; } - v3 = v1 * 3; - if ((v3.data[0] != 15) || (v3.data[1] != 15) || (v3.data[2] != 15)) { cout << "The multiplication operation does not work. Check Data v1(5,5,5), 3"; return 1; } - v3 = v1 / 5; - if ((v3.data[0] != 1) || (v3.data[1] != 1) || (v3.data[2] != 1)) { cout << "The division operation by a scalar does not work. Check Data v1(5,5,5), 5"; return 1; } - v3 = -v1; - if ((v3.data[0] != -5) || (v3.data[1] != -5) || (v3.data[2] != -5)) { cout << "The invert operation does not work. Check Data v1(5,5,5)"; return 1; } - v3 = !v1; - if ((v3.data[0] != 0) || (v3.data[1] != 0) || (v3.data[2] != 0)) { cout << "Operation ! does not work. Check Data v4(0,0,0)"; return 1; } - v3 = !v4; - if ((v3.data[0] != 1) || (v3.data[1] != 1) || (v3.data[2] != 1)) { cout << "Operation ! does not work. Check Data v1(1,1,1)"; return 1; } - return 0; -} - +int main() { + setlocale(LC_ALL, "rus"); + if (test_vector3d()) cout << " " << 1; + else cout << " " << 0; + test_vector3d(); +} \ No newline at end of file diff --git a/vectors/vector.h b/vectors/vector.h index ed3b9402..dccffb72 100644 --- a/vectors/vector.h +++ b/vectors/vector.h @@ -1,9 +1,51 @@ #pragma once #include -using namespace std; -int main() { - setlocale(LC_ALL, "rus"); - if (test_vector3d()) cout << " " << 1; - else cout << " " << 0; - test_vector3d(); -} \ No newline at end of file +#include +#include +class vector3d { +public: + float data[3]; +public: + vector3d() { data[2] = data[1] = data[0] = 0; } + vector3d(float value) { data[2] = data[1] = data[0] = value; } + vector3d(float a1, float a2, float a3) { data[0] = a1; data[1] = a2; data[2] = a3; } + + float operator [](int index) const { return data[index]; } + float& operator [](int index) { return data[index]; } + + vector3d operator +(const vector3d& other) { return vector3d(data[0] + other[0], data[1] + other[1], data[2] + other[2]); } + vector3d operator -(const vector3d& other) { return vector3d(data[0] - other[0], data[1] - other[1], data[2] - other[2]); } + vector3d operator *(const float value) { return vector3d(data[0] * value, data[1] * value, data[2] * value); } + vector3d operator /(const float value) { return vector3d(data[0] / value, data[1] / value, data[2] / value); } + vector3d operator -() { return vector3d(-data[0], -data[1], -data[2]); } + vector3d operator !() { + if (data[0] == 0 && data[1] == 0 && data[2] == 0) return vector3d(1, 1, 1); + else return vector3d(0, 0, 0); + } +}; + +ostream& operator <<(ostream& os, const vector3d& v) { + return os << "{ " << v[0] << ", " << v[1] << ", " << v[2] << " }"; +} +bool test_vector3d() +{ + vector3d v1(5, 5, 5); + vector3d v2(1, 1, 1); + vector3d v3; + vector3d v4(0, 0, 0); + v3 = v1 + v2; + if ((v3.data[0] != 6) || (v3.data[1] != 6) || (v3.data[2] != 6)) { cout << "The addition operation does not work. Check Data v1(5,5,5), v2(1,1,1)"; return 1; } + v3 = v1 - v2; + if ((v3.data[0] != 4) || (v3.data[1] != 4) || (v3.data[2] != 4)) { cout << "The subtraction operation does not work. Check Data v1(5,5,5), v2(1,1,1)"; return 1; } + v3 = v1 * 3; + if ((v3.data[0] != 15) || (v3.data[1] != 15) || (v3.data[2] != 15)) { cout << "The multiplication operation does not work. Check Data v1(5,5,5), 3"; return 1; } + v3 = v1 / 5; + if ((v3.data[0] != 1) || (v3.data[1] != 1) || (v3.data[2] != 1)) { cout << "The division operation by a scalar does not work. Check Data v1(5,5,5), 5"; return 1; } + v3 = -v1; + if ((v3.data[0] != -5) || (v3.data[1] != -5) || (v3.data[2] != -5)) { cout << "The invert operation does not work. Check Data v1(5,5,5)"; return 1; } + v3 = !v1; + if ((v3.data[0] != 0) || (v3.data[1] != 0) || (v3.data[2] != 0)) { cout << "Operation ! does not work. Check Data v4(0,0,0)"; return 1; } + v3 = !v4; + if ((v3.data[0] != 1) || (v3.data[1] != 1) || (v3.data[2] != 1)) { cout << "Operation ! does not work. Check Data v1(1,1,1)"; return 1; } + return 0; +}