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
195 changes: 190 additions & 5 deletions animals/animal.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,192 @@
#include "animal.h"

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main() {
return 0;
}


class Animal {
private:
float the_average_value_of_the_duration_of_life;
public:
void set_the_average_value_of_the_duration_of_life(float new_the_average_value_of_the_duration_of_life) { the_average_value_of_the_duration_of_life = new_the_average_value_of_the_duration_of_life; }
float get_the_average_value_of_the_duration_of_life() const { return the_average_value_of_the_duration_of_life; }

bool Gender;
virtual string about() const;
protected:
Animal();

};

string Animal::about() const {
stringstream ss;
ss << "Gender = " << " " << Gender;
return ss.str();
};

class Mammal : public Animal {
private:
int number_of_individuals;
public:
void set_number_of_individuals(int new_number_of_individuals) { number_of_individuals = new_number_of_individuals; }
int get_number_of_individuals() const { return number_of_individuals; }

string Coat_color;
virtual string about() const;
protected:
Mammal();
};

string Mammal::about() const {
stringstream ss;
ss << Animal::about() << " " << " Coat_color = " << " " << Coat_color;
return ss.str();
};



class Quadrupeds : public Mammal {
private:
float percentage_of_quality_of_life;
public:
void set_percentage_of_quality_of_life(float new_percentage_of_quality_of_life) { percentage_of_quality_of_life = new_percentage_of_quality_of_life; }
float get_percentage_of_quality_of_life() const { return percentage_of_quality_of_life; }

bool limbs;
virtual string about() const;
};

string Quadrupeds::about() const {
stringstream ss;
ss << Animal::about() << " " << " limbs = " << " " << limbs;
return ss.str();
};


class Birds : public Mammal {
private:
int the_average_value_of_the_flight_for_one_season;
public:
void set_the_average_value_of_the_flight_for_one_season(int new_the_average_value_of_the_flight_for_one_season) { the_average_value_of_the_flight_for_one_season = new_the_average_value_of_the_flight_for_one_season; }
int get_the_average_value_of_the_flight_for_one_season() const { return the_average_value_of_the_flight_for_one_season; }


bool ability_to_fly;
virtual string about() const;
};

string Birds::about() const {
stringstream ss;
ss << Mammal::about() << " " << " ability_to_fly = " << " " << ability_to_fly;
return ss.str();
};

class Cat : public Animal {
private:
int the_number_of_mice_caught_per_unit_of_time;
public:
void set_the_number_of_mice_caught_per_unit_of_time(int new_the_number_of_mice_caught_per_unit_of_time) { the_number_of_mice_caught_per_unit_of_time = new_the_number_of_mice_caught_per_unit_of_time; }
int get_the_number_of_mice_caught_per_unit_of_time() const { return the_number_of_mice_caught_per_unit_of_time; }


float vibrissaLength;
virtual string about() const;
};

string Cat::about() const {
stringstream ss;
ss << Animal::about() << " " << " vibrissaLength = " << " " << vibrissaLength;
return ss.str();
};

class Manul : public Cat {
private:
int average_weight;
public:
void set_average_weight(int new_average_weight) { average_weight = new_average_weight; }
int get_average_weight() const { return average_weight; }


float Average_length_of_wool;
virtual string about() const;
};

string Manul::about() const {
stringstream ss;
ss << Cat::about() << " " << " Average_length_of_wool = " << " " << Average_length_of_wool;
return ss.str();
};

class Mainkun : public Cat {
private:
string eye_color;
public:
void set_eye_color(string new_eye_color) { eye_color = new_eye_color; }
string get_eye_color() const { return eye_color; }

float Number_of_fleas;
virtual string about() const;
};

string Mainkun::about() const {
stringstream ss;
ss << Cat::about() << " " << " Number_of_fleas = " << " " << Number_of_fleas;
return ss.str();
};


Animal::Animal()
: Gender()
, the_average_value_of_the_duration_of_life()
{
cerr << "" << endl;
}

Mammal::Mammal()
: number_of_individuals()
, Coat_color()
{
cerr << "" << endl;
}

inline ostream& operator <<(ostream& os, const Animal& animal) {
return os << animal.about();
}

int main(){
Mainkun kot_bOris;
Manul kot_Vasily;
Cat Roudi;
Birds ANgry_birds;
Quadrupeds dogs;

kot_bOris.Gender = true;
kot_bOris.Number_of_fleas = 50000;
kot_bOris.vibrissaLength = 5;

kot_Vasily.Average_length_of_wool = 3;
kot_Vasily.Gender = true;
kot_Vasily.vibrissaLength = 6;

Roudi.Gender = true;
Roudi.vibrissaLength = 4;

ANgry_birds.ability_to_fly = true;
ANgry_birds.Coat_color = "red";
ANgry_birds.Gender = false;

dogs.Coat_color = "red";
dogs.Gender = true;
dogs.limbs = 4;

cout << "-------------------------------------------------------------" << endl;
cout << "kot_bOris: " << kot_bOris << endl;
cout << "kot_Vasily: " << kot_Vasily << endl;
cout << "Roudi: " << Roudi << endl;
cout << "ANgry_birds: " << ANgry_birds << endl;
cout << "dogs: " << dogs << endl;
cout << "-------------------------------------------------------------";


}
101 changes: 88 additions & 13 deletions memhacks/memhacks.h
Original file line number Diff line number Diff line change
@@ -1,30 +1,105 @@
#pragma once
#pragma once

#include <ostream>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;

class B; // чтобы можно было объявить printInternals() как friend в классе A
class B;

class A {
std::string a_s;
string a_s;
int foo;

friend void printInternals(const B&);

friend void printInternals(B&);
public:
std::string getBString() const;
void printData(std::ostream& os);
void printData2(std::ostream& os);
A() : a_s("It's a!"), foo(0) { }

string getAString() const { return *((const string*)((const float*)(this) + 2)); }
string getBString() const {
return *((const string*)(this + 1));
}



float getdataFloat(size_t i) { return ((float*)(this + 2) - 4)[i]; }

virtual string aboutA() const {
stringstream ss;
ss << "String A: " << a_s;
return ss.str();
}
};



class B : public A {
std::string b_s;
string b_s;
float data[7];

friend void printInternals(const B&);

friend void printInternals(B&);
public:
B();
B() : b_s("It's b!") {
for (size_t i = 0; i < sizeof(data) / sizeof(data[0]); i++)
data[i] = (float)i * 2;
}
/// <summary>
/// Извлекает значение <see cref="B::b_s"/> из текущего объекта.
/// Подразумевается, что текущий объект на самом деле представлено классом <see cref="B"/>.
/// </summary>
/// <returns>Значение B::b_s</returns>


virtual string aboutB() {
stringstream ss;
ss << "String B: " << b_s << endl;
ss << "Data : ";
for (size_t i = 0; i < sizeof(data) / sizeof(data[0]); i++) { ss << data[i] << "; "; }
cout << endl;
return ss.str();
}




void printData2(ostream& os);
/// <summary>
/// Извлекает значения <see cref="A::a_s"/>, <see cref="B::b_s"/> и <see cref="B::data"/>
/// из текущего объекта и выводит их в текстовом виде в указанный выходной поток
/// с помощью адресной арифметики.
/// Подразумевается, что текущий объект на самом деле представлено классом <see cref="B"/>.
/// </summary>
void printData(ostream& os) {
os << "A string is '" << getAString() << " ', B string is ' " << getBString() << " ' " << endl;
for (size_t i = 0; i < sizeof(data) / sizeof(data[0]); ++i) os << getdataFloat(i) << " ";
}

};

void printInternals(const B& b);
/// <summary>
/// Извлекает значения <see cref="A::a_s"/>, <see cref="B::b_s"/> и <see cref="B::data"/>
/// из текущего объекта и выводит их в текстовом виде в указанный выходной поток
/// с помощью виртуальных функций, предусмотренных в классе <see cref="A"/>.
/// </summary>
void B::printData2(ostream& os) {
os << aboutA();
os << aboutB();
}


/// <summary>
/// Выводит на экран адреса и размеры объекта типа <see cref="B"/> и его содержимого.
/// Можно модифицировать для собственных отладочных целей.
/// </summary>
/// <param name="b">Изучаемый объект</param>
void printInternals(B& b) {
const A* a = &b, * a2 = a;
cerr << "-------------------" << endl;
cerr << "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 << std::endl;
cerr << "Size of A is " << sizeof(A) << ", size of B is " << sizeof(B) << endl;
cerr << "B string is '" << b.getBString() << "'" << endl;
cerr << "B data: "; b.printData(cout); cerr << endl;
cerr << "B data: "; b.printData2(cout); cerr << endl;
cerr << "-------------------" << endl;
}
72 changes: 56 additions & 16 deletions vectors/vector.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,65 @@
#include <iostream>
#include "vector.h"
#include <iso646.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; }
// lvalue = rvalue
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] << " }";
}

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;
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); }
vector3d operator -(const vector3d& v1) { return vector3d(-v1[0], -v1[1], -v1[2]); }
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);
}

bool test_vector3d() {
vector3d v1(10, 85, 110);
vector3d v2(20, 65, 90);

cout << "v1: " << "10, 85, 110" << endl;
cout << "v2: " << "20, 65, 90" << 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(); }