From b68399ce828379f397d33c681b1fc418599194b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=BB=D0=B8=D0=B7=D0=B0=D0=B2=D0=B5=D1=82=D0=B0=20?= =?UTF-8?q?=D0=A1=D0=B8=D0=B4=D0=BE=D1=80=D0=BE=D0=B2=D0=B0?= <2003sev@mail.ru> Date: Sun, 12 Jun 2022 14:19:56 +0300 Subject: [PATCH 1/4] Animals 1 --- animals/animal.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++++-- animals/animal.h | 55 ++++++++++++++++++++++++++++++--- 2 files changed, 125 insertions(+), 6 deletions(-) diff --git a/animals/animal.cpp b/animals/animal.cpp index 6745215e..b0ca9f04 100644 --- a/animals/animal.cpp +++ b/animals/animal.cpp @@ -1,7 +1,79 @@ #include "animal.h" - +#include using namespace std; +string Animal::about() const { + stringstream ss; + ss << "Weight = " << weight << '\n' << "Height = " << height; + return ss.str(); +} + +string Bird::about() const { + stringstream ss; + ss << Animal::about() << '\n' << "Ability to fly = " << fly << '\n' << "Wingspan =" << wingspan; + return ss.str(); +} + +string Penguin::about() const { + stringstream ss; + ss << Bird::about() << '\n' << "Sex = " << sex; + return ss.str(); +} + +string Parrot::about() const { + stringstream ss; + ss << Bird::about() << '\n' << "Ability to speak = " << speak; + return ss.str(); +} + +string Mammal::about() const { + stringstream ss; + ss << Animal::about() << '\n' << "Duration of pregnancy = " << pregnancyDuration << '\n' << "Age =" << age; + return ss.str(); +} + +string Dog::about() const { + stringstream ss; + ss << Mammal::about() << '\n' << "Color of wool = " << woolcolor; + return ss.str(); +} + +string Human::about() const { + stringstream ss; + ss << Mammal::about() << '\n' << "IQ = " << iq; + return ss.str(); +} + int main() { - return 0; + Penguin Skipper; + Skipper.weight = 3; + Skipper.height = 0.4; + Skipper.fly = false; + Skipper.wingspan = 40; + Skipper.sex = "Male"; + cout << "Skipper : " << Skipper.about() << endl << endl; + + Parrot Kesha; + Kesha.weight = 0.04; + Kesha.height = 0.18; + Kesha.fly = true; + Kesha.wingspan = 30; + Kesha.speak = true; + cout << "Kesha : " << Kesha.about() << endl << endl; + + Dog Masyanya; + Masyanya.weight = 30; + Masyanya.height = 0.57; + Masyanya.pregnancyDuration = 63; + Masyanya.age = 10; + Masyanya.woolcolor = "White"; + cout << "Masyanya : " << Masyanya.about() << endl << endl; + + Human Vasilisa; + Vasilisa.weight = 50; + Vasilisa.height = 1.70; + Vasilisa.pregnancyDuration = 280; + Vasilisa.age = 30; + Vasilisa.iq = 126; + cout << "Vasilisa : " << Vasilisa.about() << endl; } \ No newline at end of file diff --git a/animals/animal.h b/animals/animal.h index 83a19442..f644a51e 100644 --- a/animals/animal.h +++ b/animals/animal.h @@ -1,18 +1,65 @@ #pragma once - +#include #include +#include +using namespace std; class Animal { public: - float weight; // kg + float weight; // kg + float height; // meters + + virtual std::string about() const; }; class Mammal : public Animal { public: float pregnancyDuration; // days + float age; //years + + virtual std::string about() const; +}; + +class Dog : public Mammal { +public: + string woolcolor; //color + + virtual std::string about() const; +}; + +class Human : public Mammal { +public: + int iq; //scores + + virtual std::string about() const; }; -class Cat : public Mammal { +class Bird : public Animal { public: - float vibrissaLength; // meters + bool fly; // true/false + float wingspan; //centimeters + + virtual std::string about() const; +}; + +class Penguin : public Bird { +public: + string sex; //male/female + + virtual std::string about() const; }; + +class Parrot : public Bird { +public: + bool speak; //true/false + + virtual std::string about() const; +}; + +inline std::ostream& operator <<(std::ostream& os, const Penguin& penguin) { + return os << penguin.about(); +} + +inline std::ostream& operator <<(std::ostream& os, const Parrot& parrot) { + return os << parrot.about(); +} \ No newline at end of file From 25ff0a87633163a1417b0a5e078aff26be4d092b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=BB=D0=B8=D0=B7=D0=B0=D0=B2=D0=B5=D1=82=D0=B0=20?= =?UTF-8?q?=D0=A1=D0=B8=D0=B4=D0=BE=D1=80=D0=BE=D0=B2=D0=B0?= <2003sev@mail.ru> Date: Sun, 12 Jun 2022 19:32:28 +0300 Subject: [PATCH 2/4] Animals 2 --- animals/animal.cpp | 80 +++++--------------------------------- animals/animal.h | 97 +++++++++++++++++++++++++++++++++------------- 2 files changed, 81 insertions(+), 96 deletions(-) diff --git a/animals/animal.cpp b/animals/animal.cpp index b0ca9f04..f46ccd3d 100644 --- a/animals/animal.cpp +++ b/animals/animal.cpp @@ -2,78 +2,18 @@ #include using namespace std; -string Animal::about() const { - stringstream ss; - ss << "Weight = " << weight << '\n' << "Height = " << height; - return ss.str(); -} - -string Bird::about() const { - stringstream ss; - ss << Animal::about() << '\n' << "Ability to fly = " << fly << '\n' << "Wingspan =" << wingspan; - return ss.str(); -} - -string Penguin::about() const { - stringstream ss; - ss << Bird::about() << '\n' << "Sex = " << sex; - return ss.str(); -} - -string Parrot::about() const { - stringstream ss; - ss << Bird::about() << '\n' << "Ability to speak = " << speak; - return ss.str(); -} - -string Mammal::about() const { - stringstream ss; - ss << Animal::about() << '\n' << "Duration of pregnancy = " << pregnancyDuration << '\n' << "Age =" << age; - return ss.str(); -} - -string Dog::about() const { - stringstream ss; - ss << Mammal::about() << '\n' << "Color of wool = " << woolcolor; - return ss.str(); -} - -string Human::about() const { - stringstream ss; - ss << Mammal::about() << '\n' << "IQ = " << iq; - return ss.str(); -} - int main() { - Penguin Skipper; - Skipper.weight = 3; - Skipper.height = 0.4; - Skipper.fly = false; - Skipper.wingspan = 40; - Skipper.sex = "Male"; - cout << "Skipper : " << Skipper.about() << endl << endl; + Penguin Skipper(3, 0.4, false, 40, "Male"); + cout << "Skipper: " << Skipper.about() << endl << endl; + + Parrot Kesha(0.04, 0.18, true, 30, true); + cout << "Kesha: " << Kesha.about() << endl << endl; - Parrot Kesha; - Kesha.weight = 0.04; - Kesha.height = 0.18; - Kesha.fly = true; - Kesha.wingspan = 30; - Kesha.speak = true; - cout << "Kesha : " << Kesha.about() << endl << endl; + Dog Masyanya(30, 0.57, 63, 10, "White"); + cout << "Masyanya: " << Masyanya.about() << endl << endl; - Dog Masyanya; - Masyanya.weight = 30; - Masyanya.height = 0.57; - Masyanya.pregnancyDuration = 63; - Masyanya.age = 10; - Masyanya.woolcolor = "White"; - cout << "Masyanya : " << Masyanya.about() << endl << endl; + Human Vasilisa(50, 1.70, 280, 30, 126); + cout << "Vasilisa: " << Vasilisa.about() << endl << endl; - Human Vasilisa; - Vasilisa.weight = 50; - Vasilisa.height = 1.70; - Vasilisa.pregnancyDuration = 280; - Vasilisa.age = 30; - Vasilisa.iq = 126; - cout << "Vasilisa : " << Vasilisa.about() << endl; + return 0; } \ No newline at end of file diff --git a/animals/animal.h b/animals/animal.h index f644a51e..820ae4cc 100644 --- a/animals/animal.h +++ b/animals/animal.h @@ -5,61 +5,106 @@ using namespace std; class Animal { -public: +private: float weight; // kg float height; // meters +protected: + Animal() {}; + Animal(float weight_, float height_) { weight = weight_; height = height_; } +public: + float getWeight() const { return weight; } + void setWeight(float newWeight) { weight = newWeight; } + float getHeight() const { return height; } + void setHeight(float newHeight) { height = newHeight; } - virtual std::string about() const; + virtual string about() const { return (stringstream() << "Weight = " << weight << '\n' << "Height = " << height).str(); } }; class Mammal : public Animal { -public: +private: float pregnancyDuration; // days float age; //years +protected: + Mammal() {}; + Mammal(float weight_, float height_, float pregnancyDuration_, float age_) :Animal(weight_, height_) { pregnancyDuration = pregnancyDuration_; age = age_; }; +public: + float getPregnancyDuration() const { return pregnancyDuration; } + void setPregnancyDuration(float newPregnancyDuration) { pregnancyDuration = newPregnancyDuration; } + float getAge() const { return age; } + void setAge(float newAge) { age = newAge; } - virtual std::string about() const; + virtual string about() const { return (stringstream() << Animal::about() << '\n' << "Duration of pregnancy = " << pregnancyDuration << '\n' << "Age = " << age).str(); } }; class Dog : public Mammal { -public: +private: string woolcolor; //color +public: + Dog() {}; + Dog(float weight_, float height_, float pregnancyDuration_, float age_, string woolcolor_) :Mammal(weight_, height_, pregnancyDuration_, age_) { + woolcolor = woolcolor_; + } - virtual std::string about() const; + string getWoolcolor() const { return woolcolor; } + void setWoolcolor(string newWoolcolor) { woolcolor = newWoolcolor; } + + virtual string about() const { return (stringstream() << Mammal::about() << '\n' << "Woolcolor = " << woolcolor).str(); } }; class Human : public Mammal { -public: +private: int iq; //scores - - virtual std::string about() const; +public: + Human() {}; + Human(float weight_, float height_, float pregnancyDuration_, float age_, int iq_) :Mammal(weight_, height_, pregnancyDuration_, age_) { + iq = iq_; + } + int getIQ() const { return iq; } + void setIQ(int newIQ) { iq = newIQ; } + + virtual string about() const { return (stringstream() << Mammal::about() << '\n' << "IQ = " << iq).str(); } }; class Bird : public Animal { -public: +private: bool fly; // true/false float wingspan; //centimeters +protected: + Bird() {}; + Bird(float weight_, float height_, bool fly_, float wingspan_) :Animal(weight_, height_) { fly = fly_; wingspan = wingspan_; }; +public: + bool getFly() const { return fly; } + void setFly(bool newFly) { fly = newFly; } + float getWingspan() const { return wingspan; } + void setWingspan(float newWingspan) { wingspan = newWingspan; } - virtual std::string about() const; + virtual string about() const { return (stringstream() << Animal::about() << '\n' << "Fly = " << fly << '\n' << "Wingspan = " << wingspan).str(); } }; class Penguin : public Bird { -public: +private: string sex; //male/female - - virtual std::string about() const; +public: + Penguin() {}; + Penguin(float weight_, float height_, bool fly_, float wingspan_, string sex_) :Bird(weight_, height_, fly_, wingspan_) { + sex = sex_; + } + string getSex() const { return sex; } + void setSex(string newSex) { sex = newSex; } + + virtual string about() const { return (stringstream() << Bird::about() << '\n' << "Sex = " << sex).str(); } }; class Parrot : public Bird { -public: +private: bool speak; //true/false - - virtual std::string about() const; -}; - -inline std::ostream& operator <<(std::ostream& os, const Penguin& penguin) { - return os << penguin.about(); -} - -inline std::ostream& operator <<(std::ostream& os, const Parrot& parrot) { - return os << parrot.about(); -} \ No newline at end of file +public: + Parrot() {}; + Parrot(float weight_, float height_, bool fly_, float wingspan_, bool speak_) :Bird(weight_, height_, fly_, wingspan_) { + speak = speak_; + } + bool getSpeak() const { return speak; } + void setSpeak(bool newSpeak) { speak = newSpeak; } + + virtual string about() const { return (stringstream() << Bird::about() << '\n' << "Speak " << speak).str(); } +}; \ No newline at end of file From c2a1842e17979005f03c774a96991d33e33e7f12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=BB=D0=B8=D0=B7=D0=B0=D0=B2=D0=B5=D1=82=D0=B0=20?= =?UTF-8?q?=D0=A1=D0=B8=D0=B4=D0=BE=D1=80=D0=BE=D0=B2=D0=B0?= <2003sev@mail.ru> Date: Mon, 13 Jun 2022 14:57:39 +0300 Subject: [PATCH 3/4] Vector --- vectors/vector.cpp | 108 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 91 insertions(+), 17 deletions(-) diff --git a/vectors/vector.cpp b/vectors/vector.cpp index 9777069c..fda386c4 100644 --- a/vectors/vector.cpp +++ b/vectors/vector.cpp @@ -1,25 +1,99 @@ #include -#include "vector.h" - +#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) const { return data[idx]; } + float& operator[] (int idx) { return data[idx]; } + 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; - //vector3d v4 = v1 + v2, v5 = v1 - v2, v6 = v1 * 0.5f; - //cout << "v4: " << v4 << endl << "v5: " << v5 << endl << "v6: " << v6 << endl; - - 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])); - - return 0; +bool test_vector3d() { + vector3d v1; + vector3d v2(3, 3, 3); + vector3d v3(4, 4, 4); + vector3d v4 = v2 + v3, v5 = v3 - v2, v6 = v2 * 4, v7 = v3 / 2, v8 = -v3, v9 = !v3, v10 = !v1; + + if ((v4[0] != 7) || (v4[1] != 7) || (v4[2] != 7)) { + cerr << "Операция сложения векторов повела себя неверно с данными - v2(3,3,3), v3(4,4,4)"; + return false; + } + + if ((v5[0] != 1) || (v5[1] != 1) || (v5[2] != 1)) { + cerr << "Операция разности векторов повела себя неверно с данными - v3(4,4,4), v2(3,3,3)"; + return false; + } + + if ((v6[0] != 12) || (v6[1] != 12) || (v6[2] != 12)) { + cerr << "Операция умножения вектора на скаляр повела себя неверно с данными - v2(3,3,3), 4"; + return false; + } + + if ((v7[0] != 2) || (v7[1] != 2) || (v7[2] != 2)) { + cerr << "Операция деления вектора на скаляр повела себя неверно с данными - v3(4,4,4), 2"; + return false; + } + + if ((v8[0] != -4) || (v8[1] != -4) || (v8[2] != -4)) { + cerr << "Операция инвертирования знака повела себя неверно с данными - v3(4,4,4)"; + return false; + } + + if ((v9[0] != 0) || (v9[1] != 0) || (v9[2] != 0)) { + cerr << "Унарный ! повел себя неверно с данными - v3(4,4,4)"; + return false; + } + + if ((v10[0] != 1) || (v10[1] != 1) || (v10[2] != 1)) { + cerr << "Унарный ! повел себя неверно с данными - v1(0,0,0)"; + return false; + } + return true; +} + +int main() { + setlocale(LC_ALL, "RUS"); + if (test_vector3d() == true) + { + return 0; + } + else + { + return 1; + }; } From 8b48da4810ab05a5627b20968893952968a7b8e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=BB=D0=B8=D0=B7=D0=B0=D0=B2=D0=B5=D1=82=D0=B0=20?= =?UTF-8?q?=D0=A1=D0=B8=D0=B4=D0=BE=D1=80=D0=BE=D0=B2=D0=B0?= <2003sev@mail.ru> Date: Wed, 15 Jun 2022 22:02:48 +0300 Subject: [PATCH 4/4] =?UTF-8?q?=D0=98=D0=B3=D1=80=D1=8B=20=D1=81=20=D0=BF?= =?UTF-8?q?=D0=B0=D0=BC=D1=8F=D1=82=D1=8C=D1=8E=20=E2=84=961?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- memhacks/memhacks.cpp | 21 ++++++++++++++++++--- memhacks/memhacks.h | 11 +++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/memhacks/memhacks.cpp b/memhacks/memhacks.cpp index 514c8689..64024d1a 100644 --- a/memhacks/memhacks.cpp +++ b/memhacks/memhacks.cpp @@ -27,7 +27,7 @@ void printInternals(const B& b) { /// /// Значение B::b_s std::string A::getBString() const { - // TODO + return *((const string*)(this + 1)); } /// @@ -37,7 +37,15 @@ std::string A::getBString() const { /// Подразумевается, что текущий объект РЅР° самом деле представлено классом . /// void A::printData(std::ostream& os) { - // TODO + os << "A::a_s: " << a_s << "\n"; + os << "B::b_s: " << A::getBString() << "\n"; + os << "B::data: "; + + const float* bData = ((const float*)(((const std::string*)(this + 1)) + 1)); + for (int i = 0; i < 7; ++i) { + os << bData[i] << " "; + } + os << "\n"; } /// @@ -46,7 +54,14 @@ void A::printData(std::ostream& os) { /// СЃ помощью виртуальных функций, предусмотренных РІ классе . /// void A::printData2(std::ostream& os) { - // TODO + os << "A::a_s: " << *A::getString() << "\n"; + os << "B::b_s: " << *getString() << "\n"; + + const float* bData = getData(); + for (int i = 0; i < 7; ++i) { + os << bData[i] << " "; + } + os << "\n"; } int main() diff --git a/memhacks/memhacks.h b/memhacks/memhacks.h index 51076b55..a8b2276d 100644 --- a/memhacks/memhacks.h +++ b/memhacks/memhacks.h @@ -15,6 +15,11 @@ class A { std::string getBString() const; void printData(std::ostream& os); void printData2(std::ostream& os); + + virtual const std::string* getString() const { + return &a_s; + }; + virtual const float* getData() const {} }; class B : public A { @@ -25,6 +30,12 @@ class B : public A { public: B(); + virtual const std::string* getString() const { + return &b_s; + } + virtual const float* getData() const { + return data; + } }; void printInternals(const B& b);