Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

КМБО-03-21 Бакулин #51

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
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
76 changes: 71 additions & 5 deletions animals/animal.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,73 @@
#include "animal.h"

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

int main() {
return 0;
}
class Animal
{
private:
float height;
protected:
Animal(float _height);
public:
float getHeight() const { return height; }
void setHeight(float newValue) { height = newValue; }
virtual string about() const;
};
ostream& operator <<(ostream& os, const Animal& animal);
class Mammal : public Animal {
private:
float duration_of_pregnancy;
protected:
Mammal(float _height, float _duration_of_pregnancy);
public:
float getduration_of_pregnancy() const { return duration_of_pregnancy; }
void setduration_of_pregnancy(float newValue) { duration_of_pregnancy = newValue; }
virtual string about() const;
};
class birds : public Animal {
private:
float feather_lenght ;
protected:
birds(float _height, float _feather_lenght);
public:
bool getfeather_lenght() const { return feather_lenght; }
void setfeather_lenght(float newValue) { feather_lenght = newValue; }
virtual string about() const;
};
class Dog :public Mammal {
private:
float ear_lenght;
public:
Dog(float _height, float _duration_of_pregnancy, float ear_lenght);
float getear_lenght() const { return ear_lenght; }
void setear_lenght(float newValue) { ear_lenght = newValue; }
virtual string about() const;
};
class Cat :public Mammal {
private:
float vibrissaLength;
public:
Cat(float _height, float _duration_of_pregnancy, float _vibrissaLength);
float getvibrissaLength() const { return vibrissaLength;}
void setvibrissaLength(float newValue) { vibrissaLength = newValue; }
virtual string about() const;
};

class Martlet : public birds {
private:
float max_flight_altitude;
public:
Martlet(float _height, float _feather_lenght, float _max_flight_altitude);
float getmax_flight_altitude() const { return max_flight_altitude; }
void setmax_flight_altitude(float newValue) { max_flight_altitude = newValue; }
virtual string about() const;
};
class goose :public birds {
private:
float max_flight_duration;
public:
goose(float _height, float _feather_lenght, float _max_flight_duration);
float getmax_flight_duration() const { return max_flight_duration; }
void setmax_flight_duration(float newValue) { max_flight_duration = newValue; }
virtual string about() const;
};
86 changes: 74 additions & 12 deletions electricity/electricity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,61 @@ using namespace std;

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

bool Object::connect(const std::string& poleName, const Object& other, const std::string& otherPoleName)
{
// TODO
if (poleName == otherPoleName && &other == this) return false;
auto pole = getPole(poleName);
auto otherPole = other.getPole(otherPoleName);
if (pole == nullptr || otherPole == nullptr) return false;
otherPole->connectedObject = this;
otherPole->connectedObjectPole = poleName;
pole->connectedObject = (Object*)(&other);
pole->connectedObjectPole = otherPoleName;
return false;
}
bool Object::disconnect(const std::string &poleName)
{
auto pole = getPole(poleName);
if (pole == nullptr || pole->connectedObjectPole.empty() || pole->connectedObject == nullptr)
return false;
auto otherPoleName = pole->connectedObjectPole;
auto otherPole = pole->connectedObject->getPole(otherPoleName);
pole->connectedObjectPole = "";
pole->connectedObject = nullptr;
otherPole->connectedObjectPole = "";
otherPole->connectedObject = nullptr;
return true;
}
Switch::Switch(const std::string& name) : Object(name), a1("A1"), a2("A2") {}

Switch::Switch(const std::string& name)
: Object(name)
, a1("A1")
, a2("A2")
const Pole* Switch::getPole(const string& name) const
{
if (name == a1.name)
return &a1;
if (name == a2.name)
return &a2;
return nullptr;
}

const Pole* Switch::getPole(const string& name) const
const Pole* Switch::getPole(size_t idx) const
{
return getPole("A" + to_string(idx + 1));
}

Light::Light(const string &name) : Object(name), a1("A1"), a2("A2") {}

const Pole *Light::getPole(const string &name) const
{
if (name == a1.name)
return &a1;
Expand All @@ -31,19 +68,44 @@ const Pole* Switch::getPole(const string& name) const
return nullptr;
}

const Pole* Switch::getPole(size_t idx) const
const Pole *Light::getPole(size_t idx) const
{
// TODO
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;
}

int main()
const Pole *Generator::getPole(size_t idx) const
{
Switch sw, sw2;
sw.connect("A2", sw2, "A1");
cout << "is " << (sw.isConnectedTo(sw2) ? "" : "not ") << "connected" << endl;
return getPole("A" + to_string(idx + 1));
}

int main()
{
Generator gen = Generator("Generator 1");
Switch sw = Switch("Switch 1");
Light light = Light("Light 1");

gen.connect("A1", sw, "A1");
sw.connect("A2", light, "A1");
gen.connect("A2", light, "A2");

cout << "is " << (gen.isConnectedTo(sw) ? "" : "not ") << "connected" << endl;

gen.disconnect("A1");

// TODO: создать цепь из генератора, выключателя и светильника
cout << "is " << (gen.isConnectedTo(sw) ? "" : "not ") << "connected" << endl;

return 0;
}
31 changes: 25 additions & 6 deletions electricity/electricity.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once
#pragma once
#include <string>

class Object;
Expand Down Expand Up @@ -108,17 +108,36 @@ class Object {
/// Простой выключатель с двумя полюсами.
/// </summary>
class Switch : public Object {

public:
Pole a1, a2;

Switch(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;
};

virtual size_t getPoleCount() const { return 2; }

virtual const Pole* getPole(const std::string& name) const;
class Light : public Object {

public:
Pole a1, a2;
Light(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;
};

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:
virtual const Pole* getPole(size_t idx) const;
const Pole* getPole(size_t idx) const override;
};

// TODO: класс светильника с двумя полюсами
Expand Down
47 changes: 34 additions & 13 deletions memhacks/memhacks.cpp
Original file line number Diff line number Diff line change
@@ -1,52 +1,73 @@
#include <iostream>
#include "memhacks.h"
#include <iostream>
#include "Header.h"

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

B::B() : b_s("It's b!") {
B::B() : b_s("It's b!")
{
for (auto i = 0; i < sizeof(data) / sizeof(data[0]); i++)
data[i] = i * 2;
data[i] = (float)i * 2;
}

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

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

/// <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) const
{
os << "(using printData): A string is '" << a_s << "', B string is '" << getBString() << "'" << endl;
for (int i = 0; i < 7; ++i) os << getData(i) << " ";
}

/// <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) const
{
B b = *dynamic_cast<const B*>(this);
os << "(using printData2): A string is '" << a_s << "', B string is '" << b.getBString() << "'" << endl;
for (int i = 0; i < 7; ++i) os << b.getData(i) << " ";
}

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

#include <ostream>
#include <string>
Expand All @@ -9,22 +9,29 @@ class A {
std::string a_s;
int foo;

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

public:
std::string getBString() const;
void printData(std::ostream& os);
void printData2(std::ostream& os);
A();

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

void printData(std::ostream& os) const;
void printData2(std::ostream& os) const;
};

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

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

public:
B();

float getData(int idx) const override;
std::string getBString() const override;
};

void printInternals(const B& b);
Loading