I have a question about inherited default builders

Asked

Viewed 288 times

1

I am doubtful when trying to improve a POO exercise here..

(-) = without

I have the PAI class with two constructors (one is default). In the FILHO class I have two more constructors (no-default). In the definition of one of these constructors there are two conditions that initialize the constructors of the parent class, each in a given situation.

The problem is that when I instate a new child type object, with parameters to initiate parent constructor with parameters. It simply jumps the no-default constructor and matches the default, thus returning an unwanted value.

The simplified code is this one:

Header file--

class Pai{
int c;
public:
   int getC() //que retorna C
   Pai(){ c=100; }
   Pai(int arg){ c=arg; }
};

class Filho: public Pai{
public:
   Filho(int arg1)//esse não precisa atentar-se
   Filho(int arg1, int arg2);
};

Definition of child constructor----

Filho::Filho(int arg1){ //esse aqui é so representativo mas existe }

Filho::Filho(int arg1, arg2){
   if(arg2>0)
      Pai(arg2); //No-Default constructor 
   else{ Pai(); //Default constructor }
}

Instantiating object of child--

Filho *f1 = new Filho(3, 5); 

cout << "O valor de arg2 é: " << f1->getC() << endl;

Out -> 100 (returned the default constructor)

BUT. When I urge directly to the father is returned the desired value.

Why is he calling his father’s standard builder without my say-so?

I have researched in several places this and I did not find, I hope I find the answer here :/

  • This example has several syntax errors, from unwritten parameter types, incorrect keys, etc. It has written this way in your code ?

  • No. That was just an example, sorry if I didn’t pay attention to some details in the example

  • This is my code just for the record. Only in a simpler way..

  • Confirm the syntax you are using to call the base class, which I don’t think exists, and the problem probably lies there. It’s usually called that Derivada() : Base() { //implementação }

  • I’ll check on..

  • This syntax presented by you I already knew but always gave error here.. (summarizing: I test and gave syntax error)

  • But look at this.. even if this was right. Wouldn’t I have to have a default constructor in the derived class? where Derivative( ) //( ) default constructor

  • " I already knew but it was always wrong here" It’s because I didn’t implement it correctly. " Wouldn’t I have to have a default constructor in the derived class? " It is not required, but in the end it depends on how you are using/instantiating the objects in that class.

  • teach me how to implement in the right way, please!

Show 4 more comments

1 answer

1

What happens is that you are not modifying the values of your class.

Filho::Filho(int arg1, arg2){
if(arg2>0)
  Pai(arg2); //<- aqui está o erro, uma nova instancia da classe pai é criada
   else{ Pai(); //Default constructor }
}

In doing so you are actually creating a new instance of the parent class. In this case the solution would be to use a method instead of a constructor (eg setC(int c))

  • So when I urge Dad again I automatically call the default constructor?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.