diff --git a/animals/animal.cpp b/animals/animal.cpp index 6745215e..f7377573 100644 --- a/animals/animal.cpp +++ b/animals/animal.cpp @@ -1,7 +1,73 @@ -#include "animal.h" - +#include +#include using namespace std; -int main() { - return 0; -} \ No newline at end of file +class Animal +{ +private: + float height; +protected: + Animal(float _height); +public: + float getHeight() const { return height; } + void setHeight(float newValue) { height = newValue; } + virtual string about() const; +}; +ostream& operator <<(ostream& os, const Animal& animal); +class Mammal : public Animal { +private: + float duration_of_pregnancy; +protected: + Mammal(float _height, float _duration_of_pregnancy); +public: + float getduration_of_pregnancy() const { return duration_of_pregnancy; } + void setduration_of_pregnancy(float newValue) { duration_of_pregnancy = newValue; } + virtual string about() const; +}; +class birds : public Animal { +private: + float feather_lenght ; +protected: + birds(float _height, float _feather_lenght); +public: + bool getfeather_lenght() const { return feather_lenght; } + void setfeather_lenght(float newValue) { feather_lenght = newValue; } + virtual string about() const; +}; +class Dog :public Mammal { +private: + float ear_lenght; +public: + Dog(float _height, float _duration_of_pregnancy, float ear_lenght); + float getear_lenght() const { return ear_lenght; } + void setear_lenght(float newValue) { ear_lenght = newValue; } + virtual string about() const; +}; +class Cat :public Mammal { +private: + float vibrissaLength; +public: + Cat(float _height, float _duration_of_pregnancy, float _vibrissaLength); + float getvibrissaLength() const { return vibrissaLength;} + void setvibrissaLength(float newValue) { vibrissaLength = newValue; } + virtual string about() const; +}; + +class Martlet : public birds { +private: + float max_flight_altitude; +public: + Martlet(float _height, float _feather_lenght, float _max_flight_altitude); + float getmax_flight_altitude() const { return max_flight_altitude; } + void setmax_flight_altitude(float newValue) { max_flight_altitude = newValue; } + virtual string about() const; +}; +class goose :public birds { +private: + float max_flight_duration; +public: + goose(float _height, float _feather_lenght, float _max_flight_duration); + float getmax_flight_duration() const { return max_flight_duration; } + void setmax_flight_duration(float newValue) { max_flight_duration = newValue; } + virtual string about() const; +}; diff --git a/electricity/electricity.cpp b/electricity/electricity.cpp index 9f1c0170..29782bc0 100644 --- a/electricity/electricity.cpp +++ b/electricity/electricity.cpp @@ -5,6 +5,12 @@ using namespace std; bool Object::isConnectedTo(const Object& other) const { + for (int i = 0; i < getPoleCount(); ++i) + { + auto pole = getPole(i); + if (pole != nullptr && pole->connectedObject == &other) + return true; + } // TODO return false; } @@ -12,17 +18,48 @@ bool Object::isConnectedTo(const Object& other) const bool Object::connect(const std::string& poleName, const Object& other, const std::string& otherPoleName) { // TODO + if (poleName == otherPoleName && &other == this) return false; + auto pole = getPole(poleName); + auto otherPole = other.getPole(otherPoleName); + if (pole == nullptr || otherPole == nullptr) return false; + otherPole->connectedObject = this; + otherPole->connectedObjectPole = poleName; + pole->connectedObject = (Object*)(&other); + pole->connectedObjectPole = otherPoleName; return false; } +bool Object::disconnect(const std::string &poleName) +{ + auto pole = getPole(poleName); + if (pole == nullptr || pole->connectedObjectPole.empty() || pole->connectedObject == nullptr) + return false; + auto otherPoleName = pole->connectedObjectPole; + auto otherPole = pole->connectedObject->getPole(otherPoleName); + pole->connectedObjectPole = ""; + pole->connectedObject = nullptr; + otherPole->connectedObjectPole = ""; + otherPole->connectedObject = nullptr; + return true; +} +Switch::Switch(const std::string& name) : Object(name), a1("A1"), a2("A2") {} -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(const string& name) const +const Pole* Switch::getPole(size_t idx) const +{ + return getPole("A" + to_string(idx + 1)); +} + +Light::Light(const string &name) : Object(name), a1("A1"), a2("A2") {} + +const Pole *Light::getPole(const string &name) const { if (name == a1.name) return &a1; @@ -31,19 +68,44 @@ const Pole* Switch::getPole(const string& name) const return nullptr; } -const Pole* Switch::getPole(size_t idx) const +const Pole *Light::getPole(size_t idx) const { - // TODO + 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; } -int main() +const Pole *Generator::getPole(size_t idx) const { - Switch sw, sw2; - sw.connect("A2", sw2, "A1"); - cout << "is " << (sw.isConnectedTo(sw2) ? "" : "not ") << "connected" << endl; + return getPole("A" + to_string(idx + 1)); +} + +int main() +{ + Generator gen = Generator("Generator 1"); + Switch sw = Switch("Switch 1"); + Light light = Light("Light 1"); + + gen.connect("A1", sw, "A1"); + sw.connect("A2", light, "A1"); + gen.connect("A2", light, "A2"); + + cout << "is " << (gen.isConnectedTo(sw) ? "" : "not ") << "connected" << endl; + + gen.disconnect("A1"); - // TODO: создать цепь из генератора, выключателя и светильника + cout << "is " << (gen.isConnectedTo(sw) ? "" : "not ") << "connected" << endl; return 0; } diff --git a/electricity/electricity.h b/electricity/electricity.h index f7df29ca..30410b7b 100644 --- a/electricity/electricity.h +++ b/electricity/electricity.h @@ -1,4 +1,4 @@ -#pragma once +#pragma once #include class Object; @@ -108,17 +108,36 @@ class Object { /// Простой выключатель с двумя полюсами. /// class Switch : public Object { + public: Pole a1, a2; - Switch(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; +}; - virtual size_t getPoleCount() const { return 2; } - - virtual const Pole* getPole(const std::string& name) const; +class Light : public Object { + +public: + Pole a1, a2; + Light(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; +}; +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: - virtual const Pole* getPole(size_t idx) const; + const Pole* getPole(size_t idx) const override; }; // TODO: класс светильника с двумя полюсами diff --git a/memhacks/memhacks.cpp b/memhacks/memhacks.cpp index 514c8689..c2f546c4 100644 --- a/memhacks/memhacks.cpp +++ b/memhacks/memhacks.cpp @@ -1,11 +1,13 @@ -#include -#include "memhacks.h" +#include +#include "Header.h" using namespace std; +A::A() : a_s("It's a!"), foo(0) {} -B::B() : b_s("It's b!") { +B::B() : b_s("It's b!") +{ for (auto i = 0; i < sizeof(data) / sizeof(data[0]); i++) - data[i] = i * 2; + data[i] = (float)i * 2; } /// @@ -13,12 +15,14 @@ B::B() : b_s("It's b!") { /// Можно модифицировать для собственных отладочных целей. /// /// Изучаемый объект -void printInternals(const B& b) { +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: "; b.printData(cout); cout << endl; + cout << "B data "; a->printData(cout); cout << endl; + cout << "B data "; a->printData2(cout); cout << endl; } /// @@ -26,18 +30,32 @@ void printInternals(const B& b) { /// Подразумевается, что текущий объект на самом деле представлено классом . /// /// Значение B::b_s -std::string A::getBString() const { - // TODO +std::string A::getBString() const +{ + return dynamic_cast(this)->getBString(); +} +std::string B::getBString() const +{ + return b_s; +} +float B::getData(int idx) const +{ + return data[idx]; +} +float A::getData(int idx) const +{ + return dynamic_cast(this)->getData(idx); } - /// /// Извлекает значения , и /// из текущего объекта и выводит их в текстовом виде в указанный выходной поток /// с помощью адресной арифметики. /// Подразумевается, что текущий объект на самом деле представлено классом . /// -void A::printData(std::ostream& os) { - // TODO +void A::printData(std::ostream& os) const +{ + os << "(using printData): A string is '" << a_s << "', B string is '" << getBString() << "'" << endl; + for (int i = 0; i < 7; ++i) os << getData(i) << " "; } /// @@ -45,8 +63,11 @@ void A::printData(std::ostream& os) { /// из текущего объекта и выводит их в текстовом виде в указанный выходной поток /// с помощью виртуальных функций, предусмотренных в классе . /// -void A::printData2(std::ostream& os) { - // TODO +void A::printData2(std::ostream& os) const +{ + B b = *dynamic_cast(this); + os << "(using printData2): A string is '" << a_s << "', B string is '" << b.getBString() << "'" << endl; + 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..1e1f6119 100644 --- a/memhacks/memhacks.h +++ b/memhacks/memhacks.h @@ -1,4 +1,4 @@ -#pragma once +#pragma once #include #include @@ -9,22 +9,29 @@ class A { std::string a_s; int foo; - friend void printInternals(const B&); + friend void printInternals(const B& b); public: - std::string getBString() const; - void printData(std::ostream& os); - void printData2(std::ostream& os); + 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]; + float data[7]{}; - friend void printInternals(const B&); + friend void printInternals(const B& b); public: B(); + + float getData(int idx) const override; + std::string getBString() const override; }; void printInternals(const B& b); diff --git a/vectors/array.cpp b/vectors/array.cpp index 8f6656da..7b8c4692 100644 --- a/vectors/array.cpp +++ b/vectors/array.cpp @@ -1,45 +1,23 @@ -#include -using namespace std; +#include "array.h" -class Vector3d { - float data[3]; -public: - Vector3d() { data[2] = data[1] = data[0] = 0; } - Vector3d(float value) { data[2] = data[1] = data[0] = value; } - float operator[] (int index) const { cerr << "const" << index << endl; return data[index]; } - float& operator[] (int index) { cerr << "non-const" << index << endl; return data[index]; } -}; +template +vector average(const vector a, const vector b) { + vector temp; + for (int i = 0; i < dim; ++i) temp[i] = (a[i] + b[i]) / 2; + return temp; +} -template -class Vector { - float data[Dimensions]; -public: - Vector(float value = 0) { for (size_t i = 0; i < Dimensions; i++) data[i] = value; } - float operator[] (size_t index) const { return data[index]; } - float& operator[] (size_t index) { return data[index]; } -}; +int main() { + vector<5> vec_1; -// Написать шаблонные функции: -// 1. operator + (реализация внутри класса) -// 2. operator * (вектор на скаляр и скаляр на вектор; реализация вне класса) -// 3. Нахождение среднего для двух векторов (реализация вне класса) + for (int i = 0; i < vec_1.size(); ++i) vec_1[i] = (float)i; -ostream& operator <<(ostream& os, const Vector3d& v) { - return os << "{ " << v[0] << ", " << v[1] << ", " << v[2] << " }"; -} + vector<5> vec_2 = vec_1 * 3; + vector<5> vec_3 = vec_1 * 4; -template -ostream& operator <<(ostream& os, const Vector& v) { - os << "{ " << v[0]; - for (size_t i = 1; i < Dimensions; i++) - os << ", " << v[i]; - return os << " }"; -} + std::cout << "vec_1 * 3: " << vec_2 << std::endl; + std::cout << "vec_1 * 4: " << vec_3 << std::endl; + std::cout << "average of vec_2 and vec_3: " << average(vec_2, vec_3) << std::endl; -int main() { - Vector<12> v1; - v1[1] = v1[2] = 54; - Vector<12> v2 = v1; - cout << v2 << endl; - return 0; + return 0; } diff --git a/vectors/array.h b/vectors/array.h index 6f70f09b..e964817f 100644 --- a/vectors/array.h +++ b/vectors/array.h @@ -1 +1,39 @@ #pragma once +#include + +template +class vector { + float data[dim] {}; +public: + vector(float value = 0) { for (auto &d: data) d = value; } + + constexpr size_t size() { return dim; } + + float operator[] (int idx) const { return data[idx]; } + float& operator[] (int idx) { return data[idx]; } + + vector operator+ (const vector &v) const { + vector temp; + for (int i = 0; i < dim; ++i) temp[i] = data[i] + v[i]; + return temp; + } +}; + +template +vector operator* (vector v, float f) { + vector temp; + for (int i = 0; i < dim; ++i) temp[i] = v[i] * f; + return temp; +} + +template +vector operator* (float f, vector v) { + return v * f; +} + +template +std::ostream& operator <<(std::ostream &os, const vector &v) { + os << "{" << v[0]; + for (int i = 1; i < dim; ++i) os << ", " << v[i]; + return os << "}"; +} diff --git a/vectors/vector.cpp b/vectors/vector.cpp index 9777069c..f40cc444 100644 --- a/vectors/vector.cpp +++ b/vectors/vector.cpp @@ -1,18 +1,142 @@ -#include -#include "vector.h" - +#include +#include "Header.h" 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] << "}"; +} +vector3d operator-(const vector3d& v) +{ + vector3d res_v; + for (int i = 0; i < 3; ++i) + { + res_v[i] = -v[i]; + } + return res_v; +} +vector3d operator+(const vector3d& v1, const vector3d& v2) +{ + vector3d res_v; + for (int i = 0; i < 3; ++i) + { + res_v[i] = v1[i] + v2[i]; + } + return res_v; +} +vector3d operator -(const vector3d& v1, const vector3d& v2) +{ + return v1 + (-v2); } -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& v, float n) +{ + vector3d res_v; + for (int i = 0; i < 3; ++i) + { + res_v[i] = v[i] * n; + } + + return res_v; +} +vector3d operator * (float n, const vector3d& v) +{ + return v * n; +} + +vector3d operator /(const vector3d& v, float n) +{ + return v * (1 / n); +} + +vector3d operator /(float n, const vector3d& v) +{ + return v / n; +} + +vector3d operator !(const vector3d& v) +{ + return (v[0] == v[1] && v[1] == v[2] && v[2] == 0 ? vector3d(1, 1, 1) : vector3d(0, 0, 0)); +} +bool operator !=(const vector3d& v1, const vector3d& v2) +{ + bool flag = false; + + for (int i = 0; i < 3; ++i) + { + if (v1[i] != v2[i]) + { + flag = true; + break; + } + } + + return flag; +} +bool test_vector3d() { + vector3d v1 = vector3d(50, 4, 40); + vector3d v2 = vector3d(39, 20, 1); + + bool flag = false; + vector3d right; + + right[0] = 50 + 39; + right[1] = 4 + 20; + right[2] = 40 + 1; + if ((v1 + v2) != right) { + flag = true; + cerr << "v1 + v2 = " << v1 + v2 << ", should be " << right << endl; + } + + right[0] = 50 - 39; + right[1] = 4 - 20; + right[2] = 40 - 1; + if ((v1 - v2) != right) { + flag = true; + cerr << "v1 - v2 = " << v1 - v2 << ", should be " << right << endl; + } + + right[0] = 50 * 5.2; + right[1] = 4 * 5.2; + right[2] = 40 * 5.2; + if (v1 * 2.2 != right) { + flag = true; + cerr << "v1 * 5.2 = " << v1 * 5.2 << ", should be " << right << endl; + } + + right[0] = 50 / 5.2; + right[1] = 4 / 5.2; + right[2] = 40 / 5.2; + if (v1 / 2.2 != right) { + flag = true; + cerr << "v1 / 5.2 = " << v1 / 5.2 << ", should be " << right << endl; + } + + right[0] = -50; + right[1] = -4; + right[2] = -40; + if (-v1 != right) { + flag = true; + cerr << "-v1 = " << -v1 << ", should be " << right << endl; + } + + right[0] = 0; + right[1] = 0; + right[2] = 0; + if (!v1 != right) { + flag = true; + cerr << "!v1 = " << !v1 << ", should be " << right << endl; + } + + return !flag; +} +int main(int argc, char** argv) +{ + vector3d v1, v2(10), v3(3, 6, 9); + v1[2] = 32; + vector3d v4 = v1 + v2, v5 = v1 - v2, v6 = v1 * 0.5f, v7 = v1 / 0.5f, v8 = -v3, v9 = !v3; + cout << "v4: " << v4 << endl << "v5: " << v5 << endl << "v6: " << v6 << endl << "v7: " << v7 << endl << "v8: " << v8 << endl << "v9: " << v9 << 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])); @@ -21,5 +145,5 @@ int main(int argc, char** argv) { 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; + return !test_vector3d(); } diff --git a/vectors/vector.h b/vectors/vector.h index 07587fb3..7b9e1b43 100644 --- a/vectors/vector.h +++ b/vectors/vector.h @@ -1,19 +1,27 @@ #pragma once - -#include - -class vector3d { +#include +using namespace std; +class vector3d +{ float data[3]; -public: +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]; } + vector3d(float a1, float a2, float a3) { data[2] = a3, data[1] = a2, data[0] = a1; } + float& operator[](int idx) { return data[idx]; } + float operator[](int idx) const { return data[idx]; } friend int main(int argc, char** argv); }; +ostream& operator<<(ostream& os, const vector3d& v); +vector3d operator -(const vector3d& v); +vector3d operator -(const vector3d& v1, const vector3d& v2); +vector3d operator +(const vector3d& v1, const vector3d& v2); +vector3d operator *(const vector3d& v, float n); +vector3d operator *(float n, const vector3d& v); +vector3d operator /(const vector3d& v, float n); +vector3d operator /(float n, const vector3d& v); +vector3d operator !(const vector3d& v); -std::ostream& operator <<(std::ostream& os, const vector3d& v); +bool test_vector3d();