Category : inheritance

I’m having the following classes: template<typename T> class t_node { public: t_node* parent, * left, * right; int key; public: t_node(int key, t_node* parent = nullptr) :key(key), parent(parent), left(nullptr), right(nullptr) {} }; class node : public t_node<node> { public: node(int key, node* parent = nullptr) :t_node(key, parent) {} }; I want to inherit multiple node ..

Read more

#include <iostream> #include <string> using namespace std; class Shape { private: string name; public: Shape(){name = "Shape";} Shape(string n){name = n;} void setName(string n){name = n;} string getName(){return name;} double calcarea(){return 0;} }; class Square : public Shape { private: double side; public: Square(){side = 1; Shape("Square");} Square(double s){side = s; Shape("Square");} void setSide(double s){side ..

Read more

I’m building a 2D platformer game, and every entity in my game inherits from class Entity. I have a std::vector<std::unique_ptr<Entity>> entities to dynamically allocate these entities. I want to also polymorphically check for collision from the driver code: for(int i = 0; i < entities.size();i++){ for(int j = 0; j < entities.size();j++) entities[i]->checkCollision(*j); } The ..

Read more

How to write constructor for class that base class name is from template? as I try to compile code below then i get this error undefined reference to >IMU<ADXL>::IMU(int) In function `_GLOBAL__sub_I_rdy’ >>>main.cpp<<< IMU<ADXL335> obj(8); >>>IMU.h<<< template <class T> class IMU : public T { public: const int someMember=0; IMU(int); }; >>>IMU.cpp<<< template <class T> ..

Read more

I have a c++ class in a header file: class Component { public: static const char identifier = ‘_’; static bool represented_by(const std::string& token_str); }; The represented_by method is implemented in a separate file: bool Component::represented_by(const std::string &token_str) { return token_str.rfind(identifier, 0) == 0; } When I create an instance of a subclass of Component ..

Read more