uCell_c U;
vCell_c V;
U.calculateStep3();//will this call uCell_c::calculateStep3, and then call cell_c::calculateStep2, and then call uCell_c::calculateStep1?
V.calculateStep2();//will this call vCell_c::calculateStep3, and then call cell_c::calculateStep2, and then call vCell_c::calculateStep1?
U.calculateStep3();//will this call uCell_c::calculateStep3, and then call cell_c::calculateStep2, and then call uCell_c::calculateStep1?
No.
uCell_c::calculateStep3 calls cell_c::calculateStep2.
cell_c::calculateStep2 calls calculateStep1() which is declared virtual, but a virtual override implementation doesn't exist in the code shown. This should cause a linker VTBL error.
V.calculateStep2();//will this call vCell_c::calculateStep3, and then call cell_c::calculateStep2, and then call vCell_c::calculateStep1?
No.
#include <iostream>
usingnamespace std;
class cell_c {
public:
cell_c();
virtualvoid calculateStep1();
void calculateStep2();
virtualvoid calculateStep3();
~cell_c();//compiler error: Class 'cell_c' has virtual method 'calculateStep3' but non-virtual destructor
};
class uvCell_c:public cell_c {
public:
uvCell_c();
~uvCell_c();//compiler error: Class 'uvCell_c' has virtual method 'calculateStep3' but non-virtual destructor
};
class uCell_c:public uvCell_c {
public:
inlinevoid calculateStep1() {cout<<"uCell"<<endl;}
void calculateStep3();
uCell_c();
~uCell_c();//compiler error: Class 'uCell_c' has virtual method 'calculateStep3' but non-virtual destructor
};
class vCell_c:public uvCell_c {
public:
inlinevoid calculateStep1() {cout<<"vCell"<<endl;}
void calculateStep3();
vCell_c();
~vCell_c();//compiler error: Class 'vCell_c' has virtual method 'calculateStep3' but non-virtual destructor
};
void cell_c::calculateStep2() {
calculateStep1();
}
void uCell_c::calculateStep3() {
calculateStep2();
cout<<"step3 u"<<endl;
}
void vCell_c::calculateStep3() {
calculateStep2();
cout<<"step3 v"<<endl;
}
int main() {
uCell_c U;//compiler error: undefined reference to `uCell_c::uCell_c()'
vCell_c V;//compiler error: Multiple markers at this line
// - undefined reference to
// `vCell_c::~vCell_c()'
// - undefined reference to
// `vCell_c::vCell_c()'
U.calculateStep3();//I want to see "uCell step3 u" print out
V.calculateStep3();//I want to see "vCell step3 v" print out
return 0;
}
The compiler error and what I want to do is written in comments, please help out! I think I don't know how to define a class with virtual member, and destructor stuff. Thanks.