0

I came across the scenario where the constructor of base class IBuy is not called. Ideally it should be called as it is inherited in shop class and shop is inherited in mall class. Please resolve this query

#include <iostream>

using namespace std;
class Ibuy
{
  public: 
    Ibuy(){"Ibuy default constructor \n";}
    virtual void nameItem()=0;
};
class shop: public Ibuy
{
  public:
  shop()
  {
    cout<<"def constructor of shop class \n";
  }
  void shopName(string name)
  {
     cout<<"Name of shop is: "<<name<<endl;
  }
};

class mall:public shop
{
 public:
  void nameItem()
 {
     cout<<"Item name... \n ";
 }
};

int main()
{

  cout<<"Abstract class \n";

  mall mObj;
  mObj.shopName("vishal mart");
  mObj.nameItem();
  return 0;

}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
mpp90
  • 1
  • 4
    `Ibuy(){"Ibuy default constructor \n";}` This doesn't print anything. – Mat Oct 20 '22 at 11:15
  • Look at `Ibuy(){"Ibuy default constructor \n";}` and think about what it prints for a few minutes. (As a hint, my g++ says "warning: statement has no effect") – molbdnilo Oct 20 '22 at 11:16
  • 1
    On a side note, the cause of mysterious problems is usually in a place where you're absolutely certain that it can't possibly be. You would probably have spotted this one immediately if you had written that constructor in a multi-line form like all the other functions. – molbdnilo Oct 20 '22 at 11:22
  • Side note: about [`using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)… – Aconcagua Oct 20 '22 at 11:27
  • 1
    don't ignore warnings https://godbolt.org/z/nGzxfh188 – 463035818_is_not_an_ai Oct 20 '22 at 11:53

0 Answers0