Skip to content

Instantly share code, notes, and snippets.

@rebelmachina
Created December 14, 2017 18:03
Show Gist options
  • Select an option

  • Save rebelmachina/15970069242d608ae02d3a8bc773edee to your computer and use it in GitHub Desktop.

Select an option

Save rebelmachina/15970069242d608ae02d3a8bc773edee to your computer and use it in GitHub Desktop.
templated class derivation example
#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