Skip to content

Commit

Permalink
add memhacks homework
Browse files Browse the repository at this point in the history
  • Loading branch information
grayed committed Mar 23, 2022
1 parent 23a1ddb commit 64b58fd
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ project ("kmbo--21")
# Include sub-projects.
add_subdirectory ("classwork")
add_subdirectory ("animals")
add_subdirectory ("memhacks")
add_subdirectory ("electricity")
add_subdirectory ("vectors")
5 changes: 5 additions & 0 deletions memhacks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cmake_minimum_required (VERSION 3.8)

add_executable (memhacks "memhacks.cpp" "memhacks.h")

# TODO: Add tests and install targets if needed.
66 changes: 66 additions & 0 deletions memhacks/memhacks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <iostream>
#include "memhacks.h"

using namespace std;

// 1. Реализовать в A функцию-геттер, возвращающую значение B::b_s.
// Используйте знание о расположении объектов в памяти.
//
// 2. Реализовать A::printData(), выводящую в поток текствое представление A::a_s, B::b_s и B::data
// с помощью A::getBString() и аналогичных техник.
//
// 3. Реализовать A::printData2(), выводящую в поток текствое представление A::a_s, B::b_s и B::data
// с помощью виртуальных функций.

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

/// <summary>
/// Выводит на экран адреса и размеры объекта типа <see cref="B"/> и его содержимого.
/// Можно модифицировать для собственных отладочных целей.
/// </summary>
/// <param name="b">Изучаемый объект</param>
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 << "B string is '" << b.getBString() << "'" << endl;
//cout << "B data: "; b.printData(cout); cout << endl;
}

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

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

/// <summary>
/// Извлекает значения <see cref="A::a_s"/>, <see cref="B::b_s"/> и <see cref="B::data"/>
/// из текущего объекта и выводит их в текстовом виде в указанный выходной поток
/// с помощью виртуальных функций, предусмотренных в классе <see cref="A"/>.
/// </summary>
void A::printData2(std::ostream& os) {
// TODO
}

int main()
{
B b;
printInternals(b);
return 0;
}
30 changes: 30 additions & 0 deletions memhacks/memhacks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <ostream>
#include <string>

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

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

friend void printInternals(const B&);

public:
std::string getBString() const;
void printData(std::ostream& os);
void printData2(std::ostream& os);
};

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

friend void printInternals(const B&);

public:
B();
};

void printInternals(const B& b);

0 comments on commit 64b58fd

Please sign in to comment.