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 ?
– Isac
No. That was just an example, sorry if I didn’t pay attention to some details in the example
– JOsinaldo Wifi
This is my code just for the record. Only in a simpler way..
– JOsinaldo Wifi
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 }
– Isac
I’ll check on..
– JOsinaldo Wifi
This syntax presented by you I already knew but always gave error here.. (summarizing: I test and gave syntax error)
– JOsinaldo Wifi
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
– JOsinaldo Wifi
" 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.
– Isac
teach me how to implement in the right way, please!
– JOsinaldo Wifi