解释下这段程序的含义,谢谢

来源:百度知道 编辑:UC知道 时间:2024/05/07 18:57:51
#include <iostream.h>
#include <stdio.h>
#include <string.h>

class pet
{
char color[30];
double weight;
int age;
char speak[30];
public:
void color();
{cout<<”pet color”<<endl;}
void weight()
{cout<<”pet weight”<<endl;}
void age()
{cout<<”pet age”<<endl;}
vitual void speak()
{cout<<”pet speak”<<endl;}
};

class cat:public pet
{public:

void speak()
{
cout<<”miao”<<endl;
}
};

#include <iostream.h>
#include <stdio.h>
#include <string.h>

class pet //定义宠物类
{
char color[30];
double weight;
int age;
char speak[30];
//上面是宠物类的私有数据成员:颜色,重量,年龄,叫声

public:
void color();{cout<<”pet color”<<endl;} //打印颜色方法
void weight(){cout<<”pet weight”<<endl;} //打印重量方法
void age(){cout<<”pet age”<<endl;} //打印年龄方法
vitual void speak(){cout<<”pet speak”<<endl;} //虚方法,打印叫声
//上面是宠物类的成员函数,也叫方法
};

class cat:public pet //定义猫类,(public)继承自宠物类pet
{
public:
void speak(){cout<<”miao”<<endl;} //实现叫声方法,喵喵的叫
};

这个例子展示了类的继承:
猫类是宠物类的派生类,也就是说猫类继承了宠物类的颜色,重量,年龄,叫声等属性,也继承了打印这些属性的各种方法

这个例子又展示了类的继承关系中的多态:
宠物会叫,但这个比较抽象,而它的派生类 猫会喵喵的叫

#include <iostream.h>
#include <stdio.h>
#include <string.h>

class pet //定义宠物类