面向对象上机考试题_第1页
面向对象上机考试题_第2页
面向对象上机考试题_第3页
面向对象上机考试题_第4页
面向对象上机考试题_第5页
已阅读5页,还剩39页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、面向对象上机考试题(1)关于宠物进笼Anne的宠物小屋有12个笼子,每个笼子可以放不同的动物,但只能放1只或0只,包括 猫Cat,狗Dog,蛇Snake. 1,实现一个简单的管理系统,可增加、删除笼子中的宠物,查询每个笼子中存放的 宠物类型,(包括笼子为空的情况),统计笼中宠物的种类和数量. 2,定义描述宠物小屋的类shelves,其中有12笼子用于存放各种宠物. 3,定义虚拟基类Animal,包括纯虚函数ShowMe,显示每个宠物的情况,包括类型、颜色、 体重和喜爱的食物。 4,定义派生类Cat,Dog,Snake,具体实现上述纯虚函数。 5,重载输入*作符,使得可能通过cin直接读入宠物颜

2、色、体重和喜爱的 食物。 6,编写main函数,测试上述要求和各种功能。 */ #include iostream.h #include string.h enum NONE=0, CAT=1, DOG=2, SNAKE=3; class Animal protected: char color20; char food20; double weight; public: virtual void ShowMe()=0; ; class Cat:public Animal public: friend istream& operator(istream &is, Cat &cat); void

3、 ShowMe() cout猫的颜色、喜欢的食物、体重分别为:; coutcolor”,”food”,”weight(istream &is, Cat &cat) coutcat.color; coutcat.food; coutcat.weight; return is; class Dog:public Animal public: friend istream& operator(istream &is, Dog &Dog); void ShowMe() cout狗的颜色、喜欢的食物、体重分别为:; coutcolor”,”food”,”weight(istream &is, Dog &

4、dog) coutdog.color; coutdog.food; coutdog.weight; return is; class Snake:public Animal public: friend istream& operator(istream &is, Snake &snake); void ShowMe() cout蛇的颜色、喜爱的食物、体重分别为:; coutcolor”,”food”,”weight(istream &is, Snake &snake) coutsnake.color; coutsnake.food; coutsnake.weight; return is;

5、class Shelves int cage12; /每个笼子里动物的数量int sum3; 每种动物的数量Cat cats12; Dog dogs12; Snake snakes12; public: Shelves () for (int i=0; i12; i+) cagei=NONE; for (i=0; i3; i+) sumi=0; int push_cat(Cat &cat, int n) catsn = cat; if (cagen!=NONE) return 0; else sum0+; return cagen=CAT; int push_dog(Dog &dog, int

6、 n) dogsn=dog; if (cagen!=NONE) return 0; else sum1+; return cagen=DOG; int push_snake(Snake &snake, int n) snakesn=snake; if (cagen!=NONE) return 0; else sum2+; return cagen=SNAKE; void del(int n) if (cagen=CAT) sum0-; if (cagen=DOG) sum1-; if (cagen=SNAKE) sum2-; if (cagen=NONE) cout笼子里没有动物;else c

7、agen=NONE; cout已删除笼子里的动物; cout void seek(int n) if (cagen=NONE) cout第N个笼子里没有动物;if (cagen=CAT) cout第N个笼子里的动物为猫,; catsn.ShowMe(); if (cagen=DOG) cout第N个笼子里的动物为狗,; dogsn.ShowMe(); if (cagen=SNAKE) cout第N个笼子里的动物为蛇,; snakesn.ShowMe(); cout void count() cout笼子里的猫总共有:SUM0只endl;cout笼子里的狗总共有:SUM1只endl;cout笼子

8、里的蛇总共有:SUM2只endl; ; void main() Cat cat; Dog dog; Snake snake; Shelves shelves; int i=1,j; cout这是关于往笼子里放动物的问题: endl;cout 1: 往笼子里放猫;endl;cout 2: 往笼子里放狗;endl;cout 3: 往笼子里放蛇;endl;cout 4: 查询笼子里的动物;endl;cout 5: 删除笼子里的动物;endl;cout 6:统计笼子里的动物种类及数量;cout 7: 退出程序;endl;cout-endl;while(i) couti; switch(i) case

9、1: coutj; cincat; while (shelves.push_cat(cat, j)=0) coutj; break; case 2: coutj; cindog; while (shelves.push_dog(dog, j)=0) coutj; break; case 3: coutj; cinsnake; while (shelves.push_snake(snake, j)=0) coutj; break; case 4: coutj; shelves.seek(j); break; case 5: coutj; shelves.del(j); break; case 6

10、: shelves.count(); break; default: i=0; break; cout”*作符,使得可以通过cin直接读入上述整数类和字符串类的对象值。 (5) 编写main函数,测试上述所要求的各种功能,即可以根据菜单命令增加栈元素、删除栈元素、显示栈顶元素,其中的元素可以 是整数和/或字符串。 提示:虚拟基类Element的定义至少包括以下纯虚函数ShowMe, class Element / public: virtual void ShowMe () = 0; / ; */ #include stdafx.h #include iostream.h #include s

11、tring.h const max=1000; #define NULL 0 class Element public: virtual void ShowMe () = 0; ; class MyInteger:public Element int a; public: MyInteger()a=0; friend istream &operator(istream &is, MyInteger &MyI); int Get() return a; void ShowMe() cout 整数:A(istream &is, MyInteger &MyI) isMyI.a; return is;

12、 class MyString:public Element char s100; public: friend istream &operator(istream &is, MyString &MyS); void ShowMe() cout 字符串:S(istream &is, MyString &MyS) isMyS.s; return is; class stack MyString MySmax; MyInteger MyImax; int top; public: stack() top=-1; void push(MyInteger &My) MyI+top=My; void p

13、ush(MyString &My) MyS+top=My; void Top() if (empty() cout栈为空else MyItop.ShowMe(); void Tops() if (empty() cout栈为空else MyStop.ShowMe(); void pop() MyItop-.ShowMe(); void pops() MyStop-.ShowMe(); bool empty() return (bool)(top=-1); bool full() return (bool)(top=max-1); ; int main(int argc, char* argv)

14、 MyInteger My1; MyString My2; stack st1, st2; int i, j; coutj; cout请输入J个整数My1; st1.push(My1); cout栈顶元素为:; st1.Top(); coutj; cout依次出栈元素为:for (i=0; ist1.pop(); cout出栈后栈顶元素为:; st1.Top(); coutj; cout请输入J个字符串My2; st2.push(My2); cout栈顶元素为:; st2.Tops(); coutj; cout依次出栈元素为:for (i=0; ist2.pops(); cout”*作符,使得

15、可以通过cin直接读入上述整数类和字符串类的对象值。 (5) 编写main函数,测试上述所要求的各种功能,即可以根据菜单命令增加队列元素、删除 队列元素、显示队列头部元素和队列尾部元素,其中的元素可以是整数和/或字符串。 提示: 虚拟基类Element的定义至少包括以下纯虚函数ShowMe, 这是我24号的考题! class Element / public: virtual void ShowMe () = 0; / ; */ #include stdafx.h #include iostream.h #include string.h const max=1000; #define NUL

16、L 0 class Element public: virtual void ShowMe () = 0; ; class MyInteger:public Element int a; public: MyInteger()a=0; friend istream &operator(istream &is, MyInteger &MyI); int Get() return a; void ShowMe() cout整数:A(istream &is, MyInteger &MyI) coutMyI.a; return is; class MyString:public Element cha

17、r s100; public: friend istream &operator(istream &is, MyString &MyS); void ShowMe() cout字符串:S(istream &is, MyString &MyS) coutMyS.s; return is; class Queue int size; Element *Elmmax; MyInteger MyImax; MyString MySmax; public: Queue() for (int i=0; iElmi=NULL; size=-1; void add(MyInteger &My) if (ful

18、l() cout队列已满else MyI+size=My; Elmsize=new MyInteger; Elmsize=&MyIsize; void add(MyString &My) if (full() cout队列已满else MyS+size=My; Elmsize=new MyString; Elmsize=&MySsize; void tail() if(empty() cout队列为空else coutShowMe(); void head() if(empty() cout队列为空else coutShowMe(); void del() if(empty() cout队列为

19、空else coutShowMe(); bool empty() return (bool)(size=-1); bool full() return (bool)(size=max-1); ; void main() MyInteger my1; MyString my2; Queue queue; int s=1; while(s) coutPlease select 1-6 cout 1: 整数进队列;cout 2: 字符串进队列;cout 3: 显示队列头元素;cout 4: 显示队列尾元素cout 5: 出队列;cout 6: 退出程序cout-couts; switch(s) ca

20、se 1: cinmy1; queue.add(my1); break; case 2: cinmy2; queue.add(my2); break; case 3: queue.head(); break; case 4: queue.tail(); break; case 5: queue.del(); break; default: s=0; break; 面向对象上机考试题(3)关于堆栈(续)/建立一个栈类,能够存放图形元素(矩形或圆形),有入栈,出栈, /取栈顶的功能。建立元素类,里面至少一个纯虚函数,矩形和圆形类 /来实现里面的函数SHOWME,实现他的显示图形信息的功能。重载,

21、/能够输入图形信息。测试以上类的功能。 #include stdafx.h #include iostream.h #include string.h const n=50; class shape protected: double width, length; public: shape() virtual void ShowMe()=0; ; class rectangle:public shape public: rectangle():shape() friend istream &operator(istream &is, rectangle &rec) coutrec.lengt

22、h; coutrec.width; return is; void ShowMe() cout长方形的长宽分别为:(,WIDTH)(istream &is, circle &cir) coutcir.width; iscir.length; coutcir.r; return is; void ShowMe() cout圆的圆心坐标为:(WIDTH,LENGTH) 半径为:R ; class stack shape *spn; rectangle recn; circle cirn; int size; public: stack() for (int i=0; ispi=NULL; size

23、=-1; bool empty()return size=-1; bool full()return size=n-1; void push(rectangle &rect) rec+size=rect; spsize=new rectangle; spsize=&recsize; void push(circle &circ) cir+size=circ; spsize=new circle; spsize=&cirsize; void top() if (empty() cout栈为空else cout栈顶元素为:ShowMe(); void pop() if (empty() cout栈

24、为空else cout出栈元素为:ShowMe(); ; void main() stack Stack; rectangle rec; circle cir; int s=1; while (s) coutcout请选择1-5号键*作cout 1: 矩形进栈;cout 2: 圆进栈;cout 3: 显示栈顶元素;cout 4: 出栈;cout 5: 退出程序;s; switch(s) case 1: cinrec; Stack.push(rec); break; case 2: cincir; Stack.push(cir); break; case 3: Stack.top(); brea

25、k; case 4: Stack.pop(); break; case 5: s=0; break; /default: cout请重新输入”操作符。 3.编写main函数,测试以上所要求的各种功能,即可以根据菜单命令增加、删除和查询商品,以及商品销售和商品上架的操作。 1、Annie的宠物小屋里有12个笼子,每个笼子可以放不同的动物,包括猫,狗和蛇,但同一时刻一个笼子只能放0只或1只宠物,本题要求完成:(1)实现一个简单的管理系统,可以增加,删除指定笼子中的宠物,查询每个笼子中存放宠物的情况,统计宠物的种类和数量.(2)定义描述宠物小屋的类Shelves,其中包括12个笼子用于存放各种宠物;

26、(3)定义虚拟基类Animal,至少包含纯虚函数Showme;(4)定义派生类Cat,Dog,Snake,具体实现上述纯虚函数showme,显示该宠物的种类,名称,着色,体重和喜欢的食物.(5)重载输入操作符,使得可以通过cin直接读入宠物的着色,体重,和喜欢的食物;(6)编写main函数,测上述所要求的各种功能;Main.cpp#include#includeanimal.husing std:cout;using std:cin;using std:endl;int main()Shelves * shelves=new Shelves;Cat *cat;Dog *dog;Snake *s

27、nake;int n;int i=0;char ch;while(i!=5)cout please chiose(1-5):endl;cout 1:add an animalendl;cout2:delete an animalendl;cout 3:queryendl;cout4:statendl;cout5:exiti;switch(i)case 1:coutn;coutc:cat d:dog s:snakeendl;coutch;switch(ch)casec:cat=new Cat;cin*cat;if(!shelves -add(n,cat)cat-deleteme();break;

28、cased:dog=new Dog;cin*dog;if(!shelves-add(n,dog)dog-deleteme();break;cases:snake=new Snake;cin*snake;if(!shelves-add(n,snake)snake-deleteme();break;default:cout your choise is wrongendl;break;break;case 3:coutn;shelves-query(n);break;case 2:coutn;shelves-Delete(n);break;case 4:shelves-stat();break;c

29、ase 5:break;return 0;Animal.h#ifndef ANIMAL_H#define ANIMAL_H#includeclass Animalpublic:virtual void showme()=0;virtual void deleteme()=0;Animal()protected:char *name ;char *color;float weight;char *food;int type;class Cat:public Animalpublic:Cat();void showme();static int number();friend std:istrea

30、m & operator (std:istream & is,Cat &cat);void deleteme();Cat();private:static int Catnum;class Dog:public Animalpublic:Dog();void showme();static int number();friend std:istream & operator(std:istream & is,Dog &dog);void deleteme();Dog();private:static int Dognum;class Snake:public Animalpublic:Snak

31、e();void showme();static int number();friend std:istream & operator(std:istream & is ,Snake &snake);void deleteme();Snake();private:static int Snakenum;class Shelvespublic:Shelves();int add(int n,Animal *animal);void Delete(int n);void query(int n);void stat();private:Animal * shelves12;#endifAnimal

32、.cpp#include#includeanimal.husing std:cout;using std:cin;using std:endl;int Cat:Catnum=0;Cat:Cat()type=0;weight=0;name=new char20;color =new char 20;food=new char 20;Catnum+;void Cat:showme()coutI am a Cat.endl;cout My name is nameendl;cout My color is colorendl;coutMy weight is weightendl;cout My f

33、avorite foood is food (std:istream & is ,Cat &cat);coutendlcat.color;coutendlcat.weight;coutendlcat.food;return is;void Cat:deleteme()Catnum-;Cat:Cat()int Dog:Dognum=0;Dog:Dog()type=1;name=new char 20;color =new char 20;food=new char 20;weight=0;Dognum+;void Dog:showme()coutendlI am a Do

34、gendl;coutMy name is nameendl;cout My color is colorendl;coutMy weight is weightendl;coutMy favorite food is food(std:istream &is ,Dog &dog);coutendldog.color;coutendldog.weight;coutendldog.food;return is;void Dog:deleteme()Dognum-;Dog:Dog()int Snake:Snakenum=0;Snake:Snake()type=2;name=n

35、ew char 20;color=new char 20;food=new char 20;Snakenum+;void Snake:showme()coutendlI am a Snakeendl;coutMy name is nameendl;cout My color is colorendl;coutMy weight is weightendl;coutMy favorite food is food(std:istream &is ,Snake &snake);coutendlsnake.color;coutendlsnake.weight;couten

36、dlsnake.food;return is;void Snake:deleteme()Snakenum-;Snake:Snake()Shelves:Shelves()for(int i=0;i12;i+)shelvesi=NULL;int Shelves:add(int n,Animal * animal)int successful=1;if(shelvesn!=NULL)coutthe n the shelve is fullendl;successful=0;return successful;shelvesn=animal;cout successful!endl;return su

37、ccessful;void Shelves:query(int n)if(shelvesn=NULL)coutthe n the shelve is empty!showme();return ;void Shelves:Delete(int n)if(shelvesn=NULL)coutthe n shelves is emptydeleteme();shelvesn=NULL;coutsuccessful!endl;return ;void Shelves:stat()coutthere ar Cat:number()catsDog:number()dog,andSnake:number(

38、)snake.endl;return ;2、请实现一个简单的银行储蓄系统,承担活期用户的存款和取款业务,要求如下:(1) 实现描述银行的类Bank,记录系统中现有哪些储户,(可用数组实现但注意越界),定义了生成储户的函数append,按照账户删除储户的函数Delete,按账号查询储户的函数query,并显示结果.(2)定义储户类Account,具有属性账号,存款人姓名和余额,操作saving withdraw和showme.函数saving 存储业务,函数withdraw处理取款业务,(余额不足时不予以取并给提示信息),showme函数显示储户所有信息.(3)编写main函数,测试上述所要求的

39、各种功能,即可以根据菜单命令增加,删除,和查询储户,以及储户存款和取款操作.Main.cpp#include#includebank.husing std:cout;using std:cin;using std:endl;int main()Bank bank;int choice=0;cout welcome to bank system endl;while(choice!=4)cout 1: add an account endl;cout2: delete an account endl;cout 3: query an account endl;cout 4: exit endl;coutchoice;switch(choice)case 1:bank.append();break;case 2:bank.deletes();break;case 3:bank.query

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论