diff --git a/animals/animal.cpp b/animals/animal.cpp index 6745215..28c60ff 100644 --- a/animals/animal.cpp +++ b/animals/animal.cpp @@ -3,5 +3,19 @@ using namespace std; int main() { + Elephant eleph(150, 3000., 22); + eleph.setWeight(3004.); + eleph.setTrunkLength(145); + cout << eleph.getTrunkLength() << " " << eleph.getPregnancyDuration() << " " << eleph.getWeight() << "\n"; + + Cat cat(14, 7, 1); + cout << cat.getVibrissaLength() << " " << cat.getWeight() << " " << cat.getPregnancyDuration() << "\n"; + + Penguin penguin(1, 40); + cout << penguin.getHeight() << " " << penguin.getWeight() << " " << penguin.getCanFly() << "\n"; + + Colibri colibri("blue", 0.02); + cout << colibri.getColor() << " " << colibri.getWeight() << " " << colibri.getCanFly() << "\n"; + return 0; } \ No newline at end of file diff --git a/animals/animal.h b/animals/animal.h index 83a1944..c251a1c 100644 --- a/animals/animal.h +++ b/animals/animal.h @@ -1,18 +1,102 @@ #pragma once #include +using namespace std; class Animal { +private: + float weight; public: - float weight; // kg + Animal(float w = 0.) { + weight = w; + } + float getWeight() const { return weight; } + void setWeight(float w) { weight = w; } + virtual void about() { + cout << "Weight = " << weight << "\n"; + } }; class Mammal : public Animal { +private: + int pregnancyDuration; public: - float pregnancyDuration; // days + Mammal(float w = 0., int p = 0) : Animal(w) { + pregnancyDuration = p; + } + float getPregnancyDuration() const { return pregnancyDuration; } + void setPregnancyDuration(int p) { pregnancyDuration = p; } + virtual void about() { + cout << "PregnancyDuration = " << pregnancyDuration << "\n"; + } }; class Cat : public Mammal { +private: + int vibrissaLength; public: - float vibrissaLength; // meters + Cat(int v = 0, float w = 0., int p = 0) : Mammal(w, p) { + vibrissaLength = v; + } + float getVibrissaLength() const { return vibrissaLength; } + void setVibrissaLength(float v) { vibrissaLength = v; } + virtual void about() { + cout << "VibrissaLength = " << vibrissaLength << "\n"; + } +}; + +class Elephant : public Mammal { +private: + int trunkLength; +public: + Elephant(int t = 0, float w = 0., int p = 0) : Mammal(w, p) { + trunkLength = t; + } + float getTrunkLength() const { return trunkLength; } + void setTrunkLength(float t) { trunkLength = t; } + virtual void about() { + cout << "TrunkLength = " << trunkLength << "\n"; + } +}; + +class Birds : public Animal { +private: + bool canFly; +public: + Birds(bool b = false, float w = 0.) : Animal(w) { + canFly = b; + } + bool getCanFly() const { return canFly; } + void setCanFly(bool b) { canFly = b; } + virtual void about() { + cout << "CanFly = " << canFly << "\n"; + } +}; + +class Penguin : public Birds { +private: + int height; +public: + Penguin(int h = 0, float w = 0.) : Birds(false, w) { + height = h; + } + bool getHeight() const { return height; } + void setHeight(int h) { height = h; } + virtual void about() { + cout << "Height = " << height << "\n"; + } +}; + +class Colibri : public Birds { +private: + string color; +public: + Colibri(string c = "green", float w = 0.) : Birds(true, w) { + color = c; + } + string getColor() const { return color; } + void setColor(string c) { color = c; } + virtual void about() { + cout << "Color = " << color << "\n"; + } }; diff --git a/memhacks/memhacks.cpp b/memhacks/memhacks.cpp index 514c868..2d3e29d 100644 --- a/memhacks/memhacks.cpp +++ b/memhacks/memhacks.cpp @@ -4,54 +4,57 @@ using namespace std; B::B() : b_s("It's b!") { - for (auto i = 0; i < sizeof(data) / sizeof(data[0]); i++) - data[i] = i * 2; + 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; +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; } -/// /// Извлекает значение из текущего объекта. -/// Подразумевается, что текущий объект на самом деле представлено классом . -/// -/// Значение B::b_s std::string A::getBString() const { - // TODO + return *((const string *) (this + 1)); } -/// /// Извлекает значения , и /// из текущего объекта и выводит их в текстовом виде в указанный выходной поток /// с помощью адресной арифметики. -/// Подразумевается, что текущий объект на самом деле представлено классом . -/// -void A::printData(std::ostream& os) { - // TODO +void A::printData(std::ostream &os) { + os << "A::a_s: " << a_s << "\n"; + os << "B::b_s: " << A::getBString() << "\n"; + os << "B::data: "; + + const float *bData = ((const float *) (((const std::string *) (this + 1)) + 1)); + for (int i = 0; i < 7; ++i) { + os << bData[i] << " "; + } + os << "\n"; } -/// /// Извлекает значения , и /// из текущего объекта и выводит их в текстовом виде в указанный выходной поток /// с помощью виртуальных функций, предусмотренных в классе . -/// -void A::printData2(std::ostream& os) { - // TODO -} +void A::printData2(std::ostream &os) { + os << "A::a_s: " << *A::getString() << "\n"; + os << "B::b_s: " << *getString() << "\n"; -int main() -{ - B b; - printInternals(b); - return 0; + const float *bData = getData(); + for (int i = 0; i < 7; ++i) { + os << bData[i] << " "; + } + os << "\n"; } + +int main() { + B b; + printInternals(b); + return 0; +} \ No newline at end of file diff --git a/memhacks/memhacks.h b/memhacks/memhacks.h index 51076b5..7c583ce 100644 --- a/memhacks/memhacks.h +++ b/memhacks/memhacks.h @@ -6,25 +6,36 @@ class B; // чтобы можно было объявить printInternals() как friend в классе A class A { - std::string a_s; - int foo; + std::string a_s; + int foo; - friend void printInternals(const B&); + friend void printInternals(const B&); public: - std::string getBString() const; - void printData(std::ostream& os); - void printData2(std::ostream& os); + std::string getBString() const; + void printData(std::ostream& os); + void printData2(std::ostream& os); + + virtual const std::string* getString() const { + return &a_s; + }; + virtual const float* getData() const {} }; class B : public A { - std::string b_s; - float data[7]; + std::string b_s; + float data[7]; - friend void printInternals(const B&); + friend void printInternals(const B&); public: - B(); + B(); + virtual const std::string* getString() const { + return &b_s; + } + virtual const float* getData() const { + return data; + } }; -void printInternals(const B& b); +void printInternals(const B& b); \ No newline at end of file diff --git a/memhacks/newhacks.cpp b/memhacks/newhacks.cpp index ba359c6..b3ea9b2 100644 --- a/memhacks/newhacks.cpp +++ b/memhacks/newhacks.cpp @@ -1,9 +1,23 @@ #include -#include "memhacks.h" +#include "newhacks.h" using namespace std; -int main() -{ - return 0; +void solve() { + //Создание всех трех элементов на стеке + Foo f(1, 'a'); Bar b(13); Buz z(123); + + //Создание всех трех элементов в динамической памяти + Foo *f1 = new Foo; Bar *b1 = new Bar; Buz *z1 = new Buz; + delete f1; delete b1; delete z1; + + //Для того чтобы запретить создание элемента типа Buz + //достаточно operator new/delete записать в private: +} + +int main() { + solve(); + + + return 0; } diff --git a/memhacks/newhacks.h b/memhacks/newhacks.h index 079a383..fc65426 100644 --- a/memhacks/newhacks.h +++ b/memhacks/newhacks.h @@ -1,2 +1,82 @@ #pragma once +#include +#include +#include + +class Foo { + int id; + char a{}; +public: + Foo(int a = 0, char b = ' ') { + std::cout << "Default constructor Foo:\n"; + id = a; + a = b; + }; + + ~Foo() { + std::cout << "Default destructor Foo:\n"; + }; + + int get_id() const { return id; } + + char get_char() const { return a; } + + static void *operator new(size_t size) { + std::cout << "Operator new for Foo:\n"; + auto m = malloc(size); + return m; + }; + + static void operator delete(void *m) { + std::cout << "Operator delete for Foo:\n"; + free(m); + }; +}; + +class Bar : public Foo { + int id1; +public: + Bar(int a = 0) { + std::cout << "Default constructor for Bar:\n"; + id1 = a; + }; + + ~Bar() { + std::cout << "Default destructor for Bar:\n"; + }; + + static void *operator new(size_t size) { + std::cout << "Operator new for Bar:\n"; + auto m = malloc(size); + return m; + }; + + static void operator delete(void *m) { + std::cout << "Operator delete for Bar:\n"; + free(m); + }; +}; + +class Buz : public Foo { + int id2; +public: + Buz(int a = 0) { + std::cout << "Default constructor for Buz:\n"; + id2 = a; + }; + + ~Buz() { + std::cout << "Default destructor for Buz:\n"; + }; + static void *operator new(size_t size) { + std::cout << "Operator new for Buz:\n"; + auto m = malloc(size); + return m; + }; + + static void operator delete(void *m) { + std::cout << "Operator delete for Buz:\n"; + free(m); + }; +}; \ No newline at end of file diff --git a/vectors/array.cpp b/vectors/array.cpp index 555ccad..2ee32f8 100644 --- a/vectors/array.cpp +++ b/vectors/array.cpp @@ -1,28 +1,127 @@ -#include "array.h" +#include using namespace std; -// Написать шаблонные функции: -// 1. operator + (реализация внутри класса) -// 2. operator * (вектор на скаляр и скаляр на вектор; реализация вне класса) -// 3. Нахождение среднего для двух векторов (реализация вне класса) +class Vector3d { + float data[3]; +public: + Vector3d() { data[2] = data[1] = data[0] = 0; } -ostream& operator <<(ostream& os, const Vector3d& v) { - return os << "{ " << v[0] << ", " << v[1] << ", " << v[2] << " }"; + 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 index) const { + cerr << "const" << index << endl; + return data[index]; + } + + float &operator[](int index) { + cerr << "non-const" << index << endl; + return data[index]; + } + + Vector3d operator+(const Vector3d &v2) { + Vector3d v3; + v3[0] = data[0] + v2[0]; + v3[1] = data[1] + v2[1]; + v3[2] = data[2] + v2[1]; + return v3; + } + + Vector3d operator*(const float value) { + Vector3d v3; + v3[0] = data[0] * value; + v3[1] = data[1] * value; + v3[2] = data[2] * value; + return v3; + } + + Vector3d operator-(const Vector3d &v2) { + Vector3d v3; + v3[0] = data[0] - v2[0]; + v3[1] = data[1] - v2[1]; + v3[2] = data[2] - v2[2]; + return v3; + } + + Vector3d operator/(const float value) { + if (value == 0) { + cout << "Value is not correct\n"; + return 0; + } + Vector3d v3; + v3[0] = data[0] / value; + v3[1] = data[1] / value; + v3[2] = data[2] / value; + return v3; + } + + Vector3d operator-() { return Vector3d(-data[0], -data[1], -data[2]); } + + Vector3d operator!() { + if (data[0] == data[1] == data[2] == 0) { + return Vector3d(1, 1, 1); + } + return Vector3d(0, 0, 0); + } +}; + +ostream &operator<<(ostream &os, const Vector3d &v) { + return os << "{ " << v[0] << ", " << v[1] << ", " << v[2] << " }"; } -template -ostream& operator <<(ostream& os, const Vector& v) { - os << "{ " << v[0]; - for (size_t i = 1; i < Dimensions; i++) - os << ", " << v[i]; - return os << " }"; +bool test_vector3d() { + Vector3d v1(4, 4, 4); + Vector3d v2(1, 1, 1); + Vector3d v3; + Vector3d v4(0, 0, 0); + v3 = v1 + v2; + if ((v3[0] != 5) || (v3[1] != 5) || (v3[2] != 5)) { + cerr << "The addition operation does not work correctly with data - v1(4,4,4), v2(1,1,1)"; + return false; + } + v3 = v1 - v2; + if ((v3[0] != 3) || (v3[1] != 3) || (v3[2] != 3)) { + cerr << "The subtract operation does not work correctly with data - v1(4,4,4), v2(1,1,1)"; + return false; + } + v3 = v1 * 3; + if ((v3[0] != 12) || (v3[1] != 12) || (v3[2] != 12)) { + cerr << "The operation of multiplying by a number does not work correctly with data - v1(4,4,4), 3"; + return false; + } + v3 = v1 / 4; + if ((v3[0] != 1) || (v3[1] != 1) || (v3[2] != 1)) { + cerr << "The operation of division by a number does not work correctly with data - v1(5,5,5), 4"; + return false; + } + v3 = -v1; + if ((v3[0] != -4) || (v3[1] != -4) || (v3[2] != -4)) { + cerr << "Unary minus does not work correctly with data - v1(5,5,5)"; + return false; + } + v3 = !v1; + if ((v3[0] != 0) || (v3[1] != 0) || (v3[2] != 0)) { + cerr << "Unary ! works incorrectly with data - v4(0,0,0)"; + return false; + } + v3 = !v4; + if ((v3[0] != 1) || (v3[1] != 1) || (v3[2] != 1)) { + cerr << "Unary ! works incorrectly with data - v1(1,1,1)"; + return false; + } + return true; } int main() { - Vector<12> v1; - v1[1] = v1[2] = 54; - Vector<12> v2 = v1; - cout << v2 << endl; - return 0; -} + if (test_vector3d()) { + return 0; + } else { + return 1; + } +} \ No newline at end of file