贪吃的长颈鹿
长颈鹿是一种动物:动物需要吃食物,因此长颈鹿也需要吃食物;长颈鹿还可以伸长脖子,可以用嘴够树叶吃。请建立Animal类和Giraffe类,并编程描述上述关系。
#include<iostream>
using namespace std;
class Animal
{
public:
void take()
{
cout<<"eat."<<endl;}
};
class Giraffe:public Animal
{
public:
void StretchNeck()
{
cout<<"stretch neck."<<endl;
}
void Func(Giraffe & an);
private:
Animal an;
};
void Func(Giraffe & an){
an.take();//够树叶吃
}
int main(){
Giraffe gir;
gir.StretchNeck();
Func(gir);
}
继承和派生汽车类
#include <iostream>
using namespace std;
class vehicle //汽车类
{
protected:
int wheels; // 车轮数
double weight; // 重量
public:
vehicle(int a,double b);
int GetWheels() {
return wheels; }
double GetWeight() {
return weight; }
void show(