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
122 changes: 118 additions & 4 deletions animals/animal.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,121 @@
#include "animal.h"

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

int main() {
return 0;
}
class Animal
{
private:
int Age; //Years
public:
float height; //Meters
void setAge(int getAge) { Age = NewAge; }
int getAge() const { return Age; }
virtual string about() const;
};
string Animal::about() const
{
stringstream ss;
ss << "Age = " << " " << Age;
return ss.str();
}

class Mammal : public Animal
{
private:
float PawLength; //centimeters
public:
void setPawLength(float NewPawLength) { PawLength = NewPawLength; }
float getPawLength() const { return PawLength; }
virtual string about() const;
};
string Mammal::about() const
{
stringstream ss;
ss << Animal::about() << " " << "PawLength = " << " " << PawLength;
return ss.str();
}

class Bird : public Animal
{
private:
int FlightDuration; //minutes
public:
float BeakSize; // days
void setFlightDuration(int newFlightDuration) { FlightDuration = newFlightDuration; }
int getFlightDuration() const { return FlightDuration; }
virtual string about() const;
};
string Bird::about() const
{
stringstream ss;
ss << Animal::about() << " " << "FlightDuration = " << " " << FlightDuration;
return ss.str();
}

class Capybara : public Mammal
{
private:
float FrontTeethLength; //centimeters
public:
void setFrontTeethLength(int newFrontTeethLength) { FrontTeethLength = newFrontTeethLength; }
int getFrontTeethLength() const { return FrontTeethLength; }
virtual string about() const;
};
string Capybara::about() const
{
stringstream ss;
ss << Mammal::about() << " " << "FrontTeethLength = " << " " << FrontTeethLength;
return ss.str();
}

class Caracal : public Mammal
{
private:
int TailLength; //centimeters
public:
void setTailLength(int newTailLength) { TailLength = newTailLength; }
int getTailLength() const { return TailLength; }
virtual string about() const;
};
string Caracal::about() const
{
stringstream ss;
ss << Mammal::about() << " " << "TailLength = " << " " << TailLength;
return ss.str();
}

class Raven : public Bird
{
private:
int WingLength; //centimeters
public:
void setWingLength(int newWingLength) { WingLength = newWingLength; }
int getWingLength() const { return WingLength; }
virtual string about() const;
};
string Raven::about() const
{
stringstream ss;
ss << Bird::about() << " " << "WingLength = " << " " << WingLength;
return ss.str();
}

class Ostrich : public Bird
{
private:
float EggWeight; //kg
public:
void setEggWeight(float newEggWeight) { EggWeight = newEggWeight; }
float getEggWeight() const { return EggWeight; }
virtual string about() const;

};
string Ostrich::about() const
{
stringstream ss;
ss << Bird::about() << " " << "EggWeight = " << " " << EggWeight;
return ss.str();
}

15 changes: 14 additions & 1 deletion memhacks/memhacks.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <iostream>
#include <iostream>
#include "memhacks.h"

using namespace std;
Expand Down Expand Up @@ -55,3 +55,16 @@ int main()
printInternals(b);
return 0;
}

int main()
{
A a;
B b;
cout << &a << "\n" << &b << "\n";
printInternals(b);
cout << "MEMHACKS:" << endl;
b.printData(cout);
cout << endl << "STANDART:" << endl;
b.printData2(cout);
return 0;
}
68 changes: 64 additions & 4 deletions memhacks/memhacks.h
Original file line number Diff line number Diff line change
@@ -1,30 +1,90 @@
#pragma once

#pragma once
using namespace std;
#include <ostream>
#include <string>
#include <sstream>
#include <iostream>

class B; // чтобы можно было объявить printInternals() как friend в классе A
class B;

class A {
class A
{
std::string a_s;
string a_s;
int foo;

friend void printInternals(const B&);

friend void printInternals(B&);
public:
std::string getBString() const;
void printData(std::ostream& os);
void printData2(std::ostream& os);
A() : a_s("It's a!"), foo(0) { }

string getAString() const { return *((const string*)((const float*)(this + 1) - 12)); }

string getBString() const {
return *((const string*)(this + 1));
}


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

virtual string about_A() const {
stringstream ss;
ss << "String A: " << a_s;
return ss.str();
}
};

class B : public A {


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

friend void printInternals(const B&);

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

virtual string about_B() {
stringstream ss;
ss << "String B: " << b_s << endl;
ss << "Data : ";
for (int i = 0; i < 7; i++) {
ss << data[i] << "; ";
}
cout << endl;
return ss.str();
}

void printData2(std::ostream& os) {
os << about_A();
os << about_B();
}
void printData(std::ostream& os) {
os << "A string is '" << getAString() << "', B string is '" << getBString() << "'" << endl;
for (int i = 0; i < 7; ++i) os << getdataFloat(i) << " ";
}

};

void printInternals(const B& b);
void printInternals(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(std::cout); cout << endl;
cout << "B data: "; b.printData2(std::cout); cout << endl;
}
Loading