Home Forums C Programming Calling Base class constructor

Viewing 0 reply threads
  • Author
    Posts
    • #2001
      serkan
      Participant

      Hi,
      please see the following scenario.

      class base{
      float rate;
      public:
      base(){}
      };
      class derived : public base{
      int principal;
      int year;
      public:
      derived(){}
      derived(int _principal, int _year)//// I am not initializing the base class
      ////constructor
      };
      main()
      {
      derived d(1222,5);
      }

      I have not writtent he body of the constructors. My question is I have not used the statement like derived(int _principal, int _year):base(_principal,_year)
      and declared the default constructor in the base class i.e. base(){} so the compliler is calling that default constructor while I create the derived class constructor. Is there any draw back in this procedure……….?
      Or always I sould call the constructor explicitly while creating the derived calss constructor and adding one more construcotor in the base class like this.

      class base{
      float rate;
      public:
      base(){}
      base(int i, int j); ///////// additional constructor with 2 arguments
      };
      class derived : public base{
      int principal;
      int year;
      public:
      derived(){}
      derived(int _principal, int _year):base(_principal,_year)
      };

      Please suggest
       

Viewing 0 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.