From e6c5f113235013031884e6f0f67fc2797cf334fb Mon Sep 17 00:00:00 2001 From: wayflayer <91454172+wayflayer@users.noreply.github.com> Date: Mon, 6 Jun 2022 11:13:45 +0300 Subject: [PATCH 1/9] Add files via upload --- homework1OOP.sln | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 homework1OOP.sln diff --git a/homework1OOP.sln b/homework1OOP.sln new file mode 100644 index 00000000..61b3b8a2 --- /dev/null +++ b/homework1OOP.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31825.309 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "homework1OOP", "homework1OOP\homework1OOP.vcxproj", "{DE56CAD4-685F-4E22-BF14-10F1C07A3B0F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {DE56CAD4-685F-4E22-BF14-10F1C07A3B0F}.Debug|x64.ActiveCfg = Debug|x64 + {DE56CAD4-685F-4E22-BF14-10F1C07A3B0F}.Debug|x64.Build.0 = Debug|x64 + {DE56CAD4-685F-4E22-BF14-10F1C07A3B0F}.Debug|x86.ActiveCfg = Debug|Win32 + {DE56CAD4-685F-4E22-BF14-10F1C07A3B0F}.Debug|x86.Build.0 = Debug|Win32 + {DE56CAD4-685F-4E22-BF14-10F1C07A3B0F}.Release|x64.ActiveCfg = Release|x64 + {DE56CAD4-685F-4E22-BF14-10F1C07A3B0F}.Release|x64.Build.0 = Release|x64 + {DE56CAD4-685F-4E22-BF14-10F1C07A3B0F}.Release|x86.ActiveCfg = Release|Win32 + {DE56CAD4-685F-4E22-BF14-10F1C07A3B0F}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {2DAAA8EF-090D-4F79-AF66-CDE520460A9D} + EndGlobalSection +EndGlobal From 9fcf163389b418505c36826af97a8d05459871b9 Mon Sep 17 00:00:00 2001 From: wayflayer <91454172+wayflayer@users.noreply.github.com> Date: Mon, 6 Jun 2022 11:29:42 +0300 Subject: [PATCH 2/9] Update and rename homework1OOP.sln to animal.h --- animal.h | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ homework1OOP.sln | 31 ------------------- 2 files changed, 79 insertions(+), 31 deletions(-) create mode 100644 animal.h delete mode 100644 homework1OOP.sln diff --git a/animal.h b/animal.h new file mode 100644 index 00000000..f7942204 --- /dev/null +++ b/animal.h @@ -0,0 +1,79 @@ +#pragma once +#include +#include +#include +using namespace std; +class Animal +{ +private: + int age; + int weight; + int height; +public: + void animal(int age, int weight, int height) { + age = age; + height = height; + weight = weight; + } + void set_age(int age) { age = age; } + void set_height(float height) { height = height; } + void set_weight(float weight) { weight = weight; } + + int get_age() { return age; } + float get_height() { return height; } + float get_weight() { return weight; } + virtual string about() const + { + stringstream ss; + ss << "age = " << get_age << ";" + << "height = " << get_height << " ;" + << "weight = " << get_weight << endl; + + } + +}; + +class Mammal :public Animal +{ +private: + string hair_color; + bool meat_eater; +public: + void mammal(string hair_color, bool meat_eater) { + hair_color = hair_color; + meat_eater = meat_eater; + } + void set_hair_color() { hair_color = hair_color; } + void set_meat_eater() { meat_eater = meat_eater; } + string get_hair_color() { return hair_color; } + bool get_meat_eater() { return meat_eater; } + virtual string about() const + { + stringstream ss; + ss << "hair color is - " << get_hair_color << ";" + << "he/she is meat eater? - " << get_meat_eater << endl; + } +}; + +class Human :public Mammal +{ +private: + bool have_work; + string lifestyle; +public: + void human(bool have_work, string lifestyle) { + have_work = have_work; + lifestyle = lifestyle; + } + void set_work() { have_work = have_work; } + void set_life_style() { lifestyle = lifestyle; } + bool get_work() { return have_work; } + string get_life_style() { lifestyle = lifestyle; } + virtual string about() const + { + stringstream ss; + ss << " have work ? - " << get_work << ", " + << "lifestyle = " << get_life_style; + return ss.str(); + } +}; diff --git a/homework1OOP.sln b/homework1OOP.sln deleted file mode 100644 index 61b3b8a2..00000000 --- a/homework1OOP.sln +++ /dev/null @@ -1,31 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.0.31825.309 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "homework1OOP", "homework1OOP\homework1OOP.vcxproj", "{DE56CAD4-685F-4E22-BF14-10F1C07A3B0F}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x64 = Debug|x64 - Debug|x86 = Debug|x86 - Release|x64 = Release|x64 - Release|x86 = Release|x86 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {DE56CAD4-685F-4E22-BF14-10F1C07A3B0F}.Debug|x64.ActiveCfg = Debug|x64 - {DE56CAD4-685F-4E22-BF14-10F1C07A3B0F}.Debug|x64.Build.0 = Debug|x64 - {DE56CAD4-685F-4E22-BF14-10F1C07A3B0F}.Debug|x86.ActiveCfg = Debug|Win32 - {DE56CAD4-685F-4E22-BF14-10F1C07A3B0F}.Debug|x86.Build.0 = Debug|Win32 - {DE56CAD4-685F-4E22-BF14-10F1C07A3B0F}.Release|x64.ActiveCfg = Release|x64 - {DE56CAD4-685F-4E22-BF14-10F1C07A3B0F}.Release|x64.Build.0 = Release|x64 - {DE56CAD4-685F-4E22-BF14-10F1C07A3B0F}.Release|x86.ActiveCfg = Release|Win32 - {DE56CAD4-685F-4E22-BF14-10F1C07A3B0F}.Release|x86.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {2DAAA8EF-090D-4F79-AF66-CDE520460A9D} - EndGlobalSection -EndGlobal From 165c5a7a6ed13c148ccf8fe67e1cef73f1e1ce4a Mon Sep 17 00:00:00 2001 From: wayflayer <91454172+wayflayer@users.noreply.github.com> Date: Mon, 6 Jun 2022 11:58:04 +0300 Subject: [PATCH 3/9] Add files via upload --- animal.cpp | 83 ++++++++++++++++++++++++++++++++++++++++++ animal.h | 4 +-- electricy.cpp | 26 ++++++++++++++ electricy.h | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++ vector.h | 21 +++++++++++ vector3d.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 324 insertions(+), 2 deletions(-) create mode 100644 animal.cpp create mode 100644 electricy.cpp create mode 100644 electricy.h create mode 100644 vector.h create mode 100644 vector3d.cpp diff --git a/animal.cpp b/animal.cpp new file mode 100644 index 00000000..9912774f --- /dev/null +++ b/animal.cpp @@ -0,0 +1,83 @@ +#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: + float kilometresRun; + public: + float vibrissaLength; // meters + 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(); } + }; + + + 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 WingBeatSpeedPerSecond; + public: + 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/animal.h b/animal.h index f7942204..d792a2b5 100644 --- a/animal.h +++ b/animal.h @@ -61,7 +61,7 @@ class Human :public Mammal bool have_work; string lifestyle; public: - void human(bool have_work, string lifestyle) { + void human(bool have_work, string lifestyle) { have_work = have_work; lifestyle = lifestyle; } @@ -76,4 +76,4 @@ class Human :public Mammal << "lifestyle = " << get_life_style; return ss.str(); } -}; +}; \ No newline at end of file diff --git a/electricy.cpp b/electricy.cpp new file mode 100644 index 00000000..16972c69 --- /dev/null +++ b/electricy.cpp @@ -0,0 +1,26 @@ +#include "electricy.h" +#include +class Generator : public Object { +public: + Pole phase, neural, earth; + + Generator(const std::string& name = "") : Object(name), phase("p"), neural("n"), earth("e") {} + + virtual size_t getPoleCount() const { return 3; } + + virtual const Pole* getPole(const std::string& name) const { + if (name == phase.name) { return &phase; } + else if (name == neural.name) { return &neural; } + else if (name == earth.name) { return &earth; } + + return nullptr; + } +protected: + virtual const Pole* getPole(size_t idx) const { + if (idx == 0) { return &phase; } + else if (idx == 1){return &neural;} + else if (idx == 2) { return &earth; } + + return nullptr; + } +}; \ No newline at end of file diff --git a/electricy.h b/electricy.h new file mode 100644 index 00000000..626d1594 --- /dev/null +++ b/electricy.h @@ -0,0 +1,99 @@ +#pragma once +#include +using namespace std; +class Object; + +struct Pole { + string name; + Object* connectedObject; + string connectedObjectPole; + + Pole(const std::string& name_) : name(name_), connectedObject(nullptr) {} +}; + + + +class Object{ + string name; +protected: + Object(const std::string& name_) : name(name_) {} + + Pole* getPole(size_t idx) { return nullptr; } + virtual const Pole* getPole(size_t idx) const = 0; + +public: + virtual ~Object() {} + + const std::string& getName() const { return name; } + void getName(const std::string& newName) { name = newName; } + + Pole* getPole(const std::string& name) { return const_cast(const_cast(this)->getPole(name)); } + + + virtual const Pole* getPole(const std::string& name) const = 0; + + virtual size_t getPoleCount() const = 0; + bool isConnectedTo(const Object& other) const; + bool connect(const std::string& poleName, const Object& other, const std::string& otherPoleName); +}; +class Switch : public Object { +public: + Pole a1, a2; + + 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 Light : public Object { +public: + Pole a1, a2; + + Light(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 { + if (name == a1.name) { return &a1; } + else if (name == a2.name) {return &a2;} + + return nullptr; + } +protected: + virtual const Pole* getPole(size_t idx) const { + if (idx == 0) {return &a1;} + else if (idx == 1) { return &a2; } + + return nullptr; + } +}; + +class Generator : public Object { +public: + Pole phase, neural, earth; + + Generator(const std::string& name = "") : Object(name), phase("p"), neural("n"), earth("e") {} + + virtual size_t getPoleCount() const { return 3; } + + virtual const Pole* getPole(const std::string& name) const { + if (name == phase.name) {return &phase;} + else if (name == neural.name) {return &neural; } + else if (name == earth.name) { return &earth; } + + return nullptr; + } +protected: + virtual const Pole* getPole(size_t idx) const { + if (idx == 0) return &phase; + else if (idx == 1) { return &neural; } + else if (idx == 2) { return &earth; } + + return nullptr; + } +}; diff --git a/vector.h b/vector.h new file mode 100644 index 00000000..d7b823c6 --- /dev/null +++ b/vector.h @@ -0,0 +1,21 @@ +#pragma once +#include +#include +using namespace std; + +class vector3d { + 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) { return data[idx]; } + float operator[](int idx) const { return data[idx]; } + + friend int main(int argc, char** argv); +}; + +std::ostream& operator <<(std::ostream& os, const vector3d& v); + diff --git a/vector3d.cpp b/vector3d.cpp new file mode 100644 index 00000000..3c7e8f73 --- /dev/null +++ b/vector3d.cpp @@ -0,0 +1,93 @@ +#include +#include "vector.h" +#include + +using namespace std; + +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; + //vector3d v4 = v1 + v2, v5 = v1 - v2, v6 = v1 * 0.5f; + //cout << "v4: " << v4 << endl << "v5: " << v5 << endl << "v6: " << v6 << endl; + class Vec { + public: + float data[3]; + Vec() { data[2] = data[1] = data[0] = 0; } + Vec(float value) { data[2] = data[1] = data[0] = value; } + Vec(float x, float y, float z) { data[0] = x; data[1] = y; data[2] = z; } + float operator[](int idx) const { return data[idx]; } + float& operator[](int idx) { return data[idx]; } + Vec operator +(const Vec& other) const { + return Vec(data[0] + other[0], data[1] + other[1], data[2] + other[2]); + }; + Vec operator -(const Vec& other) { + return Vec(data[0] - other[0], data[1] - other[1], data[2] - other[2]); + }; + Vec operator *(const float value) { + return Vec(data[0] * value, data[1] * value, data[2] * value); + }; + Vec operator /(const float value) { + return Vec(data[0] / value, data[1] / value, data[2] / value); + }; + Vec operator-() { + return Vec(-data[0], -data[1], -data[2]); + }; + Vec operator !() { + if ((data[0] == 0) && (data[1] == 0) && (data[2] == 0)) + return Vec(data[0] == 1, data[1] == 1, data[2] == 1); + else + return Vec(data[0] == 0, data[1] == 0, data[2] == 0); + }; + }; + + printf("address of v1: 0x%p, size: %zu bytes\n", &v1, sizeof(v1)); + printf("address of v1.data: 0x%p, size: %zu bytes\n", &v1.data, sizeof(v1.data)); + printf("address of v1.data[-1]: 0x%p, size: %zu bytes\n", &v1.data[-1], sizeof(v1.data[-1])); + printf("address of v1.data[0]: 0x%p, size: %zu bytes\n", &v1.data[0], sizeof(v1.data[0])); + printf("address of v1.data[1]: 0x%p, size: %zu bytes\n", &v1.data[1], sizeof(v1.data[1])); + printf("address of v1.data[2]: 0x%p, size: %zu bytes\n", &v1.data[2], sizeof(v1.data[2])); + printf("address of v1.data[2000]: 0x%p, size: %zu bytes\n", &v1.data[2000], sizeof(v1.data[2000])); + ostream& operator <<(ostream & os, const Vec3 & v); + { + return os << "{" << v[0] << "," << v[1] << "," << v[2] << "}"; + }; + + return 0; + bool test_Vec() { + Vec v1; + Vec v2(6, 6, 6); + Vec v3(4, 4, 4); + Vec v4 = v2 + v3, v5 = v3 - v2, v6 = v2 * 5, v7 = v2 / 2, v8 = -v3, v9 = !v2, v10 = !v1; + if (v4.data[0] != 10 || v4.data[1] != 10 || v4.data[2] != 10) { + cerr << "addition Error" << endl; + return 1; + } + if (v5.data[0] != 2 || v5.data[1] != 2 || v5.data[2] != 2) { + cerr << " subtraction Error" << endl; + return 1; + } + if (v6.data[0] != 30 || v6.data[1] != 30 || v6.data[2] != 30) { + cerr << " multiplication Error" << endl; + return 1; + } + if (v7.data[0] != 3 || v7.data[1] != 3 || v7.data[2] != 3) { + cerr << "division Error" << endl; + return 1; + } + if (v8.data[0] != -4 || v8.data[1] != -4 || v8.data[2] != -4) { + cerr << "inversion Error" << endl; + return 1; + } + if (v9.data[0] != 0 || v9.data[1] != 0 || v9.data[2] != 0) { + cerr << "Logic Error" << endl; + return 1; + } + if (v10.data[0] != 1 || v10.data[1] != 1 || v10.data[2] != 1) { + cerr << "Logic Error" << endl; + return 1; + } + } \ No newline at end of file From 9b0437c73ba4ba3ecb8297489f4949b3c1647ee6 Mon Sep 17 00:00:00 2001 From: wayflayer <91454172+wayflayer@users.noreply.github.com> Date: Mon, 13 Jun 2022 17:23:20 +0300 Subject: [PATCH 4/9] Update electricy.h --- electricy.h | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/electricy.h b/electricy.h index 626d1594..1f877cf5 100644 --- a/electricy.h +++ b/electricy.h @@ -1,55 +1,58 @@ #pragma once #include -using namespace std; class Object; struct Pole { - string name; + + std::string name; + Object* connectedObject; - string connectedObjectPole; + std::string connectedObjectPole; Pole(const std::string& name_) : name(name_), connectedObject(nullptr) {} }; - - -class Object{ - string name; +class Object { + std::string name; protected: Object(const std::string& name_) : name(name_) {} Pole* getPole(size_t idx) { return nullptr; } - virtual const Pole* getPole(size_t idx) const = 0; + Pole* getPole(size_t idx) { + return const_cast(const_cast(this)->getPole(idx)); + } public: 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; } - Pole* getPole(const std::string& name) { return const_cast(const_cast(this)->getPole(name)); } + Pole* getPole(const std::string& name) { return const_cast(const_cast(this)->getPole(name)); } virtual const Pole* getPole(const std::string& name) const = 0; virtual size_t getPoleCount() const = 0; - bool isConnectedTo(const Object& other) const; - bool connect(const std::string& poleName, const Object& other, const std::string& otherPoleName); + + + bool connect(const std::string& poleName, Object& other, const std::string& otherPoleName); + + bool disconnect(const std::string& poleName); }; + class Switch : public Object { public: Pole a1, a2; - 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 Light : public Object { public: Pole a1, a2; @@ -59,15 +62,15 @@ class Light : public Object { virtual size_t getPoleCount() const { return 2; } virtual const Pole* getPole(const std::string& name) const { - if (name == a1.name) { return &a1; } - else if (name == a2.name) {return &a2;} + if (name == a1.name) return &a1; + else if (name == a2.name) return &a2; return nullptr; } protected: virtual const Pole* getPole(size_t idx) const { - if (idx == 0) {return &a1;} - else if (idx == 1) { return &a2; } + if (idx == 0) return &a1; + else if (idx == 1)return &a2; return nullptr; } @@ -82,17 +85,17 @@ class Generator : public Object { virtual size_t getPoleCount() const { return 3; } virtual const Pole* getPole(const std::string& name) const { - if (name == phase.name) {return &phase;} - else if (name == neural.name) {return &neural; } - else if (name == earth.name) { return &earth; } + if (name == phase.name) return &phase; + else if (name == neural.name) return &neural; + else if (name == earth.name) return &earth; return nullptr; } protected: virtual const Pole* getPole(size_t idx) const { if (idx == 0) return &phase; - else if (idx == 1) { return &neural; } - else if (idx == 2) { return &earth; } + else if (idx == 1)return &neural; + else if (idx == 2)return &earth; return nullptr; } From 79395151fa63920b086e322124ea1c16014a5475 Mon Sep 17 00:00:00 2001 From: wayflayer <91454172+wayflayer@users.noreply.github.com> Date: Mon, 13 Jun 2022 17:23:43 +0300 Subject: [PATCH 5/9] Update electricy.cpp --- electricy.cpp | 111 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 91 insertions(+), 20 deletions(-) diff --git a/electricy.cpp b/electricy.cpp index 16972c69..15988723 100644 --- a/electricy.cpp +++ b/electricy.cpp @@ -1,26 +1,97 @@ -#include "electricy.h" #include -class Generator : public Object { -public: - Pole phase, neural, earth; +#include "electricity.h" - Generator(const std::string& name = "") : Object(name), phase("p"), neural("n"), earth("e") {} +using namespace std; - virtual size_t getPoleCount() const { return 3; } +bool Object::isConnectedTo(const Object& other) const +{ + for (size_t j = 0; j < getPoleCount(); j++) + { + const Pole* selfPole = getPole(j); + if (selfPole != nullptr && (selfPole->connectedObject) == &other) return true; + } - virtual const Pole* getPole(const std::string& name) const { - if (name == phase.name) { return &phase; } - else if (name == neural.name) { return &neural; } - else if (name == earth.name) { return &earth; } - return nullptr; - } -protected: - virtual const Pole* getPole(size_t idx) const { - if (idx == 0) { return &phase; } - else if (idx == 1){return &neural;} - else if (idx == 2) { return &earth; } + return false; +} - return nullptr; - } -}; \ No newline at end of file +bool Object::connect(const std::string& poleName, const Object& other, const std::string& otherPoleName) + +{ + + if (poleName == otherPoleName && &other == this) return false; + + Pole* selfPole = getPole(poleName); + Pole* otherPole = other.getPole(otherPoleName); + if (otherPole == nullptr || otherPole == nullptr) return false; + + (selfPole->connectedObject) = &other; + (selfPole->connectedObjectPole) = otherPoleName; + + (otherPole->connectedObject) = this; + + (otherPole->connectedObjectPole) = poleName; + + return true; +} + +bool Object::disconnect(const std::string& poleName) +{ + return false; + Pole* p = getPole(poleName); + if (p == nullptr) return false; + + (p->connectedObject) = nullptr; + (p->connectedObjectPole) = ""; + + return true; +} + + + +Switch::Switch(const std::string& name) + : Object(name) + , a1("A1") + , a2("A2") +{ +} +const Pole* Switch::getPole(const string& name) const +{ + if (name == a1.name) + return &a1; + if (name == a2.name) + return &a2; + return nullptr; +} + +const Pole* Switch::getPole(size_t idx) const +{ + if (idx == 0) return &a1; + else if (idx == 1) return &a2; + + + return nullptr; +} + +int main() +{ + Switch sw, sw2; + sw.connect("A2", sw2, "A1"); + cout << "is " << (sw.isConnectedTo(sw2) ? "" : "not ") << "connected" << endl; + + cout << "is " << (sw.isConnectedTo(sw2) ? "" : "not ") << "connected" << endl; + + Generator g; Light l; Switch sw3; + + g.connect("p", l, "A1"); + l.connect("A2", sw3, "A1"); + cout << "is " << (g.isConnectedTo(l) ? "" : "not ") << "connected" << endl; + cout << "is " << (l.isConnectedTo(sw3) ? "" : "not ") << "connected" << endl; + + g.disconnect("p"); + cout << "is " << (g.isConnectedTo(l) ? "" : "not ") << "connected" << endl; + + return 0; + + return 0; +} From 2c2bec96f0e40c14b080289e881725312f5dabde Mon Sep 17 00:00:00 2001 From: wayflayer <91454172+wayflayer@users.noreply.github.com> Date: Mon, 13 Jun 2022 17:27:42 +0300 Subject: [PATCH 6/9] Update vector.h --- vector.h | 69 ++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 57 insertions(+), 12 deletions(-) diff --git a/vector.h b/vector.h index d7b823c6..f0b75e34 100644 --- a/vector.h +++ b/vector.h @@ -1,21 +1,66 @@ #pragma once #include -#include -using namespace std; - -class vector3d { - float data[3]; +class vector3 { + 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; } + vector3() { data[0] = data[1] = data[2] = 0; } + vector3(float value) { data[0] = data[1] = data[2] = value; } + vector3(float x, float y, float z) { data[0] = x, data[1] = y, data[2] = z; } + + float& operator[](int idx) { + if (idx > 2 || idx < 0) + throw std::out_of_range("Index " + std::to_string(idx) + " out of bounds for type vector3"); + + return data[idx]; + } + float operator[](int idx) const { + if (idx > 2 || idx < 0) + throw std::out_of_range("Index " + std::to_string(idx) + " out of bounds for type vector3"); + + return data[idx]; + } + + vector3 operator+ (const vector3& v) const; + vector3 operator- (const vector3& v) const; + + vector3 operator* (const float& f) const; + vector3 operator/ (const float& f) const; - float& operator[](int idx) { return data[idx]; } - float operator[](int idx) const { return data[idx]; } + vector3 operator- () const; + vector3 operator! () const; - friend int main(int argc, char** argv); + bool operator ==(const vector3& v) const; }; -std::ostream& operator <<(std::ostream& os, const vector3d& v); +std::ostream& operator <<(std::ostream& os, const vector3& v) { + return os << "{" << v[0] << ", " << v[1] << ", " << v[2] << "}"; +} + +vector3 vector3::operator+(const vector3& v) const { + return { data[0] + v[0], data[1] + v[1], data[2] + v[2] }; +} + +vector3 vector3::operator-(const vector3& v) const { + return { data[0] - v[0], data[1] - v[1], data[2] - v[2] }; +} + +vector3 vector3::operator*(const float& f) const { + return { data[0] * f, data[1] * f, data[2] * f }; +} + +vector3 vector3::operator/(const float& f) const { + return { data[0] / f, data[1] / f, data[2] / f }; +} + +vector3 vector3::operator-() const { + return { -data[0], -data[1], -data[2] }; +} + +vector3 vector3::operator!() const { + return { float(!data[0] && !data[1] && !data[2]) }; +} +bool vector3::operator==(const vector3& v) const { + return data[0] == v[0] && data[1] == v[1] && data[2] == v[2]; +} From abc3f31e0f23ec49ab9ad410d26d77b04b1e0add Mon Sep 17 00:00:00 2001 From: wayflayer <91454172+wayflayer@users.noreply.github.com> Date: Mon, 13 Jun 2022 17:29:16 +0300 Subject: [PATCH 7/9] Update vector3d.cpp --- vector3d.cpp | 121 +++++++++++++++------------------------------------ 1 file changed, 34 insertions(+), 87 deletions(-) diff --git a/vector3d.cpp b/vector3d.cpp index 3c7e8f73..d7014313 100644 --- a/vector3d.cpp +++ b/vector3d.cpp @@ -1,93 +1,40 @@ -#include #include "vector.h" -#include -using namespace std; +bool test_vectors() { + auto vec_1 = vector3(1, 2, 3); + auto vec_2 = vector3(3, 2, 1); -ostream& operator <<(ostream& os, const vector3d& v) { - return os << "{ " << v[0] << ", " << v[1] << ", " << v[2] << " }"; -} + bool check_1 = (vec_1 + vec_2) == vector3(4); + bool check_2 = (vec_2 - vec_1) == vector3(2, 0, -2); + + auto vec_3 = vector3(3); + + bool check_3 = (vec_3 * 3) == vector3(9); + bool check_4 = (vec_3 / 3) == vector3(1); + bool check_5 = -vec_3 == vector3(-3); -int main(int argc, char** argv) { - vector3d v1, v2(12), v3(1, 3, 8); - v1[2] = 54; - //vector3d v4 = v1 + v2, v5 = v1 - v2, v6 = v1 * 0.5f; - //cout << "v4: " << v4 << endl << "v5: " << v5 << endl << "v6: " << v6 << endl; - class Vec { - public: - float data[3]; - Vec() { data[2] = data[1] = data[0] = 0; } - Vec(float value) { data[2] = data[1] = data[0] = value; } - Vec(float x, float y, float z) { data[0] = x; data[1] = y; data[2] = z; } - float operator[](int idx) const { return data[idx]; } - float& operator[](int idx) { return data[idx]; } - Vec operator +(const Vec& other) const { - return Vec(data[0] + other[0], data[1] + other[1], data[2] + other[2]); - }; - Vec operator -(const Vec& other) { - return Vec(data[0] - other[0], data[1] - other[1], data[2] - other[2]); - }; - Vec operator *(const float value) { - return Vec(data[0] * value, data[1] * value, data[2] * value); - }; - Vec operator /(const float value) { - return Vec(data[0] / value, data[1] / value, data[2] / value); - }; - Vec operator-() { - return Vec(-data[0], -data[1], -data[2]); - }; - Vec operator !() { - if ((data[0] == 0) && (data[1] == 0) && (data[2] == 0)) - return Vec(data[0] == 1, data[1] == 1, data[2] == 1); - else - return Vec(data[0] == 0, data[1] == 0, data[2] == 0); - }; - }; + auto vec_4 = vector3(0); - printf("address of v1: 0x%p, size: %zu bytes\n", &v1, sizeof(v1)); - printf("address of v1.data: 0x%p, size: %zu bytes\n", &v1.data, sizeof(v1.data)); - printf("address of v1.data[-1]: 0x%p, size: %zu bytes\n", &v1.data[-1], sizeof(v1.data[-1])); - printf("address of v1.data[0]: 0x%p, size: %zu bytes\n", &v1.data[0], sizeof(v1.data[0])); - printf("address of v1.data[1]: 0x%p, size: %zu bytes\n", &v1.data[1], sizeof(v1.data[1])); - printf("address of v1.data[2]: 0x%p, size: %zu bytes\n", &v1.data[2], sizeof(v1.data[2])); - printf("address of v1.data[2000]: 0x%p, size: %zu bytes\n", &v1.data[2000], sizeof(v1.data[2000])); - ostream& operator <<(ostream & os, const Vec3 & v); - { - return os << "{" << v[0] << "," << v[1] << "," << v[2] << "}"; - }; + bool check_6 = !vec_4 == vector3(1); + bool check_7 = !vec_1 == vector3(0); + + if (!check_1) + std::cerr << "Invalid result of the operator+ (vector3 lhs, vector3 rhs), lhs value: " << vec_1 << ", rhs value: " << vec_2 << std::endl; + if (!check_2) + std::cerr << "Invalid result of the operator- (vector3 lhs, vector3 rhs), lhs value: " << vec_2 << ", rhs value: " << vec_1 << std::endl; + if (!check_3) + std::cerr << "Invalid result of the operator* (vector3 lhs, float rhs), lhs value: " << vec_3 << ", rhs value: " << 3 << std::endl; + if (!check_4) + std::cerr << "Invalid result of the operator/ (vector3 lhs, float rhs), lhs value: " << vec_3 << ", rhs value: " << 3 << std::endl; + if (!check_5) + std::cerr << "Invalid result of the operator- (vector3 v), v value: " << vec_3 << std::endl; + if (!check_6) + std::cerr << "Invalid result of the operator! (vector3 v), v value: " << vec_4 << std::endl; + if (!check_7) + std::cerr << "Invalid result of the operator! (vector3 v), v value: " << vec_1 << std::endl; + + return check_1 && check_2 && check_3 && check_4 && check_5 && check_6 && check_7; +} - return 0; - bool test_Vec() { - Vec v1; - Vec v2(6, 6, 6); - Vec v3(4, 4, 4); - Vec v4 = v2 + v3, v5 = v3 - v2, v6 = v2 * 5, v7 = v2 / 2, v8 = -v3, v9 = !v2, v10 = !v1; - if (v4.data[0] != 10 || v4.data[1] != 10 || v4.data[2] != 10) { - cerr << "addition Error" << endl; - return 1; - } - if (v5.data[0] != 2 || v5.data[1] != 2 || v5.data[2] != 2) { - cerr << " subtraction Error" << endl; - return 1; - } - if (v6.data[0] != 30 || v6.data[1] != 30 || v6.data[2] != 30) { - cerr << " multiplication Error" << endl; - return 1; - } - if (v7.data[0] != 3 || v7.data[1] != 3 || v7.data[2] != 3) { - cerr << "division Error" << endl; - return 1; - } - if (v8.data[0] != -4 || v8.data[1] != -4 || v8.data[2] != -4) { - cerr << "inversion Error" << endl; - return 1; - } - if (v9.data[0] != 0 || v9.data[1] != 0 || v9.data[2] != 0) { - cerr << "Logic Error" << endl; - return 1; - } - if (v10.data[0] != 1 || v10.data[1] != 1 || v10.data[2] != 1) { - cerr << "Logic Error" << endl; - return 1; - } - } \ No newline at end of file +int main() { + return !test_vectors() From 5cbb3b3dd329603379ce5efdf8a2e5f327305b93 Mon Sep 17 00:00:00 2001 From: wayflayer <91454172+wayflayer@users.noreply.github.com> Date: Mon, 13 Jun 2022 17:29:44 +0300 Subject: [PATCH 8/9] Update vector3d.cpp --- vector3d.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vector3d.cpp b/vector3d.cpp index d7014313..d1db533d 100644 --- a/vector3d.cpp +++ b/vector3d.cpp @@ -1,6 +1,6 @@ #include "vector.h" -bool test_vectors() { +bool test_vector3() { auto vec_1 = vector3(1, 2, 3); auto vec_2 = vector3(3, 2, 1); @@ -37,4 +37,4 @@ bool test_vectors() { } int main() { - return !test_vectors() + return !test_vector3() From cd7a52cad8e8f22cbc3ffe3a82158f0c53818241 Mon Sep 17 00:00:00 2001 From: wayflayer <91454172+wayflayer@users.noreply.github.com> Date: Mon, 13 Jun 2022 20:01:55 +0300 Subject: [PATCH 9/9] Add files via upload --- memhacks.cpp | 39 +++++++++++++++++++++++++++++++++++++++ memhacks.h | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 memhacks.cpp create mode 100644 memhacks.h diff --git a/memhacks.cpp b/memhacks.cpp new file mode 100644 index 00000000..660aef7c --- /dev/null +++ b/memhacks.cpp @@ -0,0 +1,39 @@ +#include +#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] = (float)i * 2; +} + +void printInternals(const 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 "; a->printData(cout); cout << endl; + cout << "B data "; a->printData2(cout); cout << endl; +} + +@@ - 27, 7 + 30, 19 @@ void printInternals(const B & b) { + + std::string A::getBString() const { + return dynamic_cast(this)->getBString(); + } + + std::string B::getBString() const { + return b_s; + } + + float A::getData(int idx) const { + return dynamic_cast(this)->getData(idx); + } + + float B::getData(int idx) const { + return data[idx]; + } +} \ No newline at end of file diff --git a/memhacks.h b/memhacks.h new file mode 100644 index 00000000..c5bdd386 --- /dev/null +++ b/memhacks.h @@ -0,0 +1,35 @@ +#pragma once +#include +#include + +class B; +class A { + std::string a_s; + int foo; + + friend void printInternals(const B& b); + +public: + A(); + + virtual std::string getBString() const; + virtual float getData(int idx) const; + + void printData(std::ostream& os) const; + void printData2(std::ostream& os) const; +}; + +class B : public A { + std::string b_s; + float data[7]{}; + + friend void printInternals(const B& b); + +public: + B(); + + float getData(int idx) const override; + std::string getBString() const override; +}; + +void printInternals(const B& b); \ No newline at end of file