第一类是:
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;
}