面向对象程序设计———大花园

  • 1、实验目的
  • 2、多个类的继承关系画出结构图。
  • 3、每个类的定义,包括.h文件和.cpp文件。
    • 1)Animal类
    • 2)Hydrobiont类
    • 3)Atmobios类
    • 4)Fish类
    • 5)Dobson类
    • 6)Goose类
    • 7)Dragonfiy类
    • 8)Menu类
    • 9)主函数
  • 4、实验步骤及方法
  • 5、调试分析与测试结果
    • 1)调试分析
    • 2)测试结果
  • 6、主函数

1、实验目的

1)首先建立一个动物类,在类下派生出两个类分别是陆生生物类和空中生物类。在陆生生物类中,又派生出两个类,分别是狗和水虿。空中生物类也又派生出两个类,分别是蜻蜓和大雁。其中蜻蜓继承了水虿和空中生物的特征。
2)程序共有七个类,分别是动物类(Animal.h,Animal.app),陆生生物类(Terrestrial.h, Terrestrial.cpp),空中生物类(Atmobios.h,Atmobios.cpp),狗(Dog.h,Dog.cpp),水虿(Dobson.h,Dobson.cpp),蜻蜓(Dragonfly.h, Dragonfly.cpp),大雁(Goose.h,Goose.cpp)。
3)Animal类里运用了基类构造函数,Terrestrial和Atmobios运用了派生类的构造函数。
4)Dobson里面运用了运算符重载为友员函数,Fish里面运用了静态成员及静态成员成员函数和析构函数 。
5)Atmobios里面运用了虚基类。Dragonfly类运用了多重继承,分别在Dobson和Atmobios中继承。
6)Animal里面运用了抽象类,Animal.h中定义了一个纯虚函数“virtual void play()=0;”,里面实现了多态性。
7)主函数。

2、多个类的继承关系画出结构图。

3、每个类的定义,包括.h文件和.cpp文件。

1)Animal类

在Animal类中定义了年龄和颜色,并定义了纯虚函数
①Animal.h

#ifndef SADF
#define SADF
#include<iostream>
using namespace std;
#include<string>
class Animal
{public:int age;string color;Animal(int ag,string col);virtual void play()=0;void show();};#endif

②Animal.cpp

#include"Animal.h"
Animal::Animal(int ag,string col)
{age=ag;color=col;}
void Animal::show()
{cout<<age<<","<<color<<endl;
}

2)Hydrobiont类

在Hydrobiont类中定义了食物和形状,继承了Animal类的属性
①Hydrobiont.h

#include<iostream>
using namespace std;
#include"Animal.h"
#ifndef HYDRO
#define HYDROclass Hydrobiont:virtual public Animal{public:string food;string shape;Hydrobiont(int ag,string col,string fo,string sha);void play(){cout<<"水生生物在水里游来游去"<<endl; };void show2();};
#endif

②Hydrobiont.cpp

#include "Hydrobiont.h"
Hydrobiont::Hydrobiont(int ag,string col,string fo,string sha):Animal(ag,col)
{age=ag;color=col;food=fo;shape=sha;
}
void Hydrobiont::show2()
{cout<<"水生动物的年龄是:"<<age<<"水生动物的颜色是:"<<color<<endl;cout<<"水生动物的食物是:"<<food<<endl;cout<<"水生动物的形状是:"<<shape<<endl;
}

3)Atmobios类

在Atmobios类中定义了高度和羽毛,继承了Animal类的属性
①Atmobios.h

#include<iostream>
using namespace std;
#include"Animal.h"
#ifndef ATMO
#define ATMO
class Atmobios:virtual public Animal
{public:string high;string feather;Atmobios(int ag,string col,string hig,string fea);void play(){cout<<"空中生物在天上飞来飞去"<<endl;}void show3(); };#endif

②Atmobios.cpp

#include "Atmobios.h"
Atmobios::Atmobios(int ag,string col,string hig,string fea):Animal(ag,col){age=ag;color=col;high=hig;feather=fea;}
void Atmobios::show3()
{cout<<"空中动物的年龄是:"<<age<<"空中动物的颜色是:"<<color<<endl;cout<<"空中动物飞行的高度是:"<<high<<endl;cout<<"空中动物有没有羽毛是:"<<feather<<endl;
}

4)Fish类

Fish类中定义了用途,继承了Hydrobiont类的属性,并且在Fish类中定义了静态成员以及静态成员函数,还运用了析构函数
①Fish.h

#include<iostream>
using namespace std;
#include "Hydrobiont.h"
class Fish:virtual public Hydrobiont{public:string use;static int sum;static int  Total();~Fish();Fish(int ag,string col,string fo,string sha,string use);void play(){cout<<"鱼的嘴一张一合"<<endl; };void show4();};

②Fish.cpp

#include "Fish.h"
Fish::Fish(int ag,string col,string fo,string sha,string use):Hydrobiont(ag,col,fo,sha),Animal(ag,col){age=ag;color=col;food=fo;shape=sha;use=use;sum+=age;}void Fish::show4()
{cout<<"鱼的年龄是:"<<age<<"鱼的颜色是:"<<color<<endl;cout<<"鱼的食物是:"<<food<<"鱼的形状是:"<<shape<<endl; cout<<"鱼的用途是:"<<use<<endl;
}
int Fish::sum=0;
int Fish::Total(){return sum;   }Fish::~Fish()
{sum-=age;
}

5)Dobson类

Dobson类中定义了类别,继承了Hydrobiont类的属性,其中运用了运算符“+”重载为友元函数
①Dobson.h

#include<iostream>
using namespace std;
#include "Hydrobiont.h"
#ifndef DOBSON
#define DOBSON
class Dobson:virtual public Hydrobiont
{public:string group;Dobson(int ag=0,string col=0,string fo=0,string sha=0,string gro=0);friend Dobson operator + (const Dobson& c1, const Dobson& c2);void play(){cout<<"水虿在努力长大"<<endl; };void show5();
};
#endif

②Dobson.cpp

#include "Dobson.h"
Dobson::Dobson(int ag,string col,string fo,string sha,string gro):Hydrobiont(ag,col,fo,sha),Animal(ag,col){age=ag;color=col;food=fo;shape=sha;group=gro;   }
void Dobson::show5()
{cout<<"水虿的年龄是:"<<age<<"水虿的颜色是:"<<color<<"水虿的食物是:"<<food<<"水虿的形状是:"<<shape<<endl;cout<<"水虿的类群是:"<<group<<endl;
}Dobson operator + (const Dobson& c1, const Dobson& c2)
{Dobson temp;temp.age = c1.age + c2.age;temp.color = c1.color + c2.color;temp.food = c1.food + c2.food;temp.shape = c1.shape + c2.shape;temp.group= c1.group + c2.group;return temp;
}

6)Goose类

Goose类中定义了家庭成员数量属性,继承了Atmobios类的属性
①Goose.h

#include<iostream>
using namespace std;
#include "Atmobios.h"
class Goose:virtual public Atmobios
{public:int family;Goose(int ag,string col,string hig,string fea,int fam); void play(){cout<<"大雁在空中呈队形飞翔"<<endl;}; void show6(); };

②Goose.cpp

#include "Goose.h"Goose::Goose(int ag,string col,string hig,string fea,int fam):Atmobios( ag, col, hig,fea),Animal(ag,col){age=ag;color=col;high=hig;feather=fea;family=fam;}
void Goose::show6()
{cout<<"大雁的年龄是:"<<age<<"大雁的颜色是:"<<color<<endl;cout<<"大雁飞行的高度是:"<<high<<"大雁有没有羽毛是:"<<feather<<endl;cout<<"大雁的家庭成员数量是:"<<family<<endl;
}

7)Dragonfiy类

Dragonfly类定义了自己的属性家,继承了Atmobios类和Dobson类的属性,
①Dragonfly.h

#include<iostream>
using namespace std;
#include "Atmobios.h"
#include "Dobson.h"
#include<string>
class Dragonfly: public Atmobios,public Dobson
{public:string home;Dragonfly(int ag,string col,string hig,string fea,string fo,string sha,string gro,string hom);void play(){cout<<"蜻蜓点水"<<endl;}; void show7(); };

②Dragonfly.cpp

#include"Dragonfly.h"
Dragonfly::Dragonfly(int ag,string col,string hig,string fea,string fo,string sha,string gro,string hom):Dobson(ag,col,fo,sha,gro),Atmobios(ag,col,hig,fea),Animal(ag,col),Hydrobiont(ag,col,fo,sha){age=ag;color=col;high=hig;feather=fea;food=fo;shape=sha;group=gro;home=hom;}
void Dragonfly::show7()
{cout<<"蜻蜓的年龄是:"<<age<<"蜻蜓的颜色是:"<<color<<"蜻蜓飞行的高度是:"<<high<<"蜻蜓有没有羽毛是:"<<feather<<"蜻蜓飞行的食物是:"<<food<<"蜻蜓的大小是:"<<shape<<"蜻蜓的类群是:"<<group<<endl;cout<<"蜻蜓的家是:"<<home<<endl;
};

8)Menu类

①Menu.h

#include<string>
using namespace std;
#ifndef MENU_H
#define MENU_H
class Menu
{public:int menu_select();void handle_menu();void h1();void h2();void h3();void h4();void h5();void h6();};
#endif

②Menu.cpp

#include<iostream>
using namespace std;
#include<string>
#include"Menu.h"
#include"Animal.h"
#include "Atmobios.h"
#include "Hydrobiont.h"
#include "Dobson.h"
#include"Dragonfly.h"
#include "Fish.h"
#include"Goose.h"
void Menu::handle_menu()
{  for(int i=0;i<=6;i++){switch (menu_select()){   case 1:h1();break;case 2:h2();break;case 3:h3();break;case 4:h4();break;case 5:h5();break;case 6:h6();break;case 7: cout<<"\t再见!\n";return;}}
}
int Menu::menu_select()
{int cn;cout<<"\t1. 建立水生动物对象并显示 \n";cout<<"\t2. 建立鱼并显示 \n";cout<<"\t3. 建立水虿并显示 \n";  cout<<"\t4. 建立空中生物对象并显示 \n";cout<<"\t5. 建立蜻蜓对象并显示 \n";cout<<"\t6. 建立大雁对象并显示 \n";cout<<"\t7.退出程序\n";cout<<"\t选择1-7  \n";cin>>cn;for ( ; ;){ if (cn<1||cn>7) cout<<"\t输入错误,重选1-8\n";else break;}return cn;
}
void Menu::h1()
{   Animal *ani;Hydrobiont hyd(2,"黑色","浮游生物","小");ani=&hyd;ani->play();hyd.show2();
}
void Menu::h2()
{ Hydrobiont *hy;Fish fish(2,"黑色","小鱼","小","做菜");hy=&fish;hy->play();fish.show4();
}
void Menu::h3()
{ Hydrobiont *hy;Dobson dob(3,"黑色","小浮游生物","小","昆虫类");hy=&dob;hy->play();dob.show5();
}
void Menu::h4()
{ Animal *ani;Atmobios atm(3,"白色","高","是");ani=&atm;ani->play();atm.show3();
}
void Menu::h5()
{   Atmobios *at;Dragonfly drag(2,"白色","低","有","害虫","小","昆虫","水上");at=&drag;at->play();drag.show7();
}
void Menu::h6()
{ Atmobios *at;Goose goo(5,"黑色","高","有",5);at=&goo;at->play();goo.show6();}

9)主函数

通过输入密码操作验证,密码正确则才可进入大花园

#include <string>
#include <iostream>
#include "Menu.h"int main()
{printf("请输入密码并按回车进入大花园!\n");int data;scanf("%d",&data);if(data==1234){printf("密码输入正确!\n");}if(data!=1234){printf("密码错误,请重新输入密码!\n");return main(); }Menu m;m.handle_menu();
}

4、实验步骤及方法

首先打开devc++软件,在菜单栏中点开新建项目,选择Console Application的c++项目名为“大花园—动物篇”点击确定,完成创建项目,之后在项目名右键选择添加,在弹出的框中命名为Animal.h和Animal.cpp文件,点击保存,接下来的类别创建文件方法与此一致,最后创建主函数和菜单函数,在文件里面书写代码段,最后完成调试,运行。

5、调试分析与测试结果

1)调试分析

为了追求程序的正确性以及应用的实现性,通过对比多个类别的代码来找出错误的原因以及修改方法,进行程序的调试。

2)测试结果

输入密码


按1后

按2后

按3后

按4后

按5后

按6后

按7后

6、主函数

int main()
{Menu m;m.handle_menu();
}

面向对象程序设计———大花园相关推荐

  1. C++面向对象程序设计大作业:魔兽世界(三):开战

    C++面向对象程序设计大作业:魔兽世界(三):开战 问题描述 问题分析 代码 问题描述 问题来自于北京大学郭炜老师的C++慕课的大作业 魔兽世界的西面是红魔军的司令部,东面是蓝魔军的司令部.两个司令部 ...

  2. C++大作业(面向对象程序设计大作业)——销售公司员工管理

    面向对象程序设计大作业 目录 面向对象程序设计大作业 1.问题重述 2.编程思想 2.1数据结构 2.2功能确定 3.类的设计 3.1UML图标准 3.2本题类图 4.运行结果 1.输出所有信息 2. ...

  3. c++面向对象程序设计大作业(人事管理系统)

    1.登录 Administrator_login.h #pragma once #include"controller.h" #include<string> #inc ...

  4. 如下哪个是Java中的合法自定义标识符_吉大13春《面向对象程序设计》在线作业答案...

    吉大13春<面向对象程序设计>在线作业答案 2013-03-21 01:03:14 259 有学员问关于吉大13春<面向对象程序设计>在线作业答案的题目的参考答案和解析,具体如 ...

  5. 2017《面向对象程序设计》寒假作业一

    1.你有什么技能比大多人(超过70%以上)更好? 我看电影比一般人多一点点:我听英文歌比一般人多一点点:我有一把尤克里里和一个滑板.我有很多爱好,但都没能发展成我的特长,它们给我的生活增添了情趣,又不 ...

  6. JavaScript中的面向对象程序设计

    本文内容目录顺序: 1.Object概念讲述: 2.面向对象程序设计特点: 3.JavaScript中类和实例对象的创建: 4.原型概念: 5.原型API: 6.原型对象的具体使用:7.深入理解使用原 ...

  7. 201771010106东文财《面向对象程序设计(java)》实验12

    实验十二  图形程序设计 实验时间 2018-11-14 1.实验目的与要求 (1) 掌握Java GUI中框架创建及属性设置中常用类的API: (2) 掌握Java GUI中2D图形绘制常用类的AP ...

  8. 《面向对象程序设计》第一次作业

    大一上学期总的来说是更多地是在认识大学,适应新的环境,更多地尝试各种对我来说很新鲜的东西. 学习是主要的,大学学习更多地依靠自主学习的能力,身边没有长辈的督促,自己要越来越懂得对自己的人生负责.大学的 ...

  9. java红牛农场答案_Java面向对象程序设计实验指导与习题解答(21世纪高等学校计算机专业实用规划教材)...

    导语 <Java面向对象程序设计实验指导与习题解答>是<Java面向对象程序设计>(作者耿祥义,清华大学出版社出版,2010)的配套实验指导和习题解答,目的是通过一系列实验练习 ...

  10. C 语言的标准输入对象是,《面向对象程序设计C+》期末试卷及标准答案

    <面向对象程序设计C++>期末考试试卷(A)班级:姓名:学号:分数: 试卷说明:本套试题共四个大题,全部题目都答在答题纸上,写在 其他地方均无效. (答题纸在本套试卷的第10页上) 一.选 ...

最新文章

  1. java中super用来定义父类,Java中super的几种用法及与this的区别
  2. [并发编程] - Executor框架#ThreadPoolExecutor源码解读03
  3. 常见分布式理论(CAP、BASE)和一致性协议(Gosssip协议、Raft一致性算法)
  4. Java面向对象(一)面向对象简介和初步 了解
  5. 我理解Docker的过程2
  6. 在Ubuntu上编译libusb
  7. 你最擅长哪种数学思维?
  8. hdu5424 Rikka with Graph II
  9. TensorFlow十三 LSTM练习
  10. xshell连接虚拟机linux系统失败问题
  11. “哎哟,真的很快哦” 闪送宣布签约周杰伦为其品牌代言人
  12. apache http php,性能-安装HTTP使用PHP和Apache使标头失效
  13. 【MySQL】源码安装MySQL
  14. 安装SQL Server2008,要重启机器,解决办法
  15. 全国计算机等级考试在线报名,全国计算机等级考试网上报考具体流程
  16. linux中bzero函数,库函数
  17. android日期与时间滑动选择器
  18. 【.net core】电商平台升级之微服务架构应用实战
  19. 2022年淘宝618活动时间和天猫618有什么优惠活动
  20. 个性化智能推荐技术研究总结

热门文章

  1. Spring Cloud 尚硅谷阳哥学习笔记,每一行代码均有解释,适合快速上手,并配合尚硅谷视频食用
  2. 十分钟智商运动 李永乐 第2章 奇妙的物理
  3. hash函数的构造方法
  4. Kaggle入门 - TMDB 5000 电影推荐数据分析
  5. 四阶龙格库塔法的计算例子
  6. bch matlab,求助!关于matlab中BCH码的弱问题
  7. 基于51单片机的蜂鸣器及简谱的学习(编曲)
  8. k2p拆机ttl刷breed_【1.10】k2p A版 22.10.3.42;22.10.3.38;拆机TTL刷BREED;B版 21.6.25.20刷机 图文教程...
  9. 自动驾驶汽车电子电气架构技术开发
  10. lycos搜索引擎_Lycos中国推出全新搜索引擎