实验四:派生类和继承(一)

2020-04-17 03:22

福建农林大学实验报告

实验4 派生类和继承(一) 一、实验目的和要求

(1)掌握派生类的声明与定义方法,进一步理解类的继承的概念,能够定义和使用类的继承关系。 (2)熟悉公有派生和私有派生的访问特性。

二、实验内容和原理

1、(1)定义一个基类Animal,该类具有私有整型成员变量age,weight,构造派生类Dog公有继承 Animal。Dog类新增私有成员变量color,新增成员函数SetAge(int n)中直接给age赋值,新增成员函数SetWeight(int m)中直接给weight赋值,查看编译结果,并分析结果。(2)将类Animal中的age和weight为公有成员,重做第一步,并分析结果。(3)将类Animal中的age和weight为保护成员,重做第一步,并分析结果。(4)将派生类Dog的继承方式改为私有继承方式和保护继承方式重做以上各小题,并分析结果。

2、程序分析题(写出程序的输出结果,并分析结果)。

三、实验环境

1. 硬件:PC机;

2. 软件:Windows操作系统、Visual C++ 6.0

四、算法描述及实验步骤

1、(1):

#include class animal

{private:int age,weight;}; class dog:public animal {private:char color[10]; public: int SetAge(int n) {age=n;return n;}

int SetWeight (int m) {weight=m;return m; }

};

int main()

{ int x,y; dog a;

cout<<\: \cin>>x;cout<

cout<<\ weight:\cin>>y;cout<

cout<<\

cout<<\ weight is\return 0; } (2): #include class animal

{public:int age,weight;}; class dog:public animal {private:char color[10]; public: int SetAge(int n)

{age=n;return n;} int SetWeight (int m) {weight=m;return m; }

};

int main()

{ int x,y; dog a;

cout<<\:\cin>>x;cout<

cout<<\ weight:\cin>>y;cout<

cout<<\

cout<<\ weight is \return 0; } (3):

#include class animal

{private:int age,weight;}; class dog:public animal {private:char color[10]; public: int SetAge(int n) {age=n;return n;}

int SetWeight (int m) {weight=m;return m; }

};

int main()

{ int x,y; dog a;

cout<<\:\cin>>x;cout<

cout<<\ weight:\

cin>>y;cout<

cout<<\

cout<<\ weight is \return 0; } (4): #include class animal

{private:int age,weight;}; class dog:public animal {private:char color[10]; public: int SetAge(int n)

{age=n;return n;} int SetWeight (int m) {weight=m;return m; }

};

int main()

{ int x,y; dog a;

cout<<\:\cin>>x;cout<

cout<<\ weight:\cin>>y;cout<

cout<<\

cout<<\ weight is \return 0; } (5):

#include class animal

{private:int age,weight;}; class dog:public animal {private:char color[10]; public: int SetAge(int n) {age=n;return n;}

int SetWeight (int m) {weight=m;return m; }

};

int main()

{ int x,y; dog a;

cout<<\:\cin>>x;cout<

cout<<\ weight:\

cin>>y;cout<

cout<<\cout<<\ weight is \return 0; } (6): #include class animal

{private:int age,weight;}; class dog:public animal {private:char color[10]; public: int SetAge(int n)

{age=n;return n;} int SetWeight (int m) {weight=m;return m; }

};

int main()

{ int x,y; dog a;

cout<<\:\cin>>x;cout<

cout<<\ weight:\cin>>y;cout<

cout<<\

cout<<\ weight is \return 0; } (7):

#include class animal

{private:int age,weight;}; class dog:public animal {private:char color[10]; public: int SetAge(int n) {age=n;return n;}

int SetWeight (int m) {weight=m;return m; }

};

int main()

{ int x,y; dog a;

cout<<\:\cin>>x;cout<

cout<<\ weight:\

cin>>y;cout<

cout<<\

cout<<\ weight is \return 0; } (8): #include class animal

{private:int age,weight;}; class dog:public animal {private:char color[10]; public: int SetAge(int n)

{age=n;return n;} int SetWeight (int m) {weight=m;return m; }

};

int main()

{ int x,y; dog a;

cout<<\:\cin>>x;cout<

cout<<\ weight:\cin>>y;cout<


实验四:派生类和继承(一).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2018年反假货币培训考试模拟试题及答案 - 图文

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: