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.

Revisions

  1. techtonik revised this gist May 21, 2016. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions getname.cpp
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,8 @@ class Driver {
    }
    };

    class SpecificDriver : Driver {
    // need public here to inherit init()
    class SpecificDriver : public Driver {
    public:
    std::string name = "Specific";

    @@ -20,8 +21,7 @@ int main() {
    Driver d;
    SpecificDriver sd;

    // now need to make it a part of parent init() method,
    // because it won't work in constructor
    // this gives Unknown Unknown =/
    d.init();
    std::cout << sd.name << std::endl;
    sd.init();
    }
  2. techtonik revised this gist May 21, 2016. 2 changed files with 10 additions and 6 deletions.
    4 changes: 4 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -4,3 +4,7 @@ with and without using templates.
    $ python getname.py
    Unknown
    Specific

    $ g++ -std=c++14 -O2 -Wall -pedantic main.cpp && ./a.out
    Unknown
    Specific
    12 changes: 6 additions & 6 deletions getname.cpp
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,13 @@
    // g++ -std=c++14 -O2 -Wall -pedantic main.cpp && ./a.out

    #include <iostream>
    #include <string>

    class Driver {
    public:
    std::string name = "Unknown";

    void init() {
    std::cout << name << std::endl;
    }
    };

    class SpecificDriver : Driver {
    @@ -19,9 +20,8 @@ int main() {
    Driver d;
    SpecificDriver sd;

    // now need to make it a part of parent
    // init() method (because it won't work
    // in constructor)
    std::cout << d.name << std::endl;
    // now need to make it a part of parent init() method,
    // because it won't work in constructor
    d.init();
    std::cout << sd.name << std::endl;
    }
  3. techtonik revised this gist May 21, 2016. 2 changed files with 18 additions and 1 deletion.
    4 changes: 4 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,6 @@
    See getname.py and try to implement the same in C++
    with and without using templates.

    $ python getname.py
    Unknown
    Specific
    15 changes: 14 additions & 1 deletion getname.cpp
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // g++ -std=c++14 -O2 -Wall -pedantic main.cpp && ./a.out

    #include <iostream>
    #include <string>

    @@ -7,8 +9,19 @@ class Driver {

    };

    class SpecificDriver : Driver {
    public:
    std::string name = "Specific";

    };

    int main() {
    Driver d;
    SpecificDriver sd;

    // now need to make it a part of parent
    // init() method (because it won't work
    // in constructor)
    std::cout << d.name << std::endl;
    }
    std::cout << sd.name << std::endl;
    }
  4. techtonik revised this gist May 21, 2016. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions getname.cpp
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    #include <iostream>
    #include <string>

    class Driver {
    @@ -6,5 +7,8 @@ class Driver {

    };

    int main() {
    }
    int main() {
    Driver d;

    std::cout << d.name << std::endl;
    }
  5. techtonik revised this gist May 21, 2016. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions getname.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    #include <string>

    class Driver {
    public:
    std::string name = "Unknown";

    };

    int main() {
    }
  6. techtonik revised this gist May 20, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    See getname.py and try to implement the same in C++
    with and without using templates.
  7. techtonik created this gist May 20, 2016.
    26 changes: 26 additions & 0 deletions getname.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    """
    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
    """