add AbstractClasses and a HeapMemory program.

the AbstractClasses problem experiments with abstract classes and the
HeapMemory program experiments with heap memory.
This commit is contained in:
Fries 2023-06-21 21:19:11 -07:00
parent 3405208295
commit 9a72aa2728
3 changed files with 65 additions and 0 deletions

38
src/AbstractClasses.cc Normal file
View file

@ -0,0 +1,38 @@
#include <iostream>
class Bean {
public:
virtual void print_type() {}
virtual void print_sus_level() {}
};
class Crewmate : public Bean {
void print_type() override
{
std::cout << "i am a crewmate. unsus." << std::endl;
}
void print_sus_level() override
{
std::cout << "sus levels are quite low" << std::endl;
}
};
class Imposter : public Bean {
void print_type() override
{
std::cout << "i am an imposter. very unsus." << std::endl;
}
void print_sus_level() override
{
std::cout << "sus levels are very high." << std::endl;
}
};
int main()
{
Bean* beans[2] = {new Crewmate, new Imposter};
for (Bean *bean : beans) {
bean->print_type();
bean->print_sus_level();
}
}

View file

@ -8,3 +8,5 @@ add_executable(enumerations enumerations.cc)
add_executable(namespaces namespaces.cc)
add_executable(FunctionArguments FunctionArguments.cc)
add_executable(exceptions exceptions.cc)
add_executable(HeapMemory HeapMemory.cc)
add_executable(AbstractClasses AbstractClasses.cc)

25
src/HeapMemory.cc Normal file
View file

@ -0,0 +1,25 @@
#include <cstdlib>
#include <iostream>
#include <string>
class Heap {
public:
Heap() {
sus = (std::string*)malloc(sizeof(std::string));
*sus = "sus";
}
~Heap() {
free(sus);
}
void print_string() {
auto susi = *(this->sus);
std::cout << *(this->sus) << std::endl;
}
private:
std::string* sus;
};
int main()
{
Heap* heap = new Heap();
heap->print_string();
delete heap;
}