第一類是:虛擬功能不能正常工作
class SistemPornire{
protected:
Motor &_motor;
Electromotor &_electromotor;
public:
SistemPornire(Motor&,Electromotor&);
virtual void pornire_motor();
void opreste_motor();
};
虛函數的實現是:
SistemPornire::SistemPornire(Motor &motor, Electromotor &electromotor)
:_motor(motor), _electromotor(electromotor)
{
}
void SistemPornire::pornire_motor()
{
std::cout << "Sistemul de pornire a trimis comanda porneste_motor electromotorului." << std::endl;
this->_electromotor.start(_motor);
}
第二類:
class SistemPornireCuPreincalzire:public SistemPornire {
public:
SistemPornireCuPreincalzire(Motor&,Electromotor&);
void pornire_motor();
};
它的實現:
SistemPornireCuPreincalzire::SistemPornireCuPreincalzire(Motor&motor, Electromotor&electromotor)
: SistemPornire(motor, electromotor)
{
}
void SistemPornireCuPreincalzire::pornire_motor()
{
std::cout << "A inceput preincalzirea" <<std::endl<< "Preincalzirea incheiata" << std::endl;
std::cout << "Sistemul de pornire a trimis comanda porneste_motor electromotorului." << std::endl;
this->_electromotor.start(_motor);
}
在主函數中,我試圖調用函數pornire_motor()
來處理類型爲SistemPornireCuPreincalzire
的對象,但它會打印出來自SistemPornire::pornire_motor()
函數的消息。
你能告訴我我做錯了什麼?我提供的信息是否足夠?
class Autoturism {
private:
Electromotor electromotor;
Motor motor;
SistemPornire sistem_pornire;
SistemDirectie sistem_directie;
CutieViteze cutieviteze;
public:
Autoturism(SistemPornire&, Electromotor&, Motor&, SistemDirectie&);
void porneste_autoturism();
void condu_la_destinatie();
void parcheaza_autoturism();
};
Autoturism::Autoturism(SistemPornire &sp, Electromotor&e, Motor&m, SistemDirectie&sd): sistem_pornire(sp), electromotor(e), motor(m), sistem_directie(sd)
{
}
void Autoturism::porneste_autoturism()
{
std::cout << "Comanda porneste_autoturism a fost trimisa catre sistemul de pornire." << std::endl;
this->sistem_pornire.pornire_motor();
}
void Autoturism::condu_la_destinatie()
{
this->porneste_autoturism();
std::cout << "Odata ce masina a pornit, soferul o poate conduce la destinatie." << std::endl;
this->cutieviteze.gearUp();
this->sistem_directie.stanga(0);
this->cutieviteze.gearUp();
this->cutieviteze.gearUp();
this->cutieviteze.gearDown();
this->sistem_directie.stanga(90);
this->cutieviteze.gearUp();
this->sistem_directie.stanga(0);
this->cutieviteze.gearDown();
this->sistem_directie.dreapta(30);
this->sistem_directie.dreapta(0);
this->sistem_directie.dreapta(10);
std::cout << "Odata ce s-a ajuns la destinatie masina e gata de a fi parcata." << std::endl;
this->parcheaza_autoturism();
std::cout << "Soferul a ajuns la destinatie." << std::endl;
}
void Autoturism::parcheaza_autoturism()
{
std::cout << "Comanda parcheaza_autoturism a fost trimisa catre sistemul de pornire." << std::endl;
this->sistem_pornire.opreste_motor();
}
主要功能:
int main()
{
Motor motor;
Electromotor electromotor;
SistemPornire sistempornire(motor, electromotor);
SistemDirectie sistemdirectie;
SistemPornireCuPreincalzire sistempornireINC(motor, electromotor);
Autoturism masina(sistempornireINC, electromotor, motor,sistemdirectie);
std::cout << "Porneste autoturism:" << std::endl;
masina.porneste_autoturism();
std::cout << "Parcheaza autoturism:" << std::endl;
masina.parcheaza_autoturism();
std::cout << "Condu la destinatie:" << std::endl;
masina.condu_la_destinatie();
return 0;
}
我猜對象切片。沒有調用代碼就無法確定。 – chris 2013-03-12 21:57:42
您能構建一個證明這個問題的[minimal test-case](http://sscce.org)嗎? – 2013-03-12 21:57:50
顯示主要功能。 – Angew 2013-03-12 21:58:03