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
102 changes: 100 additions & 2 deletions animals/animal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,104 @@

using namespace std;

int main() {
return 0;
Animal::Animal(float _weight, int _age) {
weight = _weight;
age = _age;
}

string Animal::about() const {
stringstream ss;
ss << "Weight: " << weight << " kg" << ", age: " << age << " years";
return ss.str();
}

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

Mammal::Mammal(float _weight, int _age, int _pregnancyLength) : Animal(_weight, _age) {
pregnancyLength = _pregnancyLength;
}

string Mammal::about() const {
stringstream ss;
ss << Animal::about();
ss << ", pregnancyLength: " << pregnancyLength << " days";
return ss.str();
}

Reptile::Reptile(float _weight, int _age, float _eggHeight) : Animal(_weight, _age) {
eggHeight = _eggHeight;
}

string Reptile::about() const {
stringstream ss;
ss << Animal::about();
ss << ", eggHeight: " << eggHeight << " centimeters";
return ss.str();
}

Cat::Cat(float _weight, int _age, int _pregnancyLength, float _vibrissaeLength) : Mammal(_weight, _age, _pregnancyLength) {
vibrissaeLength = _vibrissaeLength;
}

string Cat::about() const {
stringstream ss;
ss << Mammal::about();
ss << ", vibrissaeLength: " << vibrissaeLength << " centimeters";
return ss.str();
}

Dog::Dog(float _weight, int _age, int _pregnancyLength, string _breed) : Mammal(_weight, _age, _pregnancyLength) {
breed = move(_breed);
}

string Dog::about() const {
stringstream ss;
ss << Mammal::about();
ss << ", breed: " << breed;
return ss.str();
}

Turtle::Turtle(float _weight, int _age, float _eggHeight, float _shellSize) : Reptile(_weight, 4, _eggHeight) {
shellSize = _shellSize;
}

string Turtle::about() const {
stringstream ss;
ss << Reptile::about();
ss << ", shellSize: " << shellSize << " meters";
return ss.str();
}

Snake::Snake(float _weight, int _age, float _eggHeight, float _length) : Reptile(_weight, _age, _eggHeight) {
length = _length;
}

string Snake::about() const {
stringstream ss;
ss << Reptile::about();
ss << ", length: " << length << " meters";
return ss.str();
}

int main()
{
// tests:
Dog dog_1 = Dog(25, 4, 60, "Husky");
Cat cat_1 = Cat(10, 8, 65, 7);
Turtle turtle_1 = Turtle(100, 2, 8, 2.35);
Snake python = Snake(50, 12, 0.38, 5);

cout << "dog_1: " << dog_1.about() << endl;
cout << "cat_1: " << cat_1.about() << endl;
cout << "turtle_1: " << turtle_1.about() << endl;
cout << "python: " << python.about() << endl;
cout << "" << endl;
cout << "dog_1: " << dog_1 << endl;
cout << "cat_1: " << cat_1 << endl;
cout << "turtle_1: " << turtle_1 << endl;
cout << "python: " << python << endl;

return 0;
}
80 changes: 77 additions & 3 deletions animals/animal.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,92 @@
#pragma once

#include <iostream>
#include <sstream>
#include <utility>

class Animal {
protected:
float weight; // kg
int age; // years

Animal(float _weight, int _age);
public:
float weight; // kg
virtual std::string about() const = 0;

float get_weight() const { return weight; }
void set_weight(float _weight) { weight = _weight; }

int get_age() const { return age; }
void set_age(int _age) { age = _age; }

friend std::ostream& operator << (std::ostream &out, const Animal &animal);
};

class Mammal : public Animal {
protected:
int pregnancyLength; // days

Mammal(float _weight, int _age, int _pregnancyLength);
public:
std::string about() const override;

int get_pregnancyLength() const { return pregnancyLength; }
void set_pregnancyLength(int _pregnancyLength) { pregnancyLength = _pregnancyLength; }
};

class Reptile : public Animal {
protected:
float eggHeight; // centimeters

Reptile(float _weight, int _age, float _eggHeight);
public:
float pregnancyDuration; // days
std::string about() const override;

float get_eggHeight() const { return eggHeight; }
void set_eggHeight(float _eggHeight) { eggHeight = _eggHeight; }
};

class Cat : public Mammal {
private:
float vibrissaeLength; // centimeters
public:
float vibrissaLength; // meters
Cat(float _weight, int _age, int _pregnancyLength, float _vibrissaeLength);

std::string about() const override;
};

class Dog : public Mammal {
private:
std::string breed;
public:
Dog(float _weight, int _age, int _pregnancyLength, std::string _breed);

std::string about() const override;

std::string get_breed() const { return breed; }
void set_breed(std::string _breed) { breed = _breed; }
};

class Turtle : public Reptile {
private:
float shellSize; // meters
public:
Turtle(float _weight, int _age, float _eggHeight, float _shellSize);

std::string about() const override;

float get_shellSize() const { return shellSize; }
void set_shellSize(float _shellSize) { shellSize = _shellSize; }
};

class Snake : public Reptile {
private:
float length; // meters
public:
Snake(float _weight, int _age, float _eggHeight, float _length);

std::string about() const override;

float get_length() const { return length; }
void set_length(float _length ) { length = _length; }
};
81 changes: 74 additions & 7 deletions electricity/electricity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,37 @@ using namespace std;

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

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));
if (otherPole == nullptr || otherPole == nullptr) return false;
pole->connectedObject = (Object*)(&other);
pole->connectedObjectPole = otherPoleName;
otherPole->connectedObject = this;
otherPole->connectedObjectPole = poleName;
return true;
}

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

Switch::Switch(const std::string& name)
Expand All @@ -39,17 +56,67 @@ const Pole* Switch::getPole(const string& name) const

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;
Lamp lamp;
Switch sw3;

generator.connect("A1", lamp, "A1");
lamp.connect("A2", sw3, "A1");
cout << "is " << (generator.isConnectedTo(lamp) ? "" : "not ") << "connected" << endl;
cout << "is " << (lamp.isConnectedTo(sw3) ? "" : "not ") << "connected" << endl;

generator.disconnect("A1");
cout << "is " << (generator.isConnectedTo(lamp) ? "" : "not ") << "connected" << endl;

return 0;
}
33 changes: 32 additions & 1 deletion 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 @@ -135,3 +139,30 @@ class Switch : public Object {
// TODO: класс светильника с двумя полюсами

// TODO: класс генератора с тремя полюсами (фаза, нейтраль, земпя).
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;
};

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