下载此文档

c 实验8 继承与派生上机练习题.doc


文档分类:IT计算机 | 页数:约6页 举报非法文档有奖
1/6
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/6 下载此文档
文档列表 文档介绍
定义一个哺乳动物类Mammal,并从中派生出一个狗类Dog,下面给出Mammal类的定义,要求:
添加Dog类的颜色数据成员,访问属性为私有,通过SetColor和GetColor成员函数来对颜色进行设置和获取。
分别为基类和派生类添加相应的构造函数(有参、无参)和析构函数,并进行测试。
class Mammal
{
protected:
int itsAge;
int itsWeight;
public:
int GetAge(){return itsAge;}
void SetAge(int age) {itsAge=age;}
int GetWeight() { return itsWeight;}
void SetWeight(int weight) {itsWeight= weight;}
};
class Dog : public Mammal
{
//定义Dog类的数据成员和成员函数
};
改:
#include <>
#include <string>
using namespace std;
class Mammal
{

protected:
int itsAge;
int itsWeight;
public:
Mammal();
~Mammal();
int GetAge(){return itsAge;}
void SetAge(int age) {itsAge=age;}
int GetWeight() { return itsWeight;}
void SetWeight(int weight) {itsWeight= weight;}
};
class Dog : public Mammal
{
protected:
char itscolor[20];
public:
Dog();
void Setcolor(char *color) {strcpy(itscolor,color);}

void getcolor(){cout<<"狗的颜色"<<itscolor<<endl;}
//定义Dog类的数据成员和成员函数
};
////////////////////////
Mammal::Mammal()
{
int age1,weight1;
cout<<"请输入动物的年龄:"<<endl;
cin>>age1;
SetAge(age1);
cout<<"请输入动物的体重:"<<endl;
cin>>weight1;
SetWeight(weight1);
}
Mammal::~Mammal()
{
cout<<"Destructor called."<<endl;
}
Dog::Dog()
{char color[20];
cout<<"请输入狗的颜色:"<<endl;
cin>>color;Setcolor(color);
cout<<"狗的颜色"<<itscolor<<"体重"<<GetWeight()<<"年龄"<<GetAge()<<endl;
}
void main()
{
Dog dog1;

c 实验8 继承与派生上机练习题 来自淘豆网www.taodocs.com转载请标明出处.

相关文档 更多>>
非法内容举报中心
文档信息
  • 页数6
  • 收藏数0 收藏
  • 顶次数0
  • 上传人xunlai783
  • 文件大小32 KB
  • 时间2018-01-04