diff --git a/animal.cpp b/animal.cpp new file mode 100644 index 0000000..f3599ec --- /dev/null +++ b/animal.cpp @@ -0,0 +1,93 @@ +using namespace std ; + +#include "animal.h" + +string Animal::about() const { + stringstream ss; + ss << "Animal_color = " << " " << Animal_color; + return ss.str(); +}; + +string Mammal::about() const { + stringstream ss; + ss << Animal::about() << " " << " gender = " << " " << gender; + return ss.str(); +}; + +string Predator::about() const { + stringstream ss; + ss << Animal::about() << " " << " omnivorous = " << " " << omnivorous; + return ss.str(); +}; + +string Whales::about() const { + stringstream ss; + ss << Mammal::about() << " " << " opportunity_to_get_out_of_the_water = " << " " << opportunity_to_get_out_of_the_water; + return ss.str(); +}; + +string Dog::about() const { + stringstream ss; + ss << Mammal::about() << " " << " Tail_length = " << " " << Tail_length; + return ss.str(); +}; + +string Husky::about() const { + stringstream ss; + ss << Dog::about() << " " << " Average_length_of_wool = " << " " << Average_length_of_wool; + return ss.str(); +}; + +string Pug::about() const { + stringstream ss; + ss << Dog::about() << " " << " Leg_length = " << " " << Leg_length; + return ss.str(); +}; + + + + + + + + + +int main(){ + Pug pes_Solnyshk0; + Husky sobaka_Marta; + Dog Sam; + Whales Dolphin; + Predator Bears; + + pes_Solnyshk0.gender = true; + pes_Solnyshk0.Animal_color = "yellow"; + pes_Solnyshk0.Leg_length = 10; + pes_Solnyshk0.Tail_length = 5; + + sobaka_Marta.Average_length_of_wool = 5; + sobaka_Marta.gender = false; + sobaka_Marta.Animal_color = "white"; + sobaka_Marta.Tail_length = 20; + + Sam.gender = true; + Sam.Tail_length = 15; + Sam.Animal_color = "black"; + + Dolphin.opportunity_to_get_out_of_the_water = false; + Dolphin.Animal_color = "blue"; + Dolphin.gender = true; + + Bears.Animal_color = "brown"; + Bears.gender = true; + Bears.omnivorous = true; + + cout << "-------------------------------------------------------------" << endl; + cout << "pes_Solnyshk0: " << pes_Solnyshk0.about() << endl; + cout << "sobaka_Marta: " << sobaka_Marta.about() << endl; + cout << "Sam: " << Sam.about() << endl; + cout << "Dolphin: " << Dolphin.about() << endl; + cout << "Bears: " << Bears.about() << endl; + cout << "-------------------------------------------------------------"; + + +} \ No newline at end of file diff --git a/electricity.cpp b/electricity.cpp new file mode 100644 index 0000000..a1c28c0 --- /dev/null +++ b/electricity.cpp @@ -0,0 +1,112 @@ +#include + +using namespace std; + +#include "electricity.h" + +bool Object::isConnectedTo(const Object& other) const +{ + for (size_t idx_1 = 0; idx_1 < this->getPoleCount(); idx_1++) { + + for (size_t idx_2 = 0; idx_2 < other.getPoleCount(); idx_2++) { + + if (this->getPole(idx_1)->connectedObjectPole == other.getPole(idx_2)->name) return true; + + } + } + return false; +} + +bool Object::disconnect(const string& poleName) +{ + if (getPole(poleName)->connectedObjectPole != "") { + + getPole(poleName)->connectedObjectPole = ""; + this->getPole(poleName)->connectedObject = nullptr; + return true; + } + else return false; +} + +bool Object::connect(const string& poleName, Object& other, const string& otherPoleName) +{ + this->getPole(poleName)->connectedObjectPole = otherPoleName; + this->getPole(poleName)->connectedObject = &other; + + other.getPole(otherPoleName)->connectedObject = this; + other.getPole(otherPoleName)->connectedObjectPole = poleName; + + return false; +} + +Switch::Switch(const string& name) : Object(name), A1("A1"), A2("A2") {} + +Light::Light(const string& name_) : Object(name_), A1("A1"), A2("A2") {} + +Power::Power(const string& name_) : Object(name_), A1("A1"), A2("A2"), A3("A3") {} + +const Pole* Switch::getPole(const string& name) const +{ + if (name == A1.name) + return &A1; + if (name == A2.name) + return &A2; + return nullptr; +} + +const Pole* Light::getPole(const string& name) const +{ + if (name == A1.name) + return &A1; + if (name == A2.name) + return &A2; + return nullptr; +} +const Pole* Power::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* Switch::getPole(size_t idx) const +{ + return (getPole("A" + to_string(idx + 1))); +} + +const Pole* Light::getPole(size_t idx) const +{ + return (getPole("A" + to_string(idx + 1))); +} +const Pole* Power::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; + + + Power p; + Light l; + Switch sw3; + + + + p.connect("A1", l, "A1"); + + l.connect("A2", sw, "A1"); + cout << "is " << (p.isConnectedTo(l) ? "" : "not ") << "connected!" << endl; + cout << "is " << (l.isConnectedTo(sw3) ? "" : "not ") << "connected!" << endl; + + p.disconnect("A1"); + cout << "is " << (p.isConnectedTo(l) ? "" : "not ") << "connected!" << endl; + + return 0; +} \ No newline at end of file diff --git a/newhacks.cpp b/newhacks.cpp new file mode 100644 index 0000000..1105efa --- /dev/null +++ b/newhacks.cpp @@ -0,0 +1,51 @@ +#include +#include "newhacks.h" + +using namespace std; + +Foo::Foo() : cat(), frog() { cerr << "constructed Foo" << this << endl; } +Foo::~Foo() { cerr << "destructed Foo" << this << endl; } + +void *Foo::operator new(size_t size) { + void *pVoid = malloc(size); + cerr << "operator new(" << size << ") returns " << pVoid << endl; + return pVoid; +} + +void Foo::operator delete(void *pVoid) { + cerr << "operator delete(" << pVoid << ")" << endl; + free(pVoid); +} + +Bar::Bar() : turtle(), dog() { cerr << "constructed Bar " << this << endl; } +Bar::~Bar() { cerr << "destructed Bar" << this << endl; } + +void *Bar::operator new(size_t size) { + void *pVoid = malloc(size); + cerr << "operator new(" << size << ") returns " << pVoid << endl; + return pVoid; +} + +void Bar::operator delete(void *pVoid) { + cerr << "operator delete(" << pVoid << ")" << endl; + free(pVoid); +} + +Buz::Buz() : snake(), rhinoceros() { cerr << "constructed Buz" << this << endl; } +Buz::~Buz() { cerr << "destructed Buz " << this << endl; } + +int main() +{ + Foo foo_stack = Foo(); + Bar bar_stack = Bar(); + Buz buz_stack = Buz(); + + Foo* foo_heap = new Foo(); + Bar* bar_heap = new Bar(); + + + delete(foo_heap); + delete(bar_heap); + + return 0; +} diff --git a/newhacks.h b/newhacks.h new file mode 100644 index 0000000..6e5c05f --- /dev/null +++ b/newhacks.h @@ -0,0 +1,36 @@ +#include + +class Foo { + int cat; + float frog; +public: + Foo(); + ~Foo(); + + static void *operator new(size_t size); + static void operator delete(void *pVoid); +}; + +class Bar : public Foo { + int turtle; + std::string dog; + +public: + Bar(); + ~Bar(); + + static void *operator new(size_t size); + static void operator delete(void *pVoid); +}; + +class Buz : public Foo { + float snake; + short rhinoceros; + +public: + Buz(); + ~Buz(); + + static void *operator new(size_t size) = delete; + static void operator delete(void *pVoid) = delete; +}; \ No newline at end of file diff --git a/vector.cpp b/vector.cpp new file mode 100644 index 0000000..8d914d5 --- /dev/null +++ b/vector.cpp @@ -0,0 +1,50 @@ +using namespace std; +#include "vector.h" + +bool test_vector3d() +{ + vector3d v1(20, 45, 160); + vector3d v2(25, 80, 30); + + cout << "v1: " + << "20, 45, 160" << endl; + cout << "v2: " + << "25, 80, 30" << 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(); +} \ No newline at end of file diff --git a/vector.h b/vector.h new file mode 100644 index 0000000..0566c0b --- /dev/null +++ b/vector.h @@ -0,0 +1,29 @@ +#include +#include + +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) 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] << " }"; +} + +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); } +const vector3d operator -(const vector3d& v1) { return vector3d(-v1[0], -v1[1], -v1[2]); } +const 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); +} \ No newline at end of file