diff --git a/animals/animal.cpp b/animals/animal.cpp index 6745215e..f46ccd3d 100644 --- a/animals/animal.cpp +++ b/animals/animal.cpp @@ -1,7 +1,19 @@ #include "animal.h" - +#include using namespace std; int main() { + Penguin Skipper(3, 0.4, false, 40, "Male"); + cout << "Skipper: " << Skipper.about() << endl << endl; + + Parrot Kesha(0.04, 0.18, true, 30, true); + cout << "Kesha: " << Kesha.about() << endl << endl; + + Dog Masyanya(30, 0.57, 63, 10, "White"); + cout << "Masyanya: " << Masyanya.about() << endl << endl; + + Human Vasilisa(50, 1.70, 280, 30, 126); + cout << "Vasilisa: " << Vasilisa.about() << endl << endl; + return 0; } \ No newline at end of file diff --git a/animals/animal.h b/animals/animal.h index 83a19442..820ae4cc 100644 --- a/animals/animal.h +++ b/animals/animal.h @@ -1,18 +1,110 @@ #pragma once - +#include #include +#include +using namespace std; class Animal { +private: + float weight; // kg + float height; // meters +protected: + Animal() {}; + Animal(float weight_, float height_) { weight = weight_; height = height_; } public: - float weight; // kg + float getWeight() const { return weight; } + void setWeight(float newWeight) { weight = newWeight; } + float getHeight() const { return height; } + void setHeight(float newHeight) { height = newHeight; } + + virtual string about() const { return (stringstream() << "Weight = " << weight << '\n' << "Height = " << height).str(); } }; class Mammal : public Animal { -public: +private: float pregnancyDuration; // days + float age; //years +protected: + Mammal() {}; + Mammal(float weight_, float height_, float pregnancyDuration_, float age_) :Animal(weight_, height_) { pregnancyDuration = pregnancyDuration_; age = age_; }; +public: + float getPregnancyDuration() const { return pregnancyDuration; } + void setPregnancyDuration(float newPregnancyDuration) { pregnancyDuration = newPregnancyDuration; } + float getAge() const { return age; } + void setAge(float newAge) { age = newAge; } + + virtual string about() const { return (stringstream() << Animal::about() << '\n' << "Duration of pregnancy = " << pregnancyDuration << '\n' << "Age = " << age).str(); } +}; + +class Dog : public Mammal { +private: + string woolcolor; //color +public: + Dog() {}; + Dog(float weight_, float height_, float pregnancyDuration_, float age_, string woolcolor_) :Mammal(weight_, height_, pregnancyDuration_, age_) { + woolcolor = woolcolor_; + } + + string getWoolcolor() const { return woolcolor; } + void setWoolcolor(string newWoolcolor) { woolcolor = newWoolcolor; } + + virtual string about() const { return (stringstream() << Mammal::about() << '\n' << "Woolcolor = " << woolcolor).str(); } +}; + +class Human : public Mammal { +private: + int iq; //scores +public: + Human() {}; + Human(float weight_, float height_, float pregnancyDuration_, float age_, int iq_) :Mammal(weight_, height_, pregnancyDuration_, age_) { + iq = iq_; + } + int getIQ() const { return iq; } + void setIQ(int newIQ) { iq = newIQ; } + + virtual string about() const { return (stringstream() << Mammal::about() << '\n' << "IQ = " << iq).str(); } }; -class Cat : public Mammal { +class Bird : public Animal { +private: + bool fly; // true/false + float wingspan; //centimeters +protected: + Bird() {}; + Bird(float weight_, float height_, bool fly_, float wingspan_) :Animal(weight_, height_) { fly = fly_; wingspan = wingspan_; }; public: - float vibrissaLength; // meters + bool getFly() const { return fly; } + void setFly(bool newFly) { fly = newFly; } + float getWingspan() const { return wingspan; } + void setWingspan(float newWingspan) { wingspan = newWingspan; } + + virtual string about() const { return (stringstream() << Animal::about() << '\n' << "Fly = " << fly << '\n' << "Wingspan = " << wingspan).str(); } }; + +class Penguin : public Bird { +private: + string sex; //male/female +public: + Penguin() {}; + Penguin(float weight_, float height_, bool fly_, float wingspan_, string sex_) :Bird(weight_, height_, fly_, wingspan_) { + sex = sex_; + } + string getSex() const { return sex; } + void setSex(string newSex) { sex = newSex; } + + virtual string about() const { return (stringstream() << Bird::about() << '\n' << "Sex = " << sex).str(); } +}; + +class Parrot : public Bird { +private: + bool speak; //true/false +public: + Parrot() {}; + Parrot(float weight_, float height_, bool fly_, float wingspan_, bool speak_) :Bird(weight_, height_, fly_, wingspan_) { + speak = speak_; + } + bool getSpeak() const { return speak; } + void setSpeak(bool newSpeak) { speak = newSpeak; } + + virtual string about() const { return (stringstream() << Bird::about() << '\n' << "Speak " << speak).str(); } +}; \ No newline at end of file diff --git a/memhacks/memhacks.cpp b/memhacks/memhacks.cpp index 514c8689..64024d1a 100644 --- a/memhacks/memhacks.cpp +++ b/memhacks/memhacks.cpp @@ -27,7 +27,7 @@ void printInternals(const B& b) { /// /// Значение B::b_s std::string A::getBString() const { - // TODO + return *((const string*)(this + 1)); } /// @@ -37,7 +37,15 @@ std::string A::getBString() const { /// Подразумевается, что текущий объект на самом деле представлено классом . /// void A::printData(std::ostream& os) { - // TODO + 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"; } /// @@ -46,7 +54,14 @@ void A::printData(std::ostream& os) { /// с помощью виртуальных функций, предусмотренных в классе . /// void A::printData2(std::ostream& os) { - // TODO + os << "A::a_s: " << *A::getString() << "\n"; + os << "B::b_s: " << *getString() << "\n"; + + const float* bData = getData(); + for (int i = 0; i < 7; ++i) { + os << bData[i] << " "; + } + os << "\n"; } int main() diff --git a/memhacks/memhacks.h b/memhacks/memhacks.h index 51076b55..a8b2276d 100644 --- a/memhacks/memhacks.h +++ b/memhacks/memhacks.h @@ -15,6 +15,11 @@ class A { 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 { @@ -25,6 +30,12 @@ class B : public A { public: B(); + virtual const std::string* getString() const { + return &b_s; + } + virtual const float* getData() const { + return data; + } }; void printInternals(const B& b); diff --git a/vectors/vector.cpp b/vectors/vector.cpp index 9777069c..fda386c4 100644 --- a/vectors/vector.cpp +++ b/vectors/vector.cpp @@ -1,25 +1,99 @@ #include -#include "vector.h" - +#include using namespace std; +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]; } + 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(data[0] = 1, data[1] = 1, data[2] = 1); + } + else + return vector3d(data[0] = 0, data[1] = 0, data[2] = 0); + } +}; 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; +bool test_vector3d() { + vector3d v1; + vector3d v2(3, 3, 3); + vector3d v3(4, 4, 4); + vector3d v4 = v2 + v3, v5 = v3 - v2, v6 = v2 * 4, v7 = v3 / 2, v8 = -v3, v9 = !v3, v10 = !v1; + + if ((v4[0] != 7) || (v4[1] != 7) || (v4[2] != 7)) { + cerr << " - v2(3,3,3), v3(4,4,4)"; + return false; + } + + if ((v5[0] != 1) || (v5[1] != 1) || (v5[2] != 1)) { + cerr << " - v3(4,4,4), v2(3,3,3)"; + return false; + } + + if ((v6[0] != 12) || (v6[1] != 12) || (v6[2] != 12)) { + cerr << " - v2(3,3,3), 4"; + return false; + } + + if ((v7[0] != 2) || (v7[1] != 2) || (v7[2] != 2)) { + cerr << " - v3(4,4,4), 2"; + return false; + } + + if ((v8[0] != -4) || (v8[1] != -4) || (v8[2] != -4)) { + cerr << " - v3(4,4,4)"; + return false; + } + + if ((v9[0] != 0) || (v9[1] != 0) || (v9[2] != 0)) { + cerr << " ! - v3(4,4,4)"; + return false; + } + + if ((v10[0] != 1) || (v10[1] != 1) || (v10[2] != 1)) { + cerr << " ! - v1(0,0,0)"; + return false; + } + return true; +} + +int main() { + setlocale(LC_ALL, "RUS"); + if (test_vector3d() == true) + { + return 0; + } + else + { + return 1; + }; }