diff --git a/animals/animal.cpp b/animals/animal.cpp index 6745215..2219079 100644 --- a/animals/animal.cpp +++ b/animals/animal.cpp @@ -1,7 +1,121 @@ #include "animal.h" - +#include +#include +#include using namespace std; -int main() { - return 0; -} \ No newline at end of file +class Animal +{ + private: + int Age; //Years + public: + float height; //Meters + void setAge(int getAge) { Age = NewAge; } + int getAge() const { return Age; } + virtual string about() const; +}; +string Animal::about() const +{ + stringstream ss; + ss << "Age = " << " " << Age; + return ss.str(); +} + +class Mammal : public Animal +{ + private: + float PawLength; //centimeters + public: + void setPawLength(float NewPawLength) { PawLength = NewPawLength; } + float getPawLength() const { return PawLength; } + virtual string about() const; +}; +string Mammal::about() const +{ + stringstream ss; + ss << Animal::about() << " " << "PawLength = " << " " << PawLength; + return ss.str(); +} + +class Bird : public Animal +{ + private: + int FlightDuration; //minutes + public: + float BeakSize; // days + void setFlightDuration(int newFlightDuration) { FlightDuration = newFlightDuration; } + int getFlightDuration() const { return FlightDuration; } + virtual string about() const; +}; +string Bird::about() const +{ + stringstream ss; + ss << Animal::about() << " " << "FlightDuration = " << " " << FlightDuration; + return ss.str(); +} + +class Capybara : public Mammal +{ + private: + float FrontTeethLength; //centimeters + public: + void setFrontTeethLength(int newFrontTeethLength) { FrontTeethLength = newFrontTeethLength; } + int getFrontTeethLength() const { return FrontTeethLength; } + virtual string about() const; +}; +string Capybara::about() const +{ + stringstream ss; + ss << Mammal::about() << " " << "FrontTeethLength = " << " " << FrontTeethLength; + return ss.str(); +} + +class Caracal : public Mammal +{ + private: + int TailLength; //centimeters + public: + void setTailLength(int newTailLength) { TailLength = newTailLength; } + int getTailLength() const { return TailLength; } + virtual string about() const; +}; +string Caracal::about() const +{ + stringstream ss; + ss << Mammal::about() << " " << "TailLength = " << " " << TailLength; + return ss.str(); +} + +class Raven : public Bird +{ + private: + int WingLength; //centimeters + public: + void setWingLength(int newWingLength) { WingLength = newWingLength; } + int getWingLength() const { return WingLength; } + virtual string about() const; +}; +string Raven::about() const +{ + stringstream ss; + ss << Bird::about() << " " << "WingLength = " << " " << WingLength; + return ss.str(); +} + +class Ostrich : public Bird +{ + private: + float EggWeight; //kg + public: + void setEggWeight(float newEggWeight) { EggWeight = newEggWeight; } + float getEggWeight() const { return EggWeight; } + virtual string about() const; + +}; +string Ostrich::about() const +{ + stringstream ss; + ss << Bird::about() << " " << "EggWeight = " << " " << EggWeight; + return ss.str(); +} + diff --git a/memhacks/memhacks.cpp b/memhacks/memhacks.cpp index 514c868..945856e 100644 --- a/memhacks/memhacks.cpp +++ b/memhacks/memhacks.cpp @@ -1,4 +1,4 @@ -#include +#include #include "memhacks.h" using namespace std; @@ -55,3 +55,16 @@ int main() printInternals(b); return 0; } + +int main() +{ + A a; + B b; + cout << &a << "\n" << &b << "\n"; + printInternals(b); + cout << "MEMHACKS:" << endl; + b.printData(cout); + cout << endl << "STANDART:" << endl; + b.printData2(cout); + return 0; +} diff --git a/memhacks/memhacks.h b/memhacks/memhacks.h index 51076b5..cacc453 100644 --- a/memhacks/memhacks.h +++ b/memhacks/memhacks.h @@ -1,30 +1,90 @@ -#pragma once - +#pragma once +using namespace std; #include #include +#include +#include class B; // чтобы можно было объявить printInternals() как friend в классе A +class B; -class A { +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 + 1) - 12)); } + + string getBString() const { + return *((const string*)(this + 1)); + } + + + float getdataFloat(int i) { return ((float*)(this + 2) - 4)[i]; } + + virtual string about_A() const { + stringstream ss; + ss << "String A: " << a_s; + return ss.str(); + } }; -class B : public A { + + +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 (auto i = 0; i < sizeof(data) / sizeof(data[0]); i++) + data[i] = (float)i * 2; + } + + virtual string about_B() { + stringstream ss; + ss << "String B: " << b_s << endl; + ss << "Data : "; + for (int i = 0; i < 7; i++) { + ss << data[i] << "; "; + } + cout << endl; + return ss.str(); + } + + void printData2(std::ostream& os) { + os << about_A(); + os << about_B(); + } + void printData(std::ostream& os) { + os << "A string is '" << getAString() << "', B string is '" << getBString() << "'" << endl; + for (int i = 0; i < 7; ++i) os << getdataFloat(i) << " "; + } + }; 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(std::cout); cout << endl; + cout << "B data: "; b.printData2(std::cout); cout << endl; +} diff --git a/vectors/vector.cpp b/vectors/vector.cpp index 9777069..c7b1059 100644 --- a/vectors/vector.cpp +++ b/vectors/vector.cpp @@ -1,25 +1,185 @@ #include #include "vector.h" +#include +#include using namespace std; -ostream& operator <<(ostream& os, const vector3d& v) { - return os << "{ " << v[0] << ", " << v[1] << ", " << v[2] << " }"; +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; +} + +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); } -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; +vector3d operator + (const vector3d & v1, const vector3d & v2) +{ + return vector3d(v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]); +} - 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])); +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); +} + +const vector3d operator -(const vector3d & v1) +{ + return vector3d(-v1[0], -v1[1], -v1[2]); +} + +const vector3d operator !(const vector3d & v1) +{ + if (v1[0] == 0 && v1[1] == 0 && v1[2] == 0) + { + return vector3d(1, 1, 1); + } + else + return vector3d(0, 0, 0); +} + +bool sum_test(vector3d& v1, vector3d& v2) +{ + bool flag = true; + vector3d v3 = v1 + v2; + for (int i = 0; i < 3; ++i) { + if ((v3[0] == (v1[0] + v2[0])) && (v3[1] == (v1[1] + v2[1])) && (v3[2] == (v1[2] + v2[2]))) + cerr << "Сложение векторов сделано верно " << endl; + else { + cerr << "Сложение векторов сделано неверно" << endl; + flag = false; + } + break; + } + return flag; +} + +bool sub_test(vector3d& v1, vector3d& v2) +{ + bool flag = true; + vector3d v3 = v1 - v2; + for (int i = 0; i < 3; ++i) { + if ((v3[0] == (v1[0] - v2[0])) && (v3[1] == (v1[1] - v2[1])) && (v3[2] == (v1[2] - v2[2]))) + cerr << "Вычитание векторов сделано верно" << endl; + else { + cerr << "Вычитание векторов сделано неверно" << endl; + flag = false; + } + break; + } + return flag; +} + +bool mult_test(vector3d& v1, float& c1) +{ + bool flag = true; + vector3d v3 = v1 * c1; + for (int i = 0; i < 3; ++i) { + if ((v3[0] == (v1[0] * c1)) && (v3[1] == (v1[1] * c1)) && (v3[2] == (v1[2] * c1))) + cerr << "Умножение вектора на скаляр выполнено верно" << endl; + else { + cerr << "Умножение вектора на скаляр выполнено неверно" << endl; + flag = false; + } + break; + } + return flag; +} + +bool div_test(vector3d& v1, float& c1) +{ + bool flag = true; + vector3d v3 = v1 / c1; + for (int i = 0; i < 3; ++i) { + if ((v3[0] == (v1[0] / c1)) && (v3[1] == (v1[1] / c1)) && (v3[2] == (v1[2] / c1))) + cerr << "Деление вектора на число выполнено верно" << endl; + else { + cerr << "Деление вектора на число выполнено неверно" << endl; + flag = false; + } + break; + } + return flag; +} + +bool minus_test(vector3d& v1) +{ + bool flag = true; + vector3d v3 = -v1; + for (int i = 0; i < 3; ++i) { + if ((v3[0] == -v1[0]) && (v3[1] == -v1[1]) && (v3[2] == -v1[2])) + cerr << "Инвертирование знака выполнено верно" << endl; + else { + cerr << "Инвертирование знака выполнено неверно" << endl; + flag = false; + } + break; + } + return flag; +} + +bool unar!_test() +{ + bool flag = true; + vector3d v1 = (0, 0, 0); + vector3d v3 = !v1; + for (int i = 0; i < 3; ++i) { + if ((v3[0] == 1) && (v3[1] == 1) && (v3[2] == 1)) + cerr << "Логическое отрицание выполнено верно" << endl; + else { + cerr << "Логическое отрицание выполнено неверно" << endl; + flag = false; + } + break; + } + return flag; +} - return 0; +bool test_vector3d(vector3d v1, vector3d v2, float c1) +{ + bool flag = true; + sum_test(v1, v2); + sub_test(v1, v2); + mult_test(v1, c1); + div_test(v1, c1); + minus_test(v1); + unar!_test(); + return flag; }