[c++]代码库
#include <iostream>
using namespace std;
class ElectricMotor {
public:
ElectricMotor() {
}
;
virtual ~ElectricMotor() {
}
;
public:
void StartMotor() {
Accelerate();
Cruise();
}
void StopMotor() {
cout << "Motor stopped" << endl;
}
;
private:
void Accelerate() {
cout << "Motor started" << endl;
}
void Cruise() {
cout << "Motor running at constant speed" << endl;
}
};
class Fan: private ElectricMotor {
public:
Fan() {
}
;
~Fan() {
}
void StartFan() {
StartMotor();
}
void StopFan() {
StopMotor();
}
};
int main() {
Fan mFan;
mFan.StartFan();
mFan.StopFan();
/*
Note: the next two lines access the base class ElectricMotor
However, as Fan features 'private inheritance' from ElectricMotor,
neither the base class instance nor its public methods are
accessible to the users of class Fan.
Un-comment them to see a compile failure!
*/
// mFan.Accelerate ();
// ElectricMotor * pMotor = &mFan;
return 0;
}
by: 发表于:2018-02-01 09:51:04 顶(0) | 踩(0) 回复
??
回复评论