Skip to content

Instantly share code, notes, and snippets.

@xianbaum

xianbaum/helloable.cpp

Last active Dec 8, 2017
Embed
What would you like to do?
C++ equivalent of my helloable.c implementation to compare generated assembly for optimization
#include <stdio.h>
/*
helloable.cpp
THIS SOURCE FILE IS IN THE PUBLIC DOMAIN
This source file was used as a basis to help me understand
how vtables are implemented, so that I could implement
an interface pattern in C. Try decompiling the source code for this
and comparing it to the decompiled code for helloable.c.
In my decompiler, helloable.c produces less instructions than
this C++ implementation in all cases! In particular, compare the
for loop asm instructions in the main function in this source file to the
for loop asm instructions in the main function in helloable.c.
hellobable.c at:
https://gist.github.com/xianbaum/463f13a8d39014131b3f4fa67a241119
*/
class Helloable
{
public:
virtual void SayHello() = 0;
virtual void SayGoodbye(char *name) = 0;
};
class Being
{
public:
int weight;
int hunger_level;
char *name;
void PrintStats()
{
printf("Name\t\t\t%s\nWeight\t\t\t%d lbs\nHunger level\t\t%d\n", this->name, this->weight, this->hunger_level);
}
};
class Animal : public Being
{
public:
char *breed;
void PrintStats()
{
Being::PrintStats();
printf("Breed\t\t\t%s\n", this->breed);
}
};
class Dog : public Animal, public Helloable
{
public:
int barks_per_minute;
void PrintStats()
{
Animal::PrintStats();
printf("Barks per min\t\t%d\n", this->barks_per_minute);
}
virtual void SayHello()
{
printf("Woof!\n");
}
virtual void SayGoodbye(char *name)
{
printf("Woof-woof, from %s to %s\n", this->name, name);
}
};
class Cat : public Animal, public Helloable
{
public:
double purr_frequency;
void PrintStats()
{
Animal::PrintStats();
printf("Purr frequency\t\t%.2f Hz\n", this->purr_frequency);
}
virtual void SayHello()
{
printf("Meow!\n");
}
virtual void SayGoodbye(char *name)
{
printf("Meow-meow, from %s to %s\n", this->name, name);
}
};
class Person : public Being, public Helloable
{
public:
char *occupation;
void PrintStats()
{
Being::PrintStats();
printf("Occupation\t\t%s\n", this->occupation);
}
virtual void SayHello()
{
printf("Hello, world!\n");
}
virtual void SayGoodbye(char *name)
{
printf("Goodbye, from %s to %s\n", this->name, name);
}
};
int main()
{
Dog *dog = new Dog();
Cat *cat = new Cat();
Person *person = new Person();
Helloable *my_helloable_array[3];
dog->weight = 10;
dog->hunger_level = 5;
dog->name = "Libby";
dog->breed = "lab";
dog->barks_per_minute = 4;
cat->weight = 3;
cat->name = "Kitty";
cat->hunger_level = 0;
cat->breed = "tabby";
cat->purr_frequency = 21.98;
person->weight = 195;
cat->hunger_level = 1;
person->name = "Christian";
person->occupation = "Programmer";
my_helloable_array[0] = (Helloable*)dog;
my_helloable_array[1] = (Helloable*)cat;
my_helloable_array[2] = (Helloable*)person;
dog->PrintStats();
cat->PrintStats();
person->PrintStats();
for (int i = 0; i < 3; i++)
{
my_helloable_array[i]->SayHello();
my_helloable_array[i]->SayGoodbye("Jack");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.