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
162 changes: 162 additions & 0 deletions animals1
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#pragma once

#include <iostream>
#include <sstream>
#include <string>

class Animal {
private:
int Number_life; //years
public:
float weight; // kg
Animal(){};
Animal(int Number_life)
{
Number_life = Numberl;
}
void setNumber_life(int Numberl) {Numberl = Number_life;}
int getNumber_life() {return Numberl;}
virtual string about()
{
stringctream str;
str << "Number of life = " << Numberl;
return str.str();
}
};

class Mammal : public Animal {
private:
int Fertility; //number per year
public:
float pregnancyDuration; // days
Mammal(){};
Mammal(int Fertility)
{
fert = Fertility;
}
void setFertility(int Fertility) {fert = Fertility;}
int getFertility() {return fert;}
virtual string about()
{
stringstream str;
str << "Fertility = " << fert;
return str.str();
}
};

class Cat : public Mammal {
class Amphibia : public Animal {
private:
bool Virulence; //poisonous or not
public:
float vibrissaLength; // meters
Amphibia(){};
Amphibia(bool Virulence)
{
toxic = Virulence;
}
bool getVirulence() {return toxic;}
void setVirulence(bool value) {toxic = value;}
virtual string about()
{
stringstream ss;
ss << "Virulence = " << toxic;
return ss.str();
}
};

class Dog : public Mammal {
private:
string Сoat_color; //colour
public:
Dog(){};
Dog(string Сoat_color)
{
сolor = Сoat_color;
}
string getСoat_color() {return сolor;}
void setСoat_color(string value) {сolor = value;}
virtual string about()
{
stringstream ss;
ss << "Сoat color = " << getСoat_color();
return ss.str();
}
};

class Husky : public Dog {
private:
int Barking_volume; //decibel
public:
Husky() {}
Husky(int Barking_volume)
{
Barking = Barking_volume;
}
int getBarking_volume() {return Barking;}
void setBarking_volume(int value) { Barking = value; }
virtual string about()
{
stringstream ss;
ss << "Barking volume = " << getSpeed() << "; "
<< "Сoat color = " << getСoat_color() << endl;
return ss.str();
}
}

class Сamel : public Mammal {
private:
int Number_humps; //amount
public:
Сamel() {}
Сamel(int Number_humps)
{
Humpsk = Number_humps;
}
void setNumber_humps(int Humpsk) {Humpsk = Number_humps;}
float getNumber_humps() {return Humpsk;}
virtual string about()
{
stringstream str;
str << "Number of humps = " << Humpsk;
return str.str();
}
};

class Snake : public Amphibia {
private:
bool Molt; //was or not
public:
Snake(){};
Snake(bool Molt)
{
molt = Molt;
}
bool getMolt() {return molt;}
void setMolt(bool value) {molt = value;}
virtual string about()
{
stringstream ss;
ss << "Was there a molt = " << getMolt();
return ss.str();
}
};

class Frog : public Amphibia {
private:
int Age; //years
public:
Frog() {}
Frog(int Age)
{
age = Age;
}
int getAge() {return age;}
void setAge(int age) {age = Age;}
virtual string about()
{
stringstream str;
str << "Age = " << age << "; "
<< "Number of life = " << Numberl << endl;
return str.str();
}
27 changes: 27 additions & 0 deletions animalss
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

#include " animals1"

#include <string>
using namespace std;

int main() {
return 0;
}
int main()
{
Dog Vlad("brown");
Vlad.setСoat_color("black");
string aboutDog = Vlad.about();
cout << "Vlad : " << aboutDog << endl;

Snake Gleb(true);
Gleb.setMolt(false);
string MoltGleb = Gleb.about();
cout << "Snake : " << MoltGleb << endl;

Frog Vasya(26, 40);
Vasya.setAge(137);
Vasya.setNumber_life(752);
string truth_about_Vasya = Vasya.about();
cout << Vasya : " << truth_about_Vasya << endl;
}
91 changes: 91 additions & 0 deletions memhacks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <iostream>
#include <sstream>
#include "memhacks.h"

using namespace std;

A::A() : a_s("It's a!") {}

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

/// <summary>
@@ -17,27 +21,52 @@ 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 << "A string is '" << b.getAString() << "'" << endl;
cout << "B string is '" << b.getBString() << "'" << endl;
//cout << "B data: "; b.printData(cout); cout << endl;
}

string A::getAString() const {
return *((const string*) ((const long*) (this) + 1));
}

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

/// <summary>
/// Извлекает значения <see cref="A::a_s"/>, <see cref="B::b_s"/> и <see cref="B::data"/>
/// из текущего объекта и выводит их в текстовом виде в указанный выходной поток
/// с помощью адресной арифметики.
/// Подразумевается, что текущий объект на самом деле представлено классом <see cref="B"/>.
/// </summary>

float A::getdataFloat(int i) const { return ((float*)(this + 2) - 4)[i]; }

void A::printData(std::ostream& os) {
// TODO
os << getAString() + " " + getBString() + " {";
for (int i = 0; i < 7; ++i) {
os << getdataFloat(i);
if (i != 6) os << ", ";
}
os << "}" << endl;
}

string A::getA_s() const { return a_s; }
string B::getB_s() const { return b_s; }
string B::getDataStr() const {
stringstream ss;
ss << "{ ";

for (int i = 0; i < 7; ++i) {
ss << data[i];
if (i != 6) ss << "," << " ";
}
ss << '}';

return ss.str();
}

/// <summary>
@@ -46,12 +75,14 @@ void A::printData(std::ostream& os) {
/// с помощью виртуальных функций, предусмотренных в классе <see cref="A"/>.
/// </summary>
void A::printData2(std::ostream& os) {
// TODO
os << getA_s() << " " << getB_s() << " " << getDataStr() << endl;
}

int main()
{
B b;
printInternals(b);
b.printData(cout);
b.printData2(cout);
return 0;
}
36 changes: 36 additions & 0 deletions memhacks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once
#include <ostream>
#include <string>
class B;
class A {
std::string a_s;
int foo;

friend void printInternals(const B&);
virtual std::string getB_s() const { return " "; };
virtual std::string getDataStr() const { return " "; };

public:
std::string getAString() const;
std::string getBString() const;
float getdataFloat(int i) const;
void printData(std::ostream& os);
void printData2(std::ostream& os);

std::string getA_s() const;

A();
};

class B : public A {
class B : public A {
friend void printInternals(const B&);

public:
std::string getB_s() const override;
std::string getDataStr() const override;

B();
};

void printInternals(const B& b);
Loading