Category : constructor

I’m currently learning C++ and would like to understand how constructors work in the context of inheritance. Here’s my parent and child classes: #include <iostream> #include <string> enum COLOR { Green, Blue, White, Black, Brown, }; class Animal { protected: std::string _name; COLOR _color; public: Animal(): _name("unknown"){ std::cout << "constructing Animal object " << _name ..

Read more

Node.cpp code : #include "Node.h" using namespace std; template <typename t> Node :: Node(t nodeDate){ data = nodeData; } Node.h code : #ifndef NODE_H #define NODE_H using namespace std; template <typename t> class Node{ public: Node(t nodeData); private: Node *nextNode; type data; friend class LinkedList; }; #endif Node :: Node(t nodeDate) in the cpp class ..

Read more

Is there any advantage of initializing data members of the class in a Non-Parameterized Constructor? Like, if it takes no parameters, initializing the class data members inside the Non-Parameterized constructor isn’t like initializing the values directly in class? Example: class persons { private: int age, id; public: persons(){ age = 2; id = 3; } ..

Read more