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: 14 additions & 0 deletions animals/animal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
90 changes: 87 additions & 3 deletions animals/animal.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,102 @@
#pragma once

#include <iostream>
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";
}
};
65 changes: 34 additions & 31 deletions memhacks/memhacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// <summary>
/// Выводит на экран адреса и размеры объекта типа <see cref="B"/> и его содержимого.
/// Можно модифицировать для собственных отладочных целей.
/// </summary>
/// <param name="b">Изучаемый объект</param>
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;
}

/// <summary>
/// Извлекает значение <see cref="B::b_s"/> из текущего объекта.
/// Подразумевается, что текущий объект на самом деле представлено классом <see cref="B"/>.
/// </summary>
/// <returns>Значение B::b_s</returns>
std::string A::getBString() const {
// TODO
return *((const string *) (this + 1));
}

/// <summary>
/// Извлекает значения <see cref="A::a_s"/>, <see cref="B::b_s"/> и <see cref="B::data"/>
/// из текущего объекта и выводит их в текстовом виде в указанный выходной поток
/// с помощью адресной арифметики.
/// Подразумевается, что текущий объект на самом деле представлено классом <see cref="B"/>.
/// </summary>
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";
}

/// <summary>
/// Извлекает значения <see cref="A::a_s"/>, <see cref="B::b_s"/> и <see cref="B::data"/>
/// из текущего объекта и выводит их в текстовом виде в указанный выходной поток
/// с помощью виртуальных функций, предусмотренных в классе <see cref="A"/>.
/// </summary>
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;
}
33 changes: 22 additions & 11 deletions memhacks/memhacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
22 changes: 18 additions & 4 deletions memhacks/newhacks.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
#include <iostream>
#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;
}
80 changes: 80 additions & 0 deletions memhacks/newhacks.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,82 @@
#pragma once

#include <sys/types.h>
#include <cstdlib>
#include <iostream>

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);
};
};
Loading