Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion animals/animal.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
#include "animal.h"

#include <string>
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;
}
102 changes: 97 additions & 5 deletions animals/animal.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,110 @@
#pragma once

#include <sstream>
#include <iostream>
#include <string>
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(); }
};
21 changes: 18 additions & 3 deletions memhacks/memhacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void printInternals(const B& b) {
/// </summary>
/// <returns>Значение B::b_s</returns>
std::string A::getBString() const {
// TODO
return *((const string*)(this + 1));
}

/// <summary>
Expand All @@ -37,7 +37,15 @@ std::string A::getBString() const {
/// Подразумевается, что текущий объект на самом деле представлено классом <see cref="B"/>.
/// </summary>
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";
}

/// <summary>
Expand All @@ -46,7 +54,14 @@ void A::printData(std::ostream& os) {
/// с помощью виртуальных функций, предусмотренных в классе <see cref="A"/>.
/// </summary>
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()
Expand Down
11 changes: 11 additions & 0 deletions memhacks/memhacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
108 changes: 91 additions & 17 deletions vectors/vector.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,99 @@
#include <iostream>
#include "vector.h"

#include <locale.h>
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;
};
}