Category : constructor

In the following situation: class A { protected: int m_int; A() : m_int{-2} {}; public: A(const A& a) { m_int = a.get(); } A& operator=(const A& a) { m_int = a.get(); return *this; } int get() const { return m_int; } }; class B : public A { protected: using A::m_int; public: // same constructors ..

Read more

I’d like to introduce my own ‘exceptions’ into my code(I don’t want to use std::exception because I want to differentiate between std exceptions and my own exceptions – I’ll catch and handle them seperately), so I built a basic CPPException class: class CPPException { const char* source = nullptr; public: CPPException(const char* src) { source ..

Read more

#include<iostream> class Example{ int i=0,j=0; public: Example(){ std::cout<<"Default Constructor is called "<<j++<<std::endl; } ~Example(){ std::cout<<"Destructor is Called "<<i++<<std::endl; } void display(){ std::cout<<"Display method called "<<std::endl; } }; void function(){ Example e; e.display(); } int main(){ function(); Example e1; } I’m trying to see how the constructor and destructor are called for two objects of the ..

Read more

Let’s say someString initialization is a tad complicated, hence we write a simple member function stringInitialization() with which to initialize someString in the body of the constructor: class DemoL { private: int someNumber; std::string someString; void stringInitialization() { if (someNumber == 1) { someString = "FIRSTSTRING"; } else if (someNumber == 2) { someString = ..

Read more

I’m very new to programming and just starting out with C++ through a OOP course on the net. so, I’m trying to write a matrix class using templates and having a hard time with the constructor. I’m getting two errors: 1."no default constructor exists for class" 2."no default constructor exists for class" #include <iostream> #define ..

Read more