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

Кудряшов Михаил КМБО - 06 -21 #41

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
83 changes: 83 additions & 0 deletions animal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <iostream>
#include <sstream>
using namespace std;


class Animal {
private:
bool CanMoveFreely;
public:
Animal() { CanMoveFreely = 0; }
Animal(bool YesOrNo) { CanMoveFreely = YesOrNo; }
void setCanMoveFreely(bool YesOrNo) { CanMoveFreely = YesOrNo; }
bool get_CanMoveFreely() const { return CanMoveFreely; }
virtual string about() const { return (stringstream() << "CanMoveFreely =" << CanMoveFreely).str(); }

};


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

class Mammal : public Animal {
class Cat :public Mammal {
private:
int MiceCaughtCounter;
public:
float pregnancyDuration; // days
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 Cat : public Mammal {
class Horse :public Mammal {
private:
float kilometresRun;
public:
float vibrissaLength; // meters
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 Birds : public Animal {
private:
int FeathersCounter;
public:
Birds() { FeathersCounter = 0; }
Birds(int Feathers) { FeathersCounter = Feathers; }
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(); }
};
79 changes: 79 additions & 0 deletions animal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#pragma once
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
class Animal
{
private:
int age;
int weight;
int height;
public:
void animal(int age, int weight, int height) {
age = age;
height = height;
weight = weight;
}
void set_age(int age) { age = age; }
void set_height(float height) { height = height; }
void set_weight(float weight) { weight = weight; }

int get_age() { return age; }
float get_height() { return height; }
float get_weight() { return weight; }
virtual string about() const
{
stringstream ss;
ss << "age = " << get_age << ";"
<< "height = " << get_height << " ;"
<< "weight = " << get_weight << endl;

}

};

class Mammal :public Animal
{
private:
string hair_color;
bool meat_eater;
public:
void mammal(string hair_color, bool meat_eater) {
hair_color = hair_color;
meat_eater = meat_eater;
}
void set_hair_color() { hair_color = hair_color; }
void set_meat_eater() { meat_eater = meat_eater; }
string get_hair_color() { return hair_color; }
bool get_meat_eater() { return meat_eater; }
virtual string about() const
{
stringstream ss;
ss << "hair color is - " << get_hair_color << ";"
<< "he/she is meat eater? - " << get_meat_eater << endl;
}
};

class Human :public Mammal
{
private:
bool have_work;
string lifestyle;
public:
void human(bool have_work, string lifestyle) {
have_work = have_work;
lifestyle = lifestyle;
}
void set_work() { have_work = have_work; }
void set_life_style() { lifestyle = lifestyle; }
bool get_work() { return have_work; }
string get_life_style() { lifestyle = lifestyle; }
virtual string about() const
{
stringstream ss;
ss << " have work ? - " << get_work << ", "
<< "lifestyle = " << get_life_style;
return ss.str();
}
};
97 changes: 97 additions & 0 deletions electricy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include <iostream>
#include "electricity.h"

using namespace std;

bool Object::isConnectedTo(const Object& other) const
{
for (size_t j = 0; j < getPoleCount(); j++)
{
const Pole* selfPole = getPole(j);
if (selfPole != nullptr && (selfPole->connectedObject) == &other) return true;
}


return false;
}

bool Object::connect(const std::string& poleName, const Object& other, const std::string& otherPoleName)

{

if (poleName == otherPoleName && &other == this) return false;

Pole* selfPole = getPole(poleName);
Pole* otherPole = other.getPole(otherPoleName);
if (otherPole == nullptr || otherPole == nullptr) return false;

(selfPole->connectedObject) = &other;
(selfPole->connectedObjectPole) = otherPoleName;

(otherPole->connectedObject) = this;

(otherPole->connectedObjectPole) = poleName;

return true;
}

bool Object::disconnect(const std::string& poleName)
{
return false;
Pole* p = getPole(poleName);
if (p == nullptr) return false;

(p->connectedObject) = nullptr;
(p->connectedObjectPole) = "";

return true;
}



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


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; Light l; Switch sw3;

g.connect("p", l, "A1");
l.connect("A2", sw3, "A1");
cout << "is " << (g.isConnectedTo(l) ? "" : "not ") << "connected" << endl;
cout << "is " << (l.isConnectedTo(sw3) ? "" : "not ") << "connected" << endl;

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

return 0;

return 0;
}
102 changes: 102 additions & 0 deletions electricy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#pragma once
#include <string>
class Object;

struct Pole {

std::string name;

Object* connectedObject;

std::string connectedObjectPole;
Pole(const std::string& name_) : name(name_), connectedObject(nullptr) {}
};

class Object {
std::string name;
protected:
Object(const std::string& name_) : name(name_) {}

Pole* getPole(size_t idx) { return nullptr; }
Pole* getPole(size_t idx) {
return const_cast<Pole*>(const_cast<const Object*>(this)->getPole(idx));
}

public:
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; }


Pole* getPole(const std::string& name) { return const_cast<Pole*>(const_cast<const Object*>(this)->getPole(name)); }

virtual const Pole* getPole(const std::string& name) const = 0;

virtual size_t getPoleCount() const = 0;


bool connect(const std::string& poleName, Object& other, const std::string& otherPoleName);

bool disconnect(const std::string& poleName);
};

class Switch : public Object {
public:
Pole a1, a2;
Switch(const std::string& name = "");
virtual size_t getPoleCount() const { return 2; }
virtual const Pole* getPole(const std::string& name) const;
Copy link
Owner

Choose a reason for hiding this comment

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

Эту функцию перереализовывать не требовалось.

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

Choose a reason for hiding this comment

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

Нет реализации.

};


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

Light(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 {
if (name == a1.name) return &a1;
else if (name == a2.name) return &a2;

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

return nullptr;
}
};

class Generator : public Object {
public:
Pole phase, neural, earth;

Generator(const std::string& name = "") : Object(name), phase("p"), neural("n"), earth("e") {}

virtual size_t getPoleCount() const { return 3; }

virtual const Pole* getPole(const std::string& name) const {
if (name == phase.name) return &phase;
else if (name == neural.name) return &neural;
else if (name == earth.name) return &earth;

return nullptr;
}
protected:
virtual const Pole* getPole(size_t idx) const {
if (idx == 0) return &phase;
else if (idx == 1)return &neural;
else if (idx == 2)return &earth;

return nullptr;
}
};
Loading