-1

I want to create a base class with two set, and derived class (inherit) with one set method. I want to combine them in a parameterized constructor.

#include <iostream>
#include <string.h>

using namespace std;

class Code
{
   protected:
      string letter;
      int number;
   public:
      string getletter();
      int getnumber();
      void setletter(string letter1);
      void setnumber(int number1);
};

void Code::setletter(string letter1)
{
   letter=letter1;
}

void Code::setnumber(int number1)
{
   number=number1;
}

string Code::getletter()
{
   return letter;
}

int Code::getnumber()
{
   return number;
}

class Course : public Code
{
   private:
      string name;
   public:
      Course(string name1, string letter1, int number1);
      string getname();
      void show();  
};

Course::Course (string name1, string letter1,int number1) :
      setletter(letter1), setnumber(number1)    //Parameterized Constructor
{
   name=name1;
   letter=letter1;
   number=number1;
}

string Course::getname()
{
   return name;
}

void show()
{
   Course com("Testing","TST",101);
   cout<<"Constructor >>>\n Course Name : "<< com.getname()<<"\n Course Code : "<< com.getletter() << com.getnumber()<<endl;
}

int main()
{
   show();
}

If it matters, I am using Microsoft's Visual C++.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
el-musleh
  • 61
  • 1
  • 1
  • 12
  • 1
    You don't call functions after `:`, you can only initialize member variables. Put those calls into the function body. – Barmar May 19 '17 at 21:57
  • 1
    The easy way? Add a constructor to `Code` that sets `Code`'s members and call the new constructor in `Course`'s constructor's member initializer list with the parameters passed to `Course`'s constructor. – user4581301 May 19 '17 at 21:59
  • Anyways, aren't you doing the same thing with `setletter(letter1)` and `letter=letter1;`??? – LogicStuff May 19 '17 at 21:59
  • Don't use getter/setter functions that don't add anything over public member variables at all please. Blame the java people for the myth that this is a useful technique. – πάντα ῥεῖ May 19 '17 at 22:03

2 Answers2

0

You can call them inside your constructor

Course::Course (const string& name1, const string& letter1, const int number1) :
{
   name=name;
   setletter(letter1); // equivalent to letter=letter1 
   setnumber(number1);
}

You can add a constructor to your Code class

Code(const string& letter, const int number);

And call it like that in your Course constructor

Course::Course(const string& name1, const string& letter1, const int number1) 
: Code(letter1, number1), name(name1)
{

}

But I'm not sure of what your asking, if you can be a little more precise on what you want that could be great, thanks.

Noki
  • 383
  • 1
  • 9
  • _"You can't call methods (nor functions) in the constructor initializers list."_ Huh?? You're telling that again?? That's not true, you can call functions there to initialize any member variables. Please stop telling this. – πάντα ῥεῖ May 19 '17 at 22:18
  • 1
    You can't initialize members of the base class like that. That is not legal code. – R Sahu May 19 '17 at 22:20
-1

The : after the parameter list of a constructor is the 'member initializer list'.

Course::Course (string name1, string letter1,int number1)
: name(name1), letter(letter1), number(number1) {  }
da-chiller
  • 156
  • 1
  • 13