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
102 changes: 102 additions & 0 deletions electricity.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;
protected:
virtual const Pole* getPole(size_t idx) const;
};


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;
}
};
71 changes: 71 additions & 0 deletions memhacks.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

using namespace std;

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

B::B() : b_s("It's b!") {
for (auto i = 0; i < sizeof(data) / sizeof(data[0]); i++)
data[i] = i * 2;
@@ -13,12 +15,13 @@ B::B() : b_s("It's b!") {



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 printData: "; b.printData(cout); cout << endl;
cout << "B data printData2: "; b.printData2(cout); cout << endl;
}


@@ -27,7 +30,19 @@ void printInternals(const B& b) {



std::string A::getBString() const {

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

std::string B::getBString() const {
return b_s;
}

float A::getData(int idx) const {
return ((float*)(this + 2))[idx];
}

float B::getData(int idx) const {
return data[idx];
}



@@ -37,7 +52,8 @@ std::string A::getBString() const {



void A::printData(std::ostream& os) {

os << "A string: " << a_s << ", B string: " << getBString() << ", data: ";
for (int i = 0; i < 7; ++i) os << getData(i) << " ";
}



@@ -46,7 +62,9 @@ void A::printData(std::ostream& os) {



void A::printData2(std::ostream& os) {

B b = *((B*)(this));
os << "A string: " << a_s << "', B string: " << b.getBString() << ", data: ";
for (int i = 0; i < 7; ++i) os << b.getData(i) << " ";
}

int main()
31 changes: 31 additions & 0 deletions memhacks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
std::string a_s;
int foo;

friend void printInternals(const B&);
friend void printInternals(B& b);

public:
std::string getBString() const;
A();

virtual std::string getBString() const;
virtual float getData(int idx) const;
void printData(std::ostream& os);
void printData2(std::ostream& os);
};
@@ -21,10 +24,13 @@ class B : public A {
std::string b_s;
float data[7];

friend void printInternals(const B&);
friend void printInternals(B& b);

public:
B();

virtual std::string getBString() const;
virtual float getData(int idx) const;
};

void printInternals(const B& b);
void printInternals(B& b);
52 changes: 52 additions & 0 deletions newhacks.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <iostream>
#include "newhacks.h"

using namespace std;

Foo::Foo() : cat(), frog() { cerr << "constructed Foo" << this << endl; }
Foo::~Foo() { cerr << "destructed Foo" << this << endl; }

void *Foo::operator new(size_t size) {
void *pVoid = malloc(size);
cerr << "operator new(" << size << ") returns " << pVoid << endl;
return pVoid;
}

void Foo::operator delete(void *pVoid) {
cerr << "operator delete(" << pVoid << ")" << endl;
free(pVoid);
}

Bar::Bar() : turtle(), dog() { cerr << "constructed Bar " << this << endl; }
Bar::~Bar() { cerr << "destructed Bar" << this << endl; }

void *Bar::operator new(size_t size) {
void *pVoid = malloc(size);
cerr << "operator new(" << size << ") returns " << pVoid << endl;
return pVoid;
}

void Bar::operator delete(void *pVoid) {
cerr << "operator delete(" << pVoid << ")" << endl;
free(pVoid);
}

Buz::Buz() : snake(), rhinoceros() { cerr << "constructed Buz" << this << endl; }
Buz::~Buz() { cerr << "destructed Buz " << this << endl; }

int main()
{
Foo foo_stack = Foo();
Bar bar_stack = Bar();
Buz buz_stack = Buz();

Foo* foo_heap = new Foo();
Bar* bar_heap = new Bar();


delete(foo_heap);
delete(bar_heap);

return 0;
}
}
36 changes: 36 additions & 0 deletions newhacks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <iostream>

class Foo {
int cat;
float frog;
public:
Foo();
~Foo();

static void *operator new(size_t size);
static void operator delete(void *pVoid);
};

class Bar : public Foo {
int turtle;
std::string dog;

public:
Bar();
~Bar();

static void *operator new(size_t size);
static void operator delete(void *pVoid);
};

class Buz : public Foo {
float snake;
short rhinoceros;

public:
Buz();
~Buz();

static void *operator new(size_t size) = delete;
static void operator delete(void *pVoid) = delete;
};
Loading