Created
December 14, 2017 18:03
-
-
Save rebelmachina/15970069242d608ae02d3a8bc773edee to your computer and use it in GitHub Desktop.
templated class derivation example
This file contains hidden or 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 <cstdio> | |
| #include <utility> | |
| #include <iostream> | |
| template<class T> | |
| class Addition { | |
| protected: | |
| T a_; | |
| public: | |
| Addition (T a) : a_(a) {} | |
| T a () { return a_; } | |
| T op (const T& b) { return a_ + b; } | |
| }; | |
| template<class T> | |
| class Multiplication : public Addition<T> { | |
| public: | |
| Multiplication (T a) : Addition<T>(a) {} | |
| T op (const T& b) { | |
| return this->a_ * b; | |
| } | |
| }; | |
| int main(){ | |
| Addition<int> add(2); | |
| std::cout << add.op(3) << std::endl; | |
| Multiplication<int> mul(2); | |
| std::cout << mul.op(3) << std::endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment