Skip to content

Instantly share code, notes, and snippets.

@techtonik
Last active May 21, 2016 07:40
Show Gist options
  • Select an option

  • Save techtonik/ce85e2e5e1773e02f91cb8e1156d049a to your computer and use it in GitHub Desktop.

Select an option

Save techtonik/ce85e2e5e1773e02f91cb8e1156d049a to your computer and use it in GitHub Desktop.
Driver inheritance pattern - parent should print child's name

See getname.py and try to implement the same in C++ with and without using templates.

#include <iostream>
#include <string>
class Driver {
public:
std::string name = "Unknown";
};
int main() {
Driver d;
std::cout << d.name << std::endl;
}
"""
This Python code contains two driver classes - parent constructor prints
name of current driver. This is an example that should be ported to C++.
Code is placed into public domain.
"""
class Driver(object):
name = "Unknown"
def __init__(self):
print(self.name)
class SpecificDriver(Driver):
name = "Specific"
def __init__(self):
super(SpecificDriver, self).__init__()
Driver()
SpecificDriver()
"""
This prints two strings to console
Unknown
Specific
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment