每個類別裡面都有自己的功能,可以利用Composition,承接現有類別的功能。
而新的類別繼承全部功能後,可以再重定義其功能。
#include <iostream>
using namespace std;
class brake
{
public:
void condition_Brake()
{
cout<<"No ABS"<<endl;
}
};
class Crash
{
public:
void condition_Crash()
{
cout<<"No airbag"<<endl;
}
};
class car
{
private:
brake fun1;
Crash fun2;
public:
void CarBrake()
{
fun1.condition_Brake();
}
void CarCrash()
{
fun2.condition_Crash();
}
};
class ABScar : public car
{
public:
void CarBrake()
{
cout<<"I have abs"<<endl;
}
};
class Airbagcar : public car
{
public:
void CarCrash()
{
cout<<"I have air bag"<<endl;
}
};
int main()
{
ABScar carA;
Airbagcar carB;
cout<<"CarA:"<<endl;
carA.CarBrake();
carA.CarCrash();
cout<<"\nCarB:"<<endl;
carB.CarBrake();
carB.CarCrash();
return 0;
}
留言列表