Last active
December 8, 2017 02:40
-
-
Save xianbaum/1407f6a63ed826e4f42e7100c38977e9 to your computer and use it in GitHub Desktop.
C++ equivalent of my helloable.c implementation to compare generated assembly for optimization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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