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
13 changes: 13 additions & 0 deletions animals/animal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,18 @@
using namespace std;

int main() {
Cat Doner(3, 60,"Ginger");
cout << Doner.about() << endl;

Capybara Mandarin(55, 120, 6);
cout << Mandarin.about() << endl;

Spider Boris(false, true, 7);
cout << Boris.about() << endl;

Scorpion Gregor(6, false, 2);
cout << Gregor.about() << endl;


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

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

class Animal {
private:
int age; //years

protected:
Animal(int a) { age = a; }

public:
float weight; // kg
void setAge(int years) { age = years; }
int getAge() const { return age; }
virtual string about() const;
};

class Mammal : public Animal {
public:
private:
float pregnancyDuration; // days

protected:
Mammal(int a, int days): Animal(a) { pregnancyDuration = days; }

public:
void setPregnancyDuration(int days) { pregnancyDuration = days; }
int getPregnancyDuration() const { return pregnancyDuration; }
virtual string about() const;
};

class Cat : public Mammal {
private:
string color;


public:
float vibrissaLength; // meters
Cat(int a, int days, string c) : Mammal(a, days) { color = c; }
void setColor(string col) { color = col; }
string getColor() const { return color; }
virtual string about() const;

};

class Capybara : public Mammal {
private:
float height; // meters
public:
Capybara(int a, int days, float meters) : Mammal(a, days) { height = meters; }
void setHeight(float meters) { height = meters;}
float getHeight() { return height; }
virtual string about() const;

};

class Arachnids : public Animal {
private:
bool livesInRussia;
protected:
Arachnids(int a, bool val) : Animal(a) { livesInRussia = val; }
public:
void setLives(bool val) { livesInRussia = val; }
bool getLives() { return livesInRussia; }
virtual string about() const;

};

class Spider : public Arachnids {
private:
bool poisonous;
public:
Spider(int a, bool live, bool val) : Arachnids(a, live) { poisonous = val; }
void setPoisonous(bool val) { poisonous = val; }
bool getPoisonous() { return poisonous; }
virtual string about() const;

};

class Scorpion : public Arachnids {
private:
int numOfEyes;
public:
Scorpion(int a, bool live, int eyes) : Arachnids(a, live) { numOfEyes = eyes; }
void setNumOfEyes(int num) { numOfEyes = num; }
virtual string about() const;
};

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

string Animal::about() const {
stringstream ss;
ss << "Age: " << age << " years\n";
return ss.str();
}

string Mammal::about() const {
stringstream ss;
ss << "Pregnancy duration: " << pregnancyDuration <<" days \n";
return ss.str();
}

string Cat::about() const {
stringstream ss;
ss << Animal::about() << Mammal::about() << "Color: " << color << endl;
return ss.str();
}

string Capybara::about() const {
stringstream ss;
ss << Animal::about() << Mammal::about() << "Height: " << height << endl;
return ss.str();
}

string Arachnids::about() const {
stringstream ss;
ss << "Does it live in Russia: " << livesInRussia << endl;
return ss.str();
}


string Spider::about() const {
stringstream ss;
ss << Animal::about() << Arachnids::about() << "Poisonous: " << poisonous << endl;
return ss.str();
}

string Scorpion::about() const {
stringstream ss;
ss << Animal::about() << Arachnids::about() << "Number of eyes: " << numOfEyes << endl;
return ss.str();
}

90 changes: 82 additions & 8 deletions electricity/electricity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,29 @@ using namespace std;

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

return false;
}

bool Object::connect(const std::string& poleName, Object& other, const std::string& otherPoleName)
{
// TODO
return false;
if (getPole(poleName) == nullptr || getPole(poleName)->name == otherPoleName) return false;
getPole(poleName)->connectedObject = &other;
getPole(poleName)->connectedObjectPole = otherPoleName;

return true;
}

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

return true;
}

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

const Pole* Switch::getPole(size_t idx) const
{
// TODO
if (idx == 0) {
return &a1;
}
else if (idx == 1) {
return &a2;
}
else {
return nullptr;
}
}

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
{
if (idx == 0) {
return &a1;
}
else if (idx == 1) {
return &a2;
}
else {
return nullptr;
}
}

const Pole* Generator::getPole(const string& name) const
{
if (name == p.name) //phase
return &p;
if (name == n.name) //neutral
return &n;
if (name == e.name) //earth
return &e;
return nullptr;
}

const Pole* Generator::getPole(size_t idx) const
{
if (idx == 0) {
return &p;
}
else if (idx == 1) {
return &n;
}
else if (idx == 2) {
return &e;
}

return nullptr;
}

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

Generator g; Lamp l; Switch s;

g.connect("Phase", s, "A1");
s.connect("A2", l, "A1");
l.connect("A2", g, "Neutral");


cout << "is " << (g.isConnectedTo(s) ? "" : "not ") << "connected" << endl;
cout << "is " << (s.isConnectedTo(l) ? "" : "not ") << "connected" << endl;
cout << "is " << (l.isConnectedTo(g) ? "" : "not ") << "connected" << endl;

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

return 0;
}
30 changes: 26 additions & 4 deletions electricity/electricity.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ 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) { const_cast<Pole*>(const_cast<const Object*>(this)->getPole(idx)); }

/// <summary>
/// Возвращает полюс по внутреннему индексу устройства.
Expand All @@ -63,7 +63,7 @@ class Object {
virtual ~Object() {}

const std::string& getName() const { return name; }
void getName(const std::string &newName) { name = newName; }
void setName(const std::string &newName) { name = newName; }

/// <summary>
/// Возвращает полюс по имени.
Expand Down Expand Up @@ -125,13 +125,35 @@ class Switch : public Object {
Switch(const std::string& name = "");

virtual size_t getPoleCount() const { return 2; }
virtual const Pole* getPole(const std::string& name) const;

protected:
virtual const Pole* getPole(size_t idx) const;
};

class Lamp : public Object {
public:
Pole a1, a2;

Lamp(const std::string& name = "") : Object(name), a1("A1"), a2("A2") {};

virtual size_t getPoleCount() const { return 2; }
virtual const Pole* getPole(const std::string& name) const;

protected:
virtual const Pole* getPole(size_t idx) const;

};

// TODO: класс светильника с двумя полюсами
class Generator : public Object {
public:
Pole p, n, e;

Generator(const std::string& name = "") : Object(name), p("Phase"), n("Neutral"), e("Earth") {};

virtual size_t getPoleCount() const { return 3; }
virtual const Pole* getPole(const std::string& name) const;

// TODO: класс генератора с тремя полюсами (фаза, нейтраль, земпя).
protected:
virtual const Pole* getPole(size_t idx) const;
};
Loading