inheritance
Jan 18, 2020 at 2:26am UTC
given:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
class Component {
public :
Component();
virtual ~Component();
void logicFunction();
void setInputs( Component &);
char getOutput() const ;
Component* leftChild();
Component* rightChild();
//void setState(bool&);//serve per vettore input: essendo vettore component, serve il metodo che viene implementato in constant component
protected :
char _present_state,_previous_state;
Component *_x1,*_x2;
float _lh,_hl;// consumo da low a high e da high a low
private :
};
class And : public Component{
public :
And();
virtual ~And();
void logicFunction();
void setInputs( Component&, Component&);
protected :
private :
//Component *_x2;
} ;
char toChar(bool x){
char c;
if (x) c='1' ;
else c='0' ;
return c;
}
bool toBool(char c){
bool x;
if (c='1' ) x=true ;
else x=false ;
return x;
}
Component ::Component() {
//ctor
}
Component ::~Component() {
//dtor
}
void Component :: logicFunction() {}
void Component ::setInputs( Component &a){
_x1=&a;
}
char Component ::getOutput() const {
return _present_state;
}
Component* Component :: leftChild(){
return _x1;
}
Component* Component :: rightChild(){
return _x2;
}
And ::And(){
_x1= nullptr ;
_x2= nullptr ;
}
And :: ~And() {
//dtor
}
void And::logicFunction() {
_previous_state=_present_state;
if ((_x1->getOutput()=='x' )||(_x2->getOutput()=='x' )) _present_state='x' ;
else _present_state=toChar(toBool(_x1->getOutput())&&toBool(_x2->getOutput()));
}
void And::setInputs( Component &a, Component &b) {
_x1=&a;
_x2=&b;
}
if I try:
Last edited on Jan 18, 2020 at 2:42am UTC
Jan 18, 2020 at 2:29am UTC
1 2 3 4 5 6
postOrder(Component* node) {
if (node== nullptr ) return ;
postOrder(node->leftChild());
postOrder(node->rightChild());
node->logicFunction();
}
If I have a And object, when I try to do node->logicFunction(), I think it's executed Component::logicFunction() instead of And::logicFunction()..
why this?
Jan 18, 2020 at 2:46am UTC
Have you heard of virtual functions? If not, look it up.
You need to make logicFunction virtual in Component.
virtual void logicFunction();
Jan 18, 2020 at 2:54am UTC
Yes I tried, but if do:
virtual void logicFunction();
I receive error: undefined reference to `Component::logicFunction()'
instead with:
virtual void logicFunction()=0;
It doesn't work..
Jan 18, 2020 at 3:03am UTC
I don't understand the problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#include <iostream>
class Component {
public :
Component();
virtual ~Component();
virtual void logicFunction();
};
Component::Component() {}
Component::~Component() {}
void Component::logicFunction() { std::cout << "Component\n" ; }
class And : public Component {
public :
And();
~And();
void logicFunction() override;
};
And::And() {}
And::~And() {}
void And::logicFunction() { std::cout << "And\n" ; }
int main() {
Component *comp = new And;
comp->logicFunction(); // prints "And"
}
Last edited on Jan 18, 2020 at 3:04am UTC
Jan 18, 2020 at 5:28pm UTC
done. Thank you very much.
Topic archived. No new replies allowed.