From e6584ca1f2fb6c95a9d2e24fe3e227a26069ca90 Mon Sep 17 00:00:00 2001 From: oksiishvts <90093543+oksiishvts@users.noreply.github.com> Date: Wed, 15 Jun 2022 22:41:56 +0300 Subject: [PATCH 1/4] Update animal.cpp --- animals/animal.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/animals/animal.cpp b/animals/animal.cpp index 6745215..feb6946 100644 --- a/animals/animal.cpp +++ b/animals/animal.cpp @@ -3,5 +3,5 @@ using namespace std; int main() { - return 0; -} \ No newline at end of file + return 0;// +} From 81e7ba12960c28ab3361cd00cab2795cd5e22067 Mon Sep 17 00:00:00 2001 From: oksiishvts <90093543+oksiishvts@users.noreply.github.com> Date: Wed, 15 Jun 2022 22:54:32 +0300 Subject: [PATCH 2/4] Update vector.cpp --- vectors/vector.cpp | 72 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 56 insertions(+), 16 deletions(-) diff --git a/vectors/vector.cpp b/vectors/vector.cpp index 9777069..68471d2 100644 --- a/vectors/vector.cpp +++ b/vectors/vector.cpp @@ -1,25 +1,65 @@ #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; } + // lvalue = rvalue + float operator[](int idx) const { return data[idx]; } + float& operator[](int idx) { return data[idx]; } + +}; + + 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; +vector3d operator + (const vector3d& v1, const vector3d& v2) { return vector3d(v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]); } +vector3d operator - (const vector3d& v1, const vector3d& v2) { return vector3d(v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2]); } +vector3d operator * (const vector3d& v1, const float x) { return vector3d(v1[0] * x, v1[1] * x, v1[2] * x); } +vector3d operator / (const vector3d& v1, const float x) { return vector3d(v1[0] / x, v1[1] / x, v1[2] / x); } +vector3d operator -(const vector3d& v1) { return vector3d(-v1[0], -v1[1], -v1[2]); } +vector3d operator !(const vector3d& v1) { + if (v1[0] == 0 and v1[1] == 0 and v1[2] == 0) { return vector3d(1, 1, 1); } + else return vector3d(0, 0, 0); } + +bool test_vector3d() { + vector3d v1(10, 85, 110); + vector3d v2(20, 65, 90); + + cout << "v1: " << "10, 85, 110" << endl; + cout << "v2: " << "20, 65, 90" << endl; + + bool ERROR = false; + + cout << "v1 + v2 = " << v1 + v2 << endl; + if ((v1 + v2)[0] != v1[0] + v2[0] or (v1 + v2)[1] != v1[1] + v2[1] or (v1 + v2)[2] != v1[2] + v2[2]) { + cerr << "invalid, should be " << "{ " << v1[0] + v2[0] << " " << v1[1] + v2[1] << " " << v1[2] + v2[2] << " }" << endl; + ERROR = true; + } + cout << "v1 - v2 = " << v1 - v2 << endl; + if ((v1 - v2)[0] != v1[0] - v2[0] or (v1 - v2)[1] != v1[1] - v2[1] or (v1 - v2)[2] != v1[2] - v2[2]) { + cerr << "invalid, should be " << "{ " << v1[0] - v2[0] << " " << v1[1] - v2[1] << " " << v1[2] - v2[2] << " }" << endl; + ERROR = true; + } + cout << "v1 * 5 = " << v1 * 5 << endl; + if ((v1 * 5)[0] != v1[0] * 5 or (v1 * 5)[1] != v1[1] * 5 or (v1 * 5)[2] != v1[2] * 5) { + cerr << "invalid, should be " << "{ " << v1[0] * 5 << " " << v1[1] * 5 << " " << v1[2] * 5 << " }" << endl; + ERROR = true; + } + cout << "v1 / 5 = " << v1 / 5 << endl; + if ((v1 / 5)[0] != v1[0] / 5 || (v1 / 5)[1] != v1[1] / 5 || (v1 / 5)[2] != v1[2] / 5) { + cerr << "invalid, should be " << "{ " << v1[0] / 5 << " " << v1[1] / 5 << " " << v1[2] / 5 << " }" << endl; + ERROR = true; + } + return ERROR; +} + +int main(int argc, char** argv) { return test_vector3d(); } From b9cdc597f0a42a3f75ee876e1c3fb8ff0515e506 Mon Sep 17 00:00:00 2001 From: oksiishvts <90093543+oksiishvts@users.noreply.github.com> Date: Wed, 15 Jun 2022 22:55:51 +0300 Subject: [PATCH 3/4] Update animal.cpp --- animals/animal.cpp | 193 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 189 insertions(+), 4 deletions(-) diff --git a/animals/animal.cpp b/animals/animal.cpp index feb6946..281ee90 100644 --- a/animals/animal.cpp +++ b/animals/animal.cpp @@ -1,7 +1,192 @@ -#include "animal.h" - +#include +#include +#include using namespace std; -int main() { - return 0;// + + +class Animal { +private: + float the_average_value_of_the_duration_of_life; +public: + void set_the_average_value_of_the_duration_of_life(float new_the_average_value_of_the_duration_of_life) { the_average_value_of_the_duration_of_life = new_the_average_value_of_the_duration_of_life; } + float get_the_average_value_of_the_duration_of_life() const { return the_average_value_of_the_duration_of_life; } + + bool Gender; + virtual string about() const; +protected: + Animal(); + +}; + +string Animal::about() const { + stringstream ss; + ss << "Gender = " << " " << Gender; + return ss.str(); +}; + +class Mammal : public Animal { +private: + int number_of_individuals; +public: + void set_number_of_individuals(int new_number_of_individuals) { number_of_individuals = new_number_of_individuals; } + int get_number_of_individuals() const { return number_of_individuals; } + + string Coat_color; + virtual string about() const; +protected: + Mammal(); +}; + +string Mammal::about() const { + stringstream ss; + ss << Animal::about() << " " << " Coat_color = " << " " << Coat_color; + return ss.str(); +}; + + + +class Quadrupeds : public Mammal { +private: + float percentage_of_quality_of_life; +public: + void set_percentage_of_quality_of_life(float new_percentage_of_quality_of_life) { percentage_of_quality_of_life = new_percentage_of_quality_of_life; } + float get_percentage_of_quality_of_life() const { return percentage_of_quality_of_life; } + + bool limbs; + virtual string about() const; +}; + +string Quadrupeds::about() const { + stringstream ss; + ss << Animal::about() << " " << " limbs = " << " " << limbs; + return ss.str(); +}; + + +class Birds : public Mammal { +private: + int the_average_value_of_the_flight_for_one_season; +public: + void set_the_average_value_of_the_flight_for_one_season(int new_the_average_value_of_the_flight_for_one_season) { the_average_value_of_the_flight_for_one_season = new_the_average_value_of_the_flight_for_one_season; } + int get_the_average_value_of_the_flight_for_one_season() const { return the_average_value_of_the_flight_for_one_season; } + + + bool ability_to_fly; + virtual string about() const; +}; + +string Birds::about() const { + stringstream ss; + ss << Mammal::about() << " " << " ability_to_fly = " << " " << ability_to_fly; + return ss.str(); +}; + +class Cat : public Animal { +private: + int the_number_of_mice_caught_per_unit_of_time; +public: + void set_the_number_of_mice_caught_per_unit_of_time(int new_the_number_of_mice_caught_per_unit_of_time) { the_number_of_mice_caught_per_unit_of_time = new_the_number_of_mice_caught_per_unit_of_time; } + int get_the_number_of_mice_caught_per_unit_of_time() const { return the_number_of_mice_caught_per_unit_of_time; } + + + float vibrissaLength; + virtual string about() const; +}; + +string Cat::about() const { + stringstream ss; + ss << Animal::about() << " " << " vibrissaLength = " << " " << vibrissaLength; + return ss.str(); +}; + +class Manul : public Cat { +private: + int average_weight; +public: + void set_average_weight(int new_average_weight) { average_weight = new_average_weight; } + int get_average_weight() const { return average_weight; } + + + float Average_length_of_wool; + virtual string about() const; +}; + +string Manul::about() const { + stringstream ss; + ss << Cat::about() << " " << " Average_length_of_wool = " << " " << Average_length_of_wool; + return ss.str(); +}; + +class Mainkun : public Cat { +private: + string eye_color; +public: + void set_eye_color(string new_eye_color) { eye_color = new_eye_color; } + string get_eye_color() const { return eye_color; } + + float Number_of_fleas; + virtual string about() const; +}; + +string Mainkun::about() const { + stringstream ss; + ss << Cat::about() << " " << " Number_of_fleas = " << " " << Number_of_fleas; + return ss.str(); +}; + + +Animal::Animal() + : Gender() + , the_average_value_of_the_duration_of_life() +{ + cerr << "" << endl; +} + +Mammal::Mammal() + : number_of_individuals() + , Coat_color() +{ + cerr << "" << endl; +} + +inline ostream& operator <<(ostream& os, const Animal& animal) { + return os << animal.about(); +} + +int main(){ + Mainkun kot_bOris; + Manul kot_Vasily; + Cat Roudi; + Birds ANgry_birds; + Quadrupeds dogs; + + kot_bOris.Gender = true; + kot_bOris.Number_of_fleas = 50000; + kot_bOris.vibrissaLength = 5; + + kot_Vasily.Average_length_of_wool = 3; + kot_Vasily.Gender = true; + kot_Vasily.vibrissaLength = 6; + + Roudi.Gender = true; + Roudi.vibrissaLength = 4; + + ANgry_birds.ability_to_fly = true; + ANgry_birds.Coat_color = "red"; + ANgry_birds.Gender = false; + + dogs.Coat_color = "red"; + dogs.Gender = true; + dogs.limbs = 4; + + cout << "-------------------------------------------------------------" << endl; + cout << "kot_bOris: " << kot_bOris << endl; + cout << "kot_Vasily: " << kot_Vasily << endl; + cout << "Roudi: " << Roudi << endl; + cout << "ANgry_birds: " << ANgry_birds << endl; + cout << "dogs: " << dogs << endl; + cout << "-------------------------------------------------------------"; + + } From 32ec29d5a8d1b4ed170c9f3bc839732f9230ce8b Mon Sep 17 00:00:00 2001 From: oksiishvts <90093543+oksiishvts@users.noreply.github.com> Date: Wed, 15 Jun 2022 22:57:17 +0300 Subject: [PATCH 4/4] Update memhacks.h --- memhacks/memhacks.h | 101 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 88 insertions(+), 13 deletions(-) diff --git a/memhacks/memhacks.h b/memhacks/memhacks.h index 51076b5..3f358e4 100644 --- a/memhacks/memhacks.h +++ b/memhacks/memhacks.h @@ -1,30 +1,105 @@ -#pragma once +#pragma once #include #include +#include +#include +using namespace std; -class B; // чтобы можно было объявить printInternals() как friend в классе A +class B; class A { - std::string a_s; + string a_s; int foo; - friend void printInternals(const B&); - + friend void printInternals(B&); public: - std::string getBString() const; - void printData(std::ostream& os); - void printData2(std::ostream& os); + A() : a_s("It's a!"), foo(0) { } + + string getAString() const { return *((const string*)((const float*)(this) + 2)); } + string getBString() const { + return *((const string*)(this + 1)); + } + + + + float getdataFloat(size_t i) { return ((float*)(this + 2) - 4)[i]; } + + virtual string aboutA() const { + stringstream ss; + ss << "String A: " << a_s; + return ss.str(); + } }; + + class B : public A { - std::string b_s; + string b_s; float data[7]; - friend void printInternals(const B&); - + friend void printInternals(B&); public: - B(); + B() : b_s("It's b!") { + for (size_t i = 0; i < sizeof(data) / sizeof(data[0]); i++) + data[i] = (float)i * 2; + } + /// + /// Извлекает значение из текущего объекта. + /// Подразумевается, что текущий объект на самом деле представлено классом . + /// + /// Значение B::b_s + + + virtual string aboutB() { + stringstream ss; + ss << "String B: " << b_s << endl; + ss << "Data : "; + for (size_t i = 0; i < sizeof(data) / sizeof(data[0]); i++) { ss << data[i] << "; "; } + cout << endl; + return ss.str(); + } + + + + + void printData2(ostream& os); + /// + /// Извлекает значения , и + /// из текущего объекта и выводит их в текстовом виде в указанный выходной поток + /// с помощью адресной арифметики. + /// Подразумевается, что текущий объект на самом деле представлено классом . + /// + void printData(ostream& os) { + os << "A string is '" << getAString() << " ', B string is ' " << getBString() << " ' " << endl; + for (size_t i = 0; i < sizeof(data) / sizeof(data[0]); ++i) os << getdataFloat(i) << " "; + } + }; -void printInternals(const B& b); +/// +/// Извлекает значения , и +/// из текущего объекта и выводит их в текстовом виде в указанный выходной поток +/// с помощью виртуальных функций, предусмотренных в классе . +/// +void B::printData2(ostream& os) { + os << aboutA(); + os << aboutB(); +} + + +/// +/// Выводит на экран адреса и размеры объекта типа и его содержимого. +/// Можно модифицировать для собственных отладочных целей. +/// +/// Изучаемый объект +void printInternals(B& b) { + const A* a = &b, * a2 = a; + cerr << "-------------------" << endl; + cerr << "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; + cerr << "Size of A is " << sizeof(A) << ", size of B is " << sizeof(B) << endl; + cerr << "B string is '" << b.getBString() << "'" << endl; + cerr << "B data: "; b.printData(cout); cerr << endl; + cerr << "B data: "; b.printData2(cout); cerr << endl; + cerr << "-------------------" << endl; +}