面向对象程序设计———大花园
面向?qū)ο蟪绦蛟O(shè)計(jì)———大花園
- 1、實(shí)驗(yàn)?zāi)康?/li>
- 2、多個類的繼承關(guān)系畫出結(jié)構(gòu)圖。
- 3、每個類的定義,包括.h文件和.cpp文件。
- 1)Animal類
- 2)Hydrobiont類
- 3)Atmobios類
- 4)Fish類
- 5)Dobson類
- 6)Goose類
- 7)Dragonfiy類
- 8)Menu類
- 9)主函數(shù)
- 4、實(shí)驗(yàn)步驟及方法
- 5、調(diào)試分析與測試結(jié)果
- 1)調(diào)試分析
- 2)測試結(jié)果
- 6、主函數(shù)
1、實(shí)驗(yàn)?zāi)康?/h1>
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類里運(yùn)用了基類構(gòu)造函數(shù),Terrestrial和Atmobios運(yùn)用了派生類的構(gòu)造函數(shù)。
4)Dobson里面運(yùn)用了運(yùn)算符重載為友員函數(shù),Fish里面運(yùn)用了靜態(tài)成員及靜態(tài)成員成員函數(shù)和析構(gòu)函數(shù) 。
5)Atmobios里面運(yùn)用了虛基類。Dragonfly類運(yùn)用了多重繼承,分別在Dobson和Atmobios中繼承。
6)Animal里面運(yùn)用了抽象類,Animal.h中定義了一個純虛函數(shù)“virtual void play()=0;”,里面實(shí)現(xiàn)了多態(tài)性。
7)主函數(shù)。
2、多個類的繼承關(guān)系畫出結(jié)構(gòu)圖。
3、每個類的定義,包括.h文件和.cpp文件。
1)Animal類
在Animal類中定義了年齡和顏色,并定義了純虛函數(shù)
①Animal.h
②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
②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
②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類中定義了靜態(tài)成員以及靜態(tài)成員函數(shù),還運(yùn)用了析構(gòu)函數(shù)
①Fish.h
②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類的屬性,其中運(yùn)用了運(yùn)算符“+”重載為友元函數(shù)
①Dobson.h
②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類中定義了家庭成員數(shù)量屬性,繼承了Atmobios類的屬性
①Goose.h
②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<<"大雁的家庭成員數(shù)量是:"<<family<<endl; }7)Dragonfiy類
Dragonfly類定義了自己的屬性家,繼承了Atmobios類和Dobson類的屬性,
①Dragonfly.h
②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類
①M(fèi)enu.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)主函數(shù)
通過輸入密碼操作驗(yàn)證,密碼正確則才可進(jìn)入大花園
#include <string> #include <iostream> #include "Menu.h"int main() {printf("請輸入密碼并按回車進(jìn)入大花園!\n");int data;scanf("%d",&data);if(data==1234){printf("密碼輸入正確!\n");}if(data!=1234){printf("密碼錯誤,請重新輸入密碼!\n");return main(); }Menu m;m.handle_menu(); }4、實(shí)驗(yàn)步驟及方法
首先打開devc++軟件,在菜單欄中點(diǎn)開新建項(xiàng)目,選擇Console Application的c++項(xiàng)目名為“大花園—動物篇”點(diǎn)擊確定,完成創(chuàng)建項(xiàng)目,之后在項(xiàng)目名右鍵選擇添加,在彈出的框中命名為Animal.h和Animal.cpp文件,點(diǎn)擊保存,接下來的類別創(chuàng)建文件方法與此一致,最后創(chuàng)建主函數(shù)和菜單函數(shù),在文件里面書寫代碼段,最后完成調(diào)試,運(yùn)行。
5、調(diào)試分析與測試結(jié)果
1)調(diào)試分析
為了追求程序的正確性以及應(yīng)用的實(shí)現(xiàn)性,通過對比多個類別的代碼來找出錯誤的原因以及修改方法,進(jìn)行程序的調(diào)試。
2)測試結(jié)果
輸入密碼
按1后
按2后
按3后
按4后
按5后
按6后
按7后
6、主函數(shù)
int main() {Menu m;m.handle_menu(); }總結(jié)
以上是生活随笔為你收集整理的面向对象程序设计———大花园的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【python项目开发】爬虫基础知识
- 下一篇: 数据库索引:位图索引