日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/8/1 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 面向对象程序设计———大花园 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

面向對象程序設計———大花園

  • 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(); }

總結

以上是生活随笔為你收集整理的面向对象程序设计———大花园的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。