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

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

int main() {
return 0;
Cat Scottish(5);
Horse Spirit(1234);
Scottish.set_MiceCaughtCounter(7);
Spirit.setkilometresRun(1500.5);
int res = Scottish.get_MiceCaughtCounter();
float res2 = Spirit.get_kilometresRun();
cout << res << " " << res2 << "\n";
return 0;
}
78 changes: 72 additions & 6 deletions animals/animal.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,84 @@
#pragma once

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


class Animal {
private:
bool CanMoveFreely;
protected:
Animal() { CanMoveFreely = 0; }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не выполнен пункт задания с использованием protected.

Animal(bool YesOrNo) { CanMoveFreely = YesOrNo; }
public:

void setCanMoveFreely(bool YesOrNo) { CanMoveFreely = YesOrNo; }
bool get_CanMoveFreely() const { return CanMoveFreely; }
virtual string about() const { return (stringstream() << "CanMoveFreely =" << CanMoveFreely).str(); }

};

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нет перегрузки операции вывода в поток для Animal.


class Mammal :public Animal {
private:
bool FeedsWithMilk;
protected:
Mammal() { FeedsWithMilk = 0; }
Mammal(bool FeedOrNot) { FeedsWithMilk = FeedOrNot; }
public:
void setFeedsWithMilk(bool FeedOrNot) { FeedsWithMilk = FeedOrNot; }
bool get_FeedsWithMilk() const { return FeedsWithMilk; }
virtual string about() const { return (stringstream() << Animal::about() << ", " << "FeedsWithMilk =" << FeedsWithMilk).str(); }
};

class Cat :public Mammal {
private:
int MiceCaughtCounter;

public:
float weight; // kg
Cat(int MiceCaught) { MiceCaughtCounter = MiceCaught; }
void set_MiceCaughtCounter(int MiceCaught) { MiceCaughtCounter = MiceCaught; }
int get_MiceCaughtCounter() const { return MiceCaughtCounter; }
virtual string about() const { return (stringstream() << Animal::about() << ", " << Mammal::about() << ", " << "MiceCaughtCounter =" << MiceCaughtCounter).str(); }
};

class Mammal : public Animal {
class Horse :public Mammal {
private:
float kilometresRun;
public:
float pregnancyDuration; // days
Horse(float distance) { kilometresRun = distance; }
void setkilometresRun(float distance) { kilometresRun = distance; }
float get_kilometresRun() const { return kilometresRun; }
virtual string about() const { return (stringstream() << Animal::about() << ", " << Mammal::about() << ", " << "kilometresRun =" << kilometresRun).str(); }
};

class Cat : public Mammal {

class Birds : public Animal {
private:
int FeathersCounter;
protected:
Birds() { FeathersCounter = 0; }
Birds(int Feathers) { FeathersCounter = Feathers; }
public:
float vibrissaLength; // meters
void setFeathersCounter(int Feathers) { FeathersCounter = Feathers; }
int get_FeathersCounter() const { return FeathersCounter; }
virtual string about() const { return (stringstream() << Animal::about() << ", " << "FeathersCounter =" << FeathersCounter).str(); }
};
class Pigeon :public Birds {
private:
bool CanBegForBread;
public:
Pigeon(int CheekyOrNot) { CanBegForBread = CheekyOrNot; }
bool get_CanBegForBread() const { return CanBegForBread; }
void setCanBegForBread(int CheekyOrNot) { CanBegForBread = CheekyOrNot; }
virtual string about() const { return (stringstream() << Animal::about() << ", " << Birds::about() << ", " << "CanBegForBread =" << CanBegForBread).str(); }
};
class Caliber :public Birds {
private:
int WingBeatSpeedPerSecond;
public:
Caliber(int BeatCounter) { WingBeatSpeedPerSecond = BeatCounter; }
int get_WingBeatSpeedPerSecond() const { return WingBeatSpeedPerSecond; }
void setWingBeatSpeedPerSecond(int BeatCounter) { WingBeatSpeedPerSecond = BeatCounter; }
virtual string about() const { return (stringstream() << Animal::about() << ", " << Birds::about() << ", " << "WingBeatSpeed =" << WingBeatSpeedPerSecond).str(); }
};
86 changes: 71 additions & 15 deletions electricity/electricity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,40 @@ using namespace std;

bool Object::isConnectedTo(const Object& other) const
{
// TODO
return false;
for (int i = 0; i < getPoleCount(); ++i) {
auto pole = getPole(i);
if (pole != nullptr && pole->connectedObject == &other)
return true;
}
}

bool Object::connect(const std::string& poleName, Object& other, const std::string& otherPoleName)
{
// TODO
return false;
}
{
if (poleName == otherPoleName && &other == this)
return false;

auto pole = getPole(poleName);
auto otherPole = (Pole*)(other.getPole(otherPoleName));

pole->connectedObject = (Object*)(&other);
pole->connectedObjectPole = otherPoleName;
otherPole->connectedObject = this;
otherPole->connectedObjectPole = poleName;

return true;
}


bool Object::disconnect(const std::string& poleName)
{
// TODO
if (getPole(poleName)->connectedObject != nullptr)
{
getPole(getPole(poleName)->connectedObjectPole)->connectedObject = nullptr;
getPole(getPole(poleName)->connectedObjectPole)->connectedObjectPole = "";
getPole(poleName)->connectedObject = nullptr;
getPole(poleName)->connectedObjectPole = "";
return true;
}
return false;
}

Expand All @@ -30,26 +51,61 @@ Switch::Switch(const std::string& name)

const Pole* Switch::getPole(const string& name) const
{
if (name == a1.name)
return &a1;
if (name == a2.name)
return &a2;
if (name == a1.name) return &a1;
if (name == a2.name) return &a2;
return nullptr;
}

const Pole* Switch::getPole(size_t idx) const
{
// TODO
return getPole("A" + to_string(idx + 1));
}
Lamp::Lamp(const string& name)
: Object(name)
, a1("A1")
, a2("A2")
{
}
const Pole* Lamp::getPole(const string& name) const {
if (name == a1.name) return &a1;
if (name == a2.name) return &a2;
return nullptr;
}

const Pole* Lamp::getPole(size_t idx) const {
return getPole("A" + to_string(idx + 1));
}

Generator::Generator(const string& name)
: Object(name)
, a1("A1")
, a2("A2")
, a3("A3")
{
}
const Pole* Generator::getPole(const string& name) const {
if (name == a1.name) return &a1;
if (name == a2.name) return &a2;
if (name == a3.name) return &a3;
return nullptr;
}
const Pole* Generator::getPole(size_t idx) const {
return getPole("A" + to_string(idx + 1));
}

int main()
{
Switch sw, sw2;
sw.connect("A2", sw2, "A1");
cout << "is " << (sw.isConnectedTo(sw2) ? "" : "not ") << "connected" << endl;

// TODO: создать цепь из генератора, выключателя и светильника

Generator generator_test;
Lamp lamp_test;
Switch switch_test;
generator_test.connect("A1", switch_test, "A2");
switch_test.connect("A1", lamp_test, "A2");
cout << "is " << (generator_test.isConnectedTo(lamp_test) ? "" : "not ") << "connected" << endl;
cout << "is " << (lamp_test.isConnectedTo(switch_test) ? "" : "not ") << "connected" << endl;
generator_test.disconnect("A1");
cout << "is " << (generator_test.isConnectedTo(lamp_test) ? "" : "not ") << "connected" << endl;
return 0;
}
27 changes: 23 additions & 4 deletions electricity/electricity.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ class Object {
/// </summary>
/// <param name="idx">Индекс полюса, от <c>0</c> до значения, возвращаемого <see cref="getPoleCount()"/>.</param>
/// <returns>Полюс с указанным индексом, или <c>nullptr</c>, если такой полюс не существует.</returns>
Pole* getPole(size_t idx) { /* TODO */ return nullptr; }
Pole* getPole(size_t idx) {
if (getPole("A" + std::to_string(idx)))
return getPole("A" + std::to_string(idx));
return nullptr;
}

/// <summary>
/// Возвращает полюс по внутреннему индексу устройства.
Expand Down Expand Up @@ -131,7 +135,22 @@ class Switch : public Object {
protected:
virtual const Pole* getPole(size_t idx) const;
};
class Lamp : public Object {
public:
Pole a1, a2;
Lamp(const std::string& name = "");
size_t getPoleCount() const override { return 2; }
const Pole* getPole(const std::string& name) const override;
protected:
const Pole* getPole(size_t idx) const override;
};

// TODO: класс светильника с двумя полюсами

// TODO: класс генератора с тремя полюсами (фаза, нейтраль, земпя).
class Generator : public Object {
public:
Pole a1, a2, a3;
Generator(const std::string& name = "");
size_t getPoleCount() const override { return 3; }
const Pole* getPole(const std::string& name) const override;
protected:
const Pole* getPole(size_t idx) const override;
};
43 changes: 32 additions & 11 deletions memhacks/memhacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,38 @@
#include "memhacks.h"

using namespace std;
A::A() : a_s("It's a!"), foo(0) {}


B::B() : b_s("It's b!") {
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;
std::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 << std::endl;
std::cout << "Size of A is " << sizeof(A) << ", size of B is " << sizeof(B) << std::endl;
std::cout << "B string is '" << b.getBString() << "'" << std::endl;
std::cout << "B data: "; const_cast<B*>(&b)->printData2(std::cout); std::cout << std::endl;
}

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

string B::getBString() const {
return b_s;
}

/// <summary>
Expand All @@ -36,17 +42,32 @@ std::string A::getBString() const {
/// с помощью адресной арифметики.
/// Подразумевается, что текущий объект на самом деле представлено классом <see cref="B"/>.
/// </summary>
void A::printData(std::ostream& os) {
// TODO
void A::printData(ostream& os) {
os << "a_s is " << a_s << std::endl;
os << "b_s is " << getBString() << endl;
float* b_Data = ((float*)(((string*)(this + 1)) + 1));
os << "data is: ";
for (auto i = 0; i < sizeof(b_Data) - 1; i++)
{
os << b_Data[i] << " ";
}
os << endl;
}

/// <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
os << "a_s is " << a_s << std::endl;
os << "b_s is " << getBString() << std::endl;
float* b_Data = getBData();
os << "data is: ";
for (auto i = 0; i < sizeof(b_Data) - 1; i++)
{
os << b_Data[i] << " ";
}
os << std::endl;
}

int main()
Expand Down
18 changes: 9 additions & 9 deletions memhacks/memhacks.h
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
#pragma once

#include <ostream>
#include <string>

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

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

friend void printInternals(const B&);
friend void printInternals(B& b);

public:
A();
std::string getBString() const;
virtual float getBData(int idx) const;
void printData(std::ostream& os);
void printData2(std::ostream& os);
virtual float* getBData() = 0;
};

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

friend void printInternals(const B&);
friend void printInternals(B& b);

public:
B();

std::string getBString() const;
virtual float getBData(int idx) const;
};

void printInternals(const B& b);
void printInternals(B& b);
Loading