diff --git a/animal.cpp b/animal.cpp new file mode 100644 index 0000000..9912774 --- /dev/null +++ b/animal.cpp @@ -0,0 +1,83 @@ +#include +#include +using namespace std; + + +class Animal { +private: + bool CanMoveFreely; +public: + Animal() { CanMoveFreely = 0; } + Animal(bool YesOrNo) { CanMoveFreely = YesOrNo; } + 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; +public: + float weight; // kg + Mammal() { FeedsWithMilk = 0; } + Mammal(bool FeedOrNot) { FeedsWithMilk = FeedOrNot; } + void setFeedsWithMilk(bool FeedOrNot) { FeedsWithMilk = FeedOrNot; } + bool get_FeedsWithMilk() const { return FeedsWithMilk; } + virtual string about() const { return (stringstream() << Animal::about() << ", " << "FeedsWithMilk =" << FeedsWithMilk).str(); } +}; + +class Mammal : public Animal { + class Cat :public Mammal { + private: + int MiceCaughtCounter; + public: + float pregnancyDuration; // days + 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 Cat : public Mammal { + class Horse :public Mammal { + private: + float kilometresRun; + public: + float vibrissaLength; // meters + 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 Birds : public Animal { + private: + int FeathersCounter; + public: + Birds() { FeathersCounter = 0; } + Birds(int Feathers) { FeathersCounter = Feathers; } + 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/animal.h b/animal.h new file mode 100644 index 0000000..d792a2b --- /dev/null +++ b/animal.h @@ -0,0 +1,79 @@ +#pragma once +#include +#include +#include +using namespace std; +class Animal +{ +private: + int age; + int weight; + int height; +public: + void animal(int age, int weight, int height) { + age = age; + height = height; + weight = weight; + } + void set_age(int age) { age = age; } + void set_height(float height) { height = height; } + void set_weight(float weight) { weight = weight; } + + int get_age() { return age; } + float get_height() { return height; } + float get_weight() { return weight; } + virtual string about() const + { + stringstream ss; + ss << "age = " << get_age << ";" + << "height = " << get_height << " ;" + << "weight = " << get_weight << endl; + + } + +}; + +class Mammal :public Animal +{ +private: + string hair_color; + bool meat_eater; +public: + void mammal(string hair_color, bool meat_eater) { + hair_color = hair_color; + meat_eater = meat_eater; + } + void set_hair_color() { hair_color = hair_color; } + void set_meat_eater() { meat_eater = meat_eater; } + string get_hair_color() { return hair_color; } + bool get_meat_eater() { return meat_eater; } + virtual string about() const + { + stringstream ss; + ss << "hair color is - " << get_hair_color << ";" + << "he/she is meat eater? - " << get_meat_eater << endl; + } +}; + +class Human :public Mammal +{ +private: + bool have_work; + string lifestyle; +public: + void human(bool have_work, string lifestyle) { + have_work = have_work; + lifestyle = lifestyle; + } + void set_work() { have_work = have_work; } + void set_life_style() { lifestyle = lifestyle; } + bool get_work() { return have_work; } + string get_life_style() { lifestyle = lifestyle; } + virtual string about() const + { + stringstream ss; + ss << " have work ? - " << get_work << ", " + << "lifestyle = " << get_life_style; + return ss.str(); + } +}; \ No newline at end of file diff --git a/electricy.cpp b/electricy.cpp new file mode 100644 index 0000000..1598872 --- /dev/null +++ b/electricy.cpp @@ -0,0 +1,97 @@ +#include +#include "electricity.h" + +using namespace std; + +bool Object::isConnectedTo(const Object& other) const +{ + for (size_t j = 0; j < getPoleCount(); j++) + { + const Pole* selfPole = getPole(j); + if (selfPole != nullptr && (selfPole->connectedObject) == &other) return true; + } + + + return false; +} + +bool Object::connect(const std::string& poleName, const Object& other, const std::string& otherPoleName) + +{ + + if (poleName == otherPoleName && &other == this) return false; + + Pole* selfPole = getPole(poleName); + Pole* otherPole = other.getPole(otherPoleName); + if (otherPole == nullptr || otherPole == nullptr) return false; + + (selfPole->connectedObject) = &other; + (selfPole->connectedObjectPole) = otherPoleName; + + (otherPole->connectedObject) = this; + + (otherPole->connectedObjectPole) = poleName; + + return true; +} + +bool Object::disconnect(const std::string& poleName) +{ + return false; + Pole* p = getPole(poleName); + if (p == nullptr) return false; + + (p->connectedObject) = nullptr; + (p->connectedObjectPole) = ""; + + return true; +} + + + +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(size_t idx) const +{ + if (idx == 0) return &a1; + else if (idx == 1) return &a2; + + + return nullptr; +} + +int main() +{ + Switch sw, sw2; + sw.connect("A2", sw2, "A1"); + cout << "is " << (sw.isConnectedTo(sw2) ? "" : "not ") << "connected" << endl; + + cout << "is " << (sw.isConnectedTo(sw2) ? "" : "not ") << "connected" << endl; + + Generator g; Light l; Switch sw3; + + g.connect("p", l, "A1"); + l.connect("A2", sw3, "A1"); + cout << "is " << (g.isConnectedTo(l) ? "" : "not ") << "connected" << endl; + cout << "is " << (l.isConnectedTo(sw3) ? "" : "not ") << "connected" << endl; + + g.disconnect("p"); + cout << "is " << (g.isConnectedTo(l) ? "" : "not ") << "connected" << endl; + + return 0; + + return 0; +} diff --git a/electricy.h b/electricy.h new file mode 100644 index 0000000..1f877cf --- /dev/null +++ b/electricy.h @@ -0,0 +1,102 @@ +#pragma once +#include +class Object; + +struct Pole { + + std::string name; + + Object* connectedObject; + + std::string connectedObjectPole; + Pole(const std::string& name_) : name(name_), connectedObject(nullptr) {} +}; + +class Object { + std::string name; +protected: + Object(const std::string& name_) : name(name_) {} + + Pole* getPole(size_t idx) { return nullptr; } + Pole* getPole(size_t idx) { + return const_cast(const_cast(this)->getPole(idx)); + } + +public: + virtual ~Object() {} + + const std::string& getName() const { return name; } + void getName(const std::string& newName) { name = newName; } + void setName(const std::string& newName) { name = newName; } + + + Pole* getPole(const std::string& name) { return const_cast(const_cast(this)->getPole(name)); } + + virtual const Pole* getPole(const std::string& name) const = 0; + + virtual size_t getPoleCount() const = 0; + + + bool connect(const std::string& poleName, Object& other, const std::string& otherPoleName); + + bool disconnect(const std::string& poleName); +}; + +class Switch : public Object { +public: + Pole a1, a2; + Switch(const std::string& name = ""); + virtual size_t getPoleCount() const { return 2; } + virtual const Pole* getPole(const std::string& name) const; +protected: + virtual const Pole* getPole(size_t idx) const; +}; + + +class Light : public Object { +public: + Pole a1, a2; + + Light(const std::string& name = "") : Object(name), a1("A1"), a2("A2") {} + + virtual size_t getPoleCount() const { return 2; } + + virtual const Pole* getPole(const std::string& name) const { + if (name == a1.name) return &a1; + else if (name == a2.name) return &a2; + + return nullptr; + } +protected: + virtual const Pole* getPole(size_t idx) const { + if (idx == 0) return &a1; + else if (idx == 1)return &a2; + + return nullptr; + } +}; + +class Generator : public Object { +public: + Pole phase, neural, earth; + + Generator(const std::string& name = "") : Object(name), phase("p"), neural("n"), earth("e") {} + + virtual size_t getPoleCount() const { return 3; } + + virtual const Pole* getPole(const std::string& name) const { + if (name == phase.name) return &phase; + else if (name == neural.name) return &neural; + else if (name == earth.name) return &earth; + + return nullptr; + } +protected: + virtual const Pole* getPole(size_t idx) const { + if (idx == 0) return &phase; + else if (idx == 1)return &neural; + else if (idx == 2)return &earth; + + return nullptr; + } +}; diff --git a/memhacks.cpp b/memhacks.cpp new file mode 100644 index 0000000..660aef7 --- /dev/null +++ b/memhacks.cpp @@ -0,0 +1,39 @@ +#include +#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] = (float)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 "; a->printData(cout); cout << endl; + cout << "B data "; a->printData2(cout); cout << endl; +} + +@@ - 27, 7 + 30, 19 @@ void printInternals(const B & b) { + + std::string A::getBString() const { + return dynamic_cast(this)->getBString(); + } + + std::string B::getBString() const { + return b_s; + } + + float A::getData(int idx) const { + return dynamic_cast(this)->getData(idx); + } + + float B::getData(int idx) const { + return data[idx]; + } +} \ No newline at end of file diff --git a/memhacks.h b/memhacks.h new file mode 100644 index 0000000..c5bdd38 --- /dev/null +++ b/memhacks.h @@ -0,0 +1,35 @@ +#pragma once +#include +#include + +class B; +class A { + std::string a_s; + int foo; + + friend void printInternals(const B& b); + +public: + 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]{}; + + friend void printInternals(const B& b); + +public: + B(); + + float getData(int idx) const override; + std::string getBString() const override; +}; + +void printInternals(const B& b); \ No newline at end of file diff --git a/vector.h b/vector.h new file mode 100644 index 0000000..f0b75e3 --- /dev/null +++ b/vector.h @@ -0,0 +1,66 @@ +#pragma once +#include + +class vector3 { + float data[3]{}; +public: + vector3() { data[0] = data[1] = data[2] = 0; } + vector3(float value) { data[0] = data[1] = data[2] = value; } + vector3(float x, float y, float z) { data[0] = x, data[1] = y, data[2] = z; } + + float& operator[](int idx) { + if (idx > 2 || idx < 0) + throw std::out_of_range("Index " + std::to_string(idx) + " out of bounds for type vector3"); + + return data[idx]; + } + float operator[](int idx) const { + if (idx > 2 || idx < 0) + throw std::out_of_range("Index " + std::to_string(idx) + " out of bounds for type vector3"); + + return data[idx]; + } + + vector3 operator+ (const vector3& v) const; + vector3 operator- (const vector3& v) const; + + vector3 operator* (const float& f) const; + vector3 operator/ (const float& f) const; + + vector3 operator- () const; + vector3 operator! () const; + + bool operator ==(const vector3& v) const; +}; + +std::ostream& operator <<(std::ostream& os, const vector3& v) { + return os << "{" << v[0] << ", " << v[1] << ", " << v[2] << "}"; +} + +vector3 vector3::operator+(const vector3& v) const { + return { data[0] + v[0], data[1] + v[1], data[2] + v[2] }; +} + +vector3 vector3::operator-(const vector3& v) const { + return { data[0] - v[0], data[1] - v[1], data[2] - v[2] }; +} + +vector3 vector3::operator*(const float& f) const { + return { data[0] * f, data[1] * f, data[2] * f }; +} + +vector3 vector3::operator/(const float& f) const { + return { data[0] / f, data[1] / f, data[2] / f }; +} + +vector3 vector3::operator-() const { + return { -data[0], -data[1], -data[2] }; +} + +vector3 vector3::operator!() const { + return { float(!data[0] && !data[1] && !data[2]) }; +} + +bool vector3::operator==(const vector3& v) const { + return data[0] == v[0] && data[1] == v[1] && data[2] == v[2]; +} diff --git a/vector3d.cpp b/vector3d.cpp new file mode 100644 index 0000000..d1db533 --- /dev/null +++ b/vector3d.cpp @@ -0,0 +1,40 @@ +#include "vector.h" + +bool test_vector3() { + auto vec_1 = vector3(1, 2, 3); + auto vec_2 = vector3(3, 2, 1); + + bool check_1 = (vec_1 + vec_2) == vector3(4); + bool check_2 = (vec_2 - vec_1) == vector3(2, 0, -2); + + auto vec_3 = vector3(3); + + bool check_3 = (vec_3 * 3) == vector3(9); + bool check_4 = (vec_3 / 3) == vector3(1); + bool check_5 = -vec_3 == vector3(-3); + + auto vec_4 = vector3(0); + + bool check_6 = !vec_4 == vector3(1); + bool check_7 = !vec_1 == vector3(0); + + if (!check_1) + std::cerr << "Invalid result of the operator+ (vector3 lhs, vector3 rhs), lhs value: " << vec_1 << ", rhs value: " << vec_2 << std::endl; + if (!check_2) + std::cerr << "Invalid result of the operator- (vector3 lhs, vector3 rhs), lhs value: " << vec_2 << ", rhs value: " << vec_1 << std::endl; + if (!check_3) + std::cerr << "Invalid result of the operator* (vector3 lhs, float rhs), lhs value: " << vec_3 << ", rhs value: " << 3 << std::endl; + if (!check_4) + std::cerr << "Invalid result of the operator/ (vector3 lhs, float rhs), lhs value: " << vec_3 << ", rhs value: " << 3 << std::endl; + if (!check_5) + std::cerr << "Invalid result of the operator- (vector3 v), v value: " << vec_3 << std::endl; + if (!check_6) + std::cerr << "Invalid result of the operator! (vector3 v), v value: " << vec_4 << std::endl; + if (!check_7) + std::cerr << "Invalid result of the operator! (vector3 v), v value: " << vec_1 << std::endl; + + return check_1 && check_2 && check_3 && check_4 && check_5 && check_6 && check_7; +} + +int main() { + return !test_vector3()