diff --git a/animals/animal.cpp b/animals/animal.cpp index 6745215e..32b925f7 100644 --- a/animals/animal.cpp +++ b/animals/animal.cpp @@ -1,7 +1,15 @@ #include "animal.h" - +#include +#include using namespace std; int main() { - return 0; + Cat Scottish(5); + Horse Spirit(1234); + Scottish.set_MiceCaughtCounter(7); + Spirit.setkilometresRun(1500.5); + int res = Scottish.get_MiceCaughtCounter(); + float res2 = Spirit.get_kilometresRun(); + cout << res << " " << res2 << "\n"; + return 0; } \ No newline at end of file diff --git a/animals/animal.h b/animals/animal.h index 83a19442..c518c783 100644 --- a/animals/animal.h +++ b/animals/animal.h @@ -1,18 +1,84 @@ #pragma once - #include +#include +using namespace std; + class Animal { +private: + bool CanMoveFreely; +protected: + Animal() { CanMoveFreely = 0; } + Animal(bool YesOrNo) { CanMoveFreely = YesOrNo; } +public: + + 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; +protected: + Mammal() { FeedsWithMilk = 0; } + Mammal(bool FeedOrNot) { FeedsWithMilk = FeedOrNot; } +public: + void setFeedsWithMilk(bool FeedOrNot) { FeedsWithMilk = FeedOrNot; } + bool get_FeedsWithMilk() const { return FeedsWithMilk; } + virtual string about() const { return (stringstream() << Animal::about() << ", " << "FeedsWithMilk =" << FeedsWithMilk).str(); } +}; + +class Cat :public Mammal { +private: + int MiceCaughtCounter; + public: - float weight; // kg + 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 Mammal : public Animal { +class Horse :public Mammal { +private: + float kilometresRun; public: - float pregnancyDuration; // days + 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 Cat : public Mammal { + +class Birds : public Animal { +private: + int FeathersCounter; +protected: + Birds() { FeathersCounter = 0; } + Birds(int Feathers) { FeathersCounter = Feathers; } public: - float vibrissaLength; // meters + 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/electricity/electricity.cpp b/electricity/electricity.cpp index 62da9453..48c1f4da 100644 --- a/electricity/electricity.cpp +++ b/electricity/electricity.cpp @@ -5,19 +5,40 @@ using namespace std; bool Object::isConnectedTo(const Object& other) const { - // TODO - return false; + for (int i = 0; i < getPoleCount(); ++i) { + auto pole = getPole(i); + if (pole != nullptr && pole->connectedObject == &other) + return true; + } } bool Object::connect(const std::string& poleName, Object& other, const std::string& otherPoleName) -{ - // TODO - return false; -} +{ + if (poleName == otherPoleName && &other == this) + return false; + + auto pole = getPole(poleName); + auto otherPole = (Pole*)(other.getPole(otherPoleName)); + + pole->connectedObject = (Object*)(&other); + pole->connectedObjectPole = otherPoleName; + otherPole->connectedObject = this; + otherPole->connectedObjectPole = poleName; + + return true; + } + bool Object::disconnect(const std::string& poleName) { - // TODO + if (getPole(poleName)->connectedObject != nullptr) + { + getPole(getPole(poleName)->connectedObjectPole)->connectedObject = nullptr; + getPole(getPole(poleName)->connectedObjectPole)->connectedObjectPole = ""; + getPole(poleName)->connectedObject = nullptr; + getPole(poleName)->connectedObjectPole = ""; + return true; + } return false; } @@ -30,26 +51,61 @@ Switch::Switch(const std::string& name) const Pole* Switch::getPole(const string& name) const { - if (name == a1.name) - return &a1; - if (name == a2.name) - return &a2; + if (name == a1.name) return &a1; + if (name == a2.name) return &a2; return nullptr; } const Pole* Switch::getPole(size_t idx) const { - // TODO + return getPole("A" + to_string(idx + 1)); +} +Lamp::Lamp(const string& name) + : Object(name) + , a1("A1") + , a2("A2") +{ +} +const Pole* Lamp::getPole(const string& name) const { + if (name == a1.name) return &a1; + if (name == a2.name) return &a2; return nullptr; } +const Pole* Lamp::getPole(size_t idx) const { + 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; +} +const Pole* Generator::getPole(size_t idx) const { + return getPole("A" + to_string(idx + 1)); +} + int main() { Switch sw, sw2; sw.connect("A2", sw2, "A1"); cout << "is " << (sw.isConnectedTo(sw2) ? "" : "not ") << "connected" << endl; - - // TODO: создать цепь из генератора, выключателя и светильника - + Generator generator_test; + Lamp lamp_test; + Switch switch_test; + generator_test.connect("A1", switch_test, "A2"); + switch_test.connect("A1", lamp_test, "A2"); + cout << "is " << (generator_test.isConnectedTo(lamp_test) ? "" : "not ") << "connected" << endl; + cout << "is " << (lamp_test.isConnectedTo(switch_test) ? "" : "not ") << "connected" << endl; + generator_test.disconnect("A1"); + cout << "is " << (generator_test.isConnectedTo(lamp_test) ? "" : "not ") << "connected" << endl; return 0; } diff --git a/electricity/electricity.h b/electricity/electricity.h index cb19597b..6256992a 100644 --- a/electricity/electricity.h +++ b/electricity/electricity.h @@ -50,7 +50,11 @@ class Object { /// /// Индекс полюса, от 0 до значения, возвращаемого . /// Полюс с указанным индексом, или nullptr, если такой полюс не существует. - Pole* getPole(size_t idx) { /* TODO */ return nullptr; } + Pole* getPole(size_t idx) { + if (getPole("A" + std::to_string(idx))) + return getPole("A" + std::to_string(idx)); + return nullptr; + } /// /// Возвращает полюс по внутреннему индексу устройства. @@ -131,7 +135,22 @@ class Switch : public Object { protected: virtual const Pole* getPole(size_t idx) const; }; +class Lamp : public Object { +public: + Pole a1, a2; + Lamp(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; +}; -// TODO: класс светильника с двумя полюсами - -// TODO: класс генератора с тремя полюсами (фаза, нейтраль, земпя). +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: + const Pole* getPole(size_t idx) const override; +}; \ No newline at end of file diff --git a/memhacks/memhacks.cpp b/memhacks/memhacks.cpp index 514c8689..c2404c67 100644 --- a/memhacks/memhacks.cpp +++ b/memhacks/memhacks.cpp @@ -2,23 +2,25 @@ #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] = 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: "; b.printData(cout); cout << endl; + std::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 << std::endl; + std::cout << "Size of A is " << sizeof(A) << ", size of B is " << sizeof(B) << std::endl; + std::cout << "B string is '" << b.getBString() << "'" << std::endl; + std::cout << "B data: "; const_cast(&b)->printData2(std::cout); std::cout << std::endl; } /// @@ -26,8 +28,12 @@ void printInternals(const B& b) { /// Подразумевается, что текущий объект на самом деле представлено классом . /// /// Значение B::b_s -std::string A::getBString() const { - // TODO +string A::getBString() const { + return *((const string*)(this + 1)); +} + +string B::getBString() const { + return b_s; } /// @@ -36,17 +42,32 @@ std::string A::getBString() const { /// с помощью адресной арифметики. /// Подразумевается, что текущий объект на самом деле представлено классом . /// -void A::printData(std::ostream& os) { - // TODO +void A::printData(ostream& os) { + os << "a_s is " << a_s << std::endl; + os << "b_s is " << getBString() << endl; + float* b_Data = ((float*)(((string*)(this + 1)) + 1)); + os << "data is: "; + for (auto i = 0; i < sizeof(b_Data) - 1; i++) + { + os << b_Data[i] << " "; + } + os << endl; } - /// /// Извлекает значения , и /// из текущего объекта и выводит их в текстовом виде в указанный выходной поток /// с помощью виртуальных функций, предусмотренных в классе . /// void A::printData2(std::ostream& os) { - // TODO + os << "a_s is " << a_s << std::endl; + os << "b_s is " << getBString() << std::endl; + float* b_Data = getBData(); + os << "data is: "; + for (auto i = 0; i < sizeof(b_Data) - 1; i++) + { + os << b_Data[i] << " "; + } + os << std::endl; } int main() diff --git a/memhacks/memhacks.h b/memhacks/memhacks.h index 51076b55..cb36e87b 100644 --- a/memhacks/memhacks.h +++ b/memhacks/memhacks.h @@ -1,30 +1,30 @@ #pragma once - #include #include - class B; // чтобы можно было объявить printInternals() как friend в классе A - class A { std::string a_s; int foo; - - friend void printInternals(const B&); + friend void printInternals(B& b); public: + A(); std::string getBString() const; + virtual float getBData(int idx) const; void printData(std::ostream& os); void printData2(std::ostream& os); + virtual float* getBData() = 0; }; - class B : public A { std::string b_s; float data[7]; - - friend void printInternals(const B&); + friend void printInternals(B& b); public: B(); + + std::string getBString() const; + virtual float getBData(int idx) const; }; -void printInternals(const B& b); +void printInternals(B& b); \ No newline at end of file diff --git a/memhacks/newhacks.cpp b/memhacks/newhacks.cpp index ba359c62..bdbcb297 100644 --- a/memhacks/newhacks.cpp +++ b/memhacks/newhacks.cpp @@ -1,9 +1,41 @@ #include -#include "memhacks.h" - +#include "newhacks.h" using namespace std; +Foo::Foo() : student(), pupil() { cerr << "Вызван конструктор " << this << endl; } +Foo::~Foo() { cerr << "Вызван деструктор " << this << endl; } +void* Foo::operator new(size_t size) { + void* pVoid = malloc(size); + cerr << "Выполняется оператор new(" << size << ") returns " << pVoid << endl; + return pVoid; +} +void Foo::operator delete(void* memory) { + cerr << "Выполняется оператор delete(" << memory << ")" << endl; + free(memory); +} +Bar::Bar() : clever(), stupid() { cerr << "Вызван конструктор " << this << endl; } +Bar::~Bar() { cerr << "Вызван деструктор" << this << endl; } +void* Bar::operator new(size_t size) { + void* pVoid = malloc(size); + cerr << "Выполняется оператор new(" << size << ") returns " << pVoid << endl; + return pVoid; +} +void Bar::operator delete(void* memory) { + cerr << "Выполняется оператор delete(" << memory << ")" << endl; + free(memory); +} +Buz::Buz() : poltorashka(), shpala() { cerr << "Вызван конструктор" << this << endl; } +Buz::~Buz() { cerr << "Вызван деструктор" << this << endl; } + int main() { - return 0; -} + setlocale(LC_ALL, "Russian"); + Foo foo_test = Foo(); + Bar bar_test = Bar(); + Buz buz_test = Buz(); + Foo* foo_test2 = new Foo(); + Bar* bar_test2 = new Bar(); + delete(foo_test2); + delete(bar_test2); + return 0; +} \ No newline at end of file diff --git a/memhacks/newhacks.h b/memhacks/newhacks.h index 079a3835..85c64008 100644 --- a/memhacks/newhacks.h +++ b/memhacks/newhacks.h @@ -1,2 +1,36 @@ #pragma once +#include +using namespace std; +class Foo { + int student; + double pupil; +public: + Foo(); + ~Foo(); + static void* operator new(size_t size); + static void operator delete(void* memory); +}; +class Bar : public Foo { + int clever; + string stupid; + +public: + Bar(); + ~Bar(); + + static void* operator new(size_t size); + static void operator delete(void* memory); +}; + +class Buz : public Foo { + long poltorashka; + long long shpala; + +public: + Buz(); + ~Buz(); + + static void* operator new(size_t size) = delete; + static void operator delete(void* memory) = delete; +}; diff --git a/vectors/vector.cpp b/vectors/vector.cpp index 9777069c..316819f8 100644 --- a/vectors/vector.cpp +++ b/vectors/vector.cpp @@ -1,25 +1,11 @@ +#pragma once #include #include "vector.h" 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; - - 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; -} +int main() { + setlocale(LC_ALL, "rus"); + if (test_vector3d()) cout << " " << 1; + else cout << " " << 0; + test_vector3d(); +} \ No newline at end of file diff --git a/vectors/vector.h b/vectors/vector.h index 07587fb3..dccffb72 100644 --- a/vectors/vector.h +++ b/vectors/vector.h @@ -1,19 +1,51 @@ #pragma once - -#include - +#include +#include +#include class vector3d { +public: 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]; } + float operator [](int index) const { return data[index]; } + float& operator [](int index) { return data[index]; } - friend int main(int argc, char** argv); + 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(1, 1, 1); + else return vector3d(0, 0, 0); + } }; -std::ostream& operator <<(std::ostream& os, const vector3d& v); +ostream& operator <<(ostream& os, const vector3d& v) { + return os << "{ " << v[0] << ", " << v[1] << ", " << v[2] << " }"; +} +bool test_vector3d() +{ + vector3d v1(5, 5, 5); + vector3d v2(1, 1, 1); + vector3d v3; + vector3d v4(0, 0, 0); + v3 = v1 + v2; + if ((v3.data[0] != 6) || (v3.data[1] != 6) || (v3.data[2] != 6)) { cout << "The addition operation does not work. Check Data v1(5,5,5), v2(1,1,1)"; return 1; } + v3 = v1 - v2; + if ((v3.data[0] != 4) || (v3.data[1] != 4) || (v3.data[2] != 4)) { cout << "The subtraction operation does not work. Check Data v1(5,5,5), v2(1,1,1)"; return 1; } + v3 = v1 * 3; + if ((v3.data[0] != 15) || (v3.data[1] != 15) || (v3.data[2] != 15)) { cout << "The multiplication operation does not work. Check Data v1(5,5,5), 3"; return 1; } + v3 = v1 / 5; + if ((v3.data[0] != 1) || (v3.data[1] != 1) || (v3.data[2] != 1)) { cout << "The division operation by a scalar does not work. Check Data v1(5,5,5), 5"; return 1; } + v3 = -v1; + if ((v3.data[0] != -5) || (v3.data[1] != -5) || (v3.data[2] != -5)) { cout << "The invert operation does not work. Check Data v1(5,5,5)"; return 1; } + v3 = !v1; + if ((v3.data[0] != 0) || (v3.data[1] != 0) || (v3.data[2] != 0)) { cout << "Operation ! does not work. Check Data v4(0,0,0)"; return 1; } + v3 = !v4; + if ((v3.data[0] != 1) || (v3.data[1] != 1) || (v3.data[2] != 1)) { cout << "Operation ! does not work. Check Data v1(1,1,1)"; return 1; } + return 0; +}