File features.h

  1#include <cmath>
  2#include <iostream>
  3#include <vector>
  4
  5
  6//-----
  7unsigned int gUint = 0;
  8
  9//-----
 10class Abstract {
 11public:
 12    virtual ~Abstract() {}
 13    virtual void abstract_method() = 0;
 14    virtual void concrete_method() = 0;
 15};
 16
 17void Abstract::concrete_method() {
 18    std::cout << "called Abstract::concrete_method" << std::endl;
 19}
 20
 21//-----
 22class Concrete : Abstract {
 23public:
 24    Concrete(int n=42) : m_int(n), m_const_int(17) {}
 25    ~Concrete() {}
 26
 27    virtual void abstract_method() {
 28        std::cout << "called Concrete::abstract_method" << std::endl;
 29    }
 30
 31    virtual void concrete_method() {
 32        std::cout << "called Concrete::concrete_method" << std::endl;
 33    }
 34
 35    void array_method(int* ad, int size) {
 36        for (int i=0; i < size; ++i)
 37            std::cout << ad[i] << ' ';
 38        std::cout << '\n';
 39    }
 40
 41    void array_method(double* ad, int size) {
 42        for (int i=0; i < size; ++i)
 43            std::cout << ad[i] << ' ';
 44        std::cout << '\n';
 45    }
 46
 47    void uint_ref_assign(unsigned int& target, unsigned int value) {
 48        target = value;
 49    }
 50
 51    Abstract* show_autocast() {
 52        return this;
 53    }
 54
 55    operator const char*() {
 56        return "Hello operator const char*!";
 57    }
 58
 59public:
 60    double m_data[4];
 61    int m_int;
 62    const int m_const_int;
 63
 64    static int s_int;
 65};
 66
 67typedef Concrete Concrete_t;
 68
 69int Concrete::s_int = 321;
 70
 71void call_abstract_method(Abstract* a) {
 72    a->abstract_method();
 73}
 74
 75//-----
 76class Abstract1 {
 77public:
 78    virtual ~Abstract1() {}
 79    virtual std::string abstract_method1() = 0;
 80};
 81
 82class Abstract2 {
 83public:
 84    virtual ~Abstract2() {}
 85    virtual std::string abstract_method2() = 0;
 86};
 87
 88std::string call_abstract_method1(Abstract1* a) {
 89    return a->abstract_method1();
 90}
 91
 92std::string call_abstract_method2(Abstract2* a) {
 93    return a->abstract_method2();
 94}
 95
 96//-----
 97int global_function(int) {
 98    return 42;
 99}
100
101double global_function(double) {
102    return std::exp(1);
103}
104
105int call_int_int(int (*f)(int, int), int i1, int i2) {
106    return f(i1, i2);
107}
108
109template<class A, class B, class C = A>
110C multiply(A a, B b) {
111    return C{a*b};
112}
113
114//-----
115namespace Namespace {
116
117    class Concrete {
118    public:
119        class NestedClass {
120        public:
121            std::vector<int> m_v;
122        };
123
124    };
125
126    int global_function(int i) {
127        return 2*::global_function(i);
128    }
129
130    double global_function(double d) {
131        return 2*::global_function(d);
132    }
133
134} // namespace Namespace
135
136//-----
137enum EFruit {kApple=78, kBanana=29, kCitrus=34};
138enum class NamedClassEnum { E1 = 42 };
139
140//-----
141void throw_an_error(int i);
142
143class SomeError : public std::exception {
144public:
145    explicit SomeError(const std::string& msg) : fMsg(msg) {}
146    const char* what() const throw() override { return fMsg.c_str(); }
147
148private:
149    std::string fMsg;
150};
151
152class SomeOtherError : public SomeError {
153public:
154    explicit SomeOtherError(const std::string& msg) : SomeError(msg) {}
155    SomeOtherError(const SomeOtherError& s) : SomeError(s) {}
156};