

已阅读5页,还剩34页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
面向对象的程序设计大作业报告 班级 姓名 总学号 题目2-1题目要求:用穷举法找出1100间的质数,显示出来。分别使用while, do-while,for循环语句实现。设计思路:通过判断整除比自己小的数的余数是否为零来判别质数 由于非平方数的因素必有一个小于其平方根值 故可以只验算那些比根值小的数来更快获得结果程序代码:使用while:void main()int i,j,k,flag; i=2; while(i=100) flag=1; k=sqrt(i); j=2; while(j=k) if(i%j=0) flag=0; breake; j+; if(flag) couti是质数endl; i+; 使用do while:void main()int i,j,k,flag; i=2; do flag=1; k=sqrt(i);j=2; do if(i%j=0) flag=0; breake; j+; while(j=k); if(flag) couti是质数endl; i+; while(i=100);使用for循环:void main() int i,j,k,flag; for(i=2;i=100;i+) flag=1; k=sqrt(i); for(j=2;j=k;j+) if(i%j=0) flag=0; breake; if(flag) couti是质数endl; 运行结果:心得体会:举一反三题目:2-2题目内容:输入一个有符号的十进制数,转换成机内二进制数输出(求 用位操作运算)。屏蔽低4位,再以2进制显示。设计思路:进行进制转换 按位进行右移程序代码:void main()char a;int t8;int i;couta;for(i=0;i1;for(i=7;i=0;i-)coutti;coutendl;system(pause);运行结果:心得体会:熟悉进制 的意义即可 题目3-1 题目:观察下面程序的运行输出,与你设想的有何不同?仔细体会引用的用法。源程序:#include int main() int intone; int &rsomeref = intone; intone = 5; cout intone:tt intone endl; cout rsomeref:t rsomeref endl; int inttwo = 8; rsomeref = inttwo; / not what you think! cout nintone:tt intone endl; cout inttwo:tt inttwo endl; cout rsomeref:t rsomeref endl; return 0;程序运行输出:心得体会:与设想一样 题目3-2题目要求:用递归的方法编写函数求n阶勒让德多项式的值,在主程序中实现输入、输出;设计思路:程序代码:int main() int x,n; float polya(int ,int ); coutxn; coutpolya(x,n)endl; return 0;float polya(int x,int n) if(n=0) return 1; else if(1=n) return x; else return ( (2*n-1)*x*polya(n-1,x) - (n-1)*polya(n-2,x) )/n;输出结果:心得体会:搞懂递归的意义即可 题目3-3题目要求:求任意2(3或n)个数之和,要求分别用函数:缺省参数,重载和模板函数完成设计思路:定义不同的函数让两个数相加,然后让其根据数据类型相加。 设计相加的函数,然后用主函数调用函数,运行结果。程序代码:函数重载:int sum(int m,int n)return m+n;double sum(double m,double n)return m+n;int main()int m,n;coutmn;cout他们的和为:sum(m,n)endl; double x,y; coutxy; cout他们的和为:sum(x,y)endl;return 0;缺省参数:int sum(int a,int b=0,int c=0) return a+b+c;int main()int const x=3,y=2, z=1;coutsum(x,y,z)endl;coutsum(x,y)endl;coutsum(x)endl;return 0;函数模版:t sum(t x,t y)return x+y;int main()int a=3,b=2;double c=3.2,d=2.3;cout他们的和:sum(a,b)endl;cout他们的和:sum(c,d)endl;return 0;输出结果:心得体会:函数重载比较简洁题目4-1题目要求 定义一个dog 类,包含的age、weight等属性,以及对这些属性操作的方法。实现并测试这个类。设计思路:程序代码:class dog private: int itsage; int itsweight; public: dog():itsage(0),itsweight(0) void setage(int i)itsage=i; void setweight(int i)itsweight=i; int age()return itsaage; int weight()return itsweight;int main() dog d; coutd.age() d.weight()endl; return 0;运行结果:心得体会: 题目4-2题目要求:定义一个矩形类,有长、宽两个属性,有成员函数计算矩形的面积 。设计思路:通过设计类来实现程序代码:#includeusing namespace std;class rectanglepublic:rectangle(); float area(); void show();private:float a;float b;rectangle:rectangle() do coutplease input two numbers :ab;while(a= 0 | b= 0); float rectangle:area()return a*b;void rectangle:show()couta=a,b=b,area=area()endl;int main()rectangle c;c.show();return 0;运行结果:心得体会:理解类的含义题目4-3题目要求:设计一个人事管理的“人员”类,包含编号,性别,出生日期,身份证号等,实现对人员信息的录入和显示。设计思路:通过构造函数实现程序代码:#includeusing namespace std;class dateprivate: int year; int month; int day;public: date(int a=0,int b=0,int c=0)year=a;month=b;day=c; inline void setyear(int y)year=y; void setmonth(int m) month=m; void setday(int d) day=d; void showdate() coutyear month day endl; ;class peopleprivate: char number100; char id100; char sex2; date birthday; public: people(); people(people&p); people(); void setnumber(char* a) strcpy(number,a); void setid(char*); void setsex(char* c) strcpy(sex,c); void setbirthday(date d) birthday=d; char *getnumber() return number; char *getsex() return sex; char *getid() return id; date getbirthday() return birthday; ;date d;char m;people:people():birthday(d)void people:setid (char*ids) strcpy(id,ids);int main() date birthday; cout录入信息endl; people p1; /people*p4=&p1,&p2,&p3,&p4; cout输入员工的出生日期endl; couta; birthday.setyear (a); coutb; birthday.setmonth (b); coutc; birthday.setday (c); cout输入编号numberstr; p1.setnumber (numberstr); cout输入身份证号idstr; p1.setid (idstr); cout输入性别sexstr; p1.setsex (sexstr); cout输出信息endl; cout员工的出生日期; birthday.showdate (); cout编号为 p1.getnumber() 身份证号为 p1.getid() 性别为 p1.getsex() ; return 0;运行结果:心得体会:只能单员工输入 题目5-1题目: 下面的程序的运行结果是什么,实际运行一下,看看与你的设想有何不同。#include void myfunction(); int x = 5, y = 7; int main() cout x from main: x n; cout y from main: y nn; myfunction(); cout back from myfunction!nn; cout x from main: x n; cout y from main: y n; return 0;void myfunction() int y = 10; cout x from myfunction: x n; cout y from myfunction: y nn;解:预计程序运行输出:x from main: 5y from main: 7x from myfunction: 5y from myfunction: 10back from myfunction!x from main: 5y from main: 7实际输出: 心得体会:输出结果和预估一致,感觉只要搞清楚各同名变量在函数中的值,参数传递的过程就可以。题目5-2题目要求: 定义一个cat类,拥有静态数据成员howmanycats,记录cat的个体数目;静态成员函数gethowmany(),存取howmanycats。设计程序测试这个类,体会静态数据成员和静态成员函数的用法。设计思路:定义一个cat类,通过构造函数,并且声明静态数据成员程序代码:#include #include using namespace std;class catpublic: cat()+howmanycats; cat(const cat& cat)+howmanycats; virtual cat()-howmanycats; static int gethowmany()return howmanycats;private: static int howmanycats;int cat:howmanycats=0;int main() cat a; cat b; couthowmanycats:cat:gethowmany()endl; cat c(a); cat* p=new cat(); couthowmanycats:cat:gethowmany()endl; delete p; couthowmanycats:cat:gethowmany()endl; return 0;运行结果:心得体会:理解静态数据成员的用法题目5-3题目要求:定义boat与car两个类,二者都有weight属性,定义二者的一个友元函数totalweight(),计算二者的重量和。设计思路:定义两个类,使其为友元函数,在其基础上进行所需的运算。程序代码:#includeusing namespace std;class car;class boatprivate: int boatweight;public: boat() boatweight=400; friend int totalweight(boat &,car &);class carprivate: int carweight;public: car( ) carweight=450; friend int totalweight(boat &,car &);int totalweight(boat &x,car &y) return x.boatweight+y.carweight;int main() boat a; car b; cout这两者的总重量为totalweight(a,b)endl; return 0;运行结果:心得体会:理解友元函数题目6-1 已知有一个数组名叫onearray,用一条语句求出其元素的个数。解: narraylength = sizeof(onearray) / sizeof(onearray0);题目6-2 题目要求:实现一个名为simplecircle的简单圆类,其数据成员int *itsradius为一个指向其半径值的指针,设计对数据成员的各种操作,给出这个类的完整实现并测试这个类。设计思路:利用类与友元函数程序代码:#include using namespace std;class simplecirclepublic:simplecircle();simplecircle(int);simplecircle(const simplecircle &);simplecircle() void setradius(int);int getradius()const;private:int *itsradius;simplecircle:simplecircle()itsradius = new int(5);simplecircle:simplecircle(int radius)itsradius = new int(radius);simplecircle:simplecircle(const simplecircle & rhs)int val = rhs.getradius();itsradius = new int(val);int simplecircle:getradius() constreturn *itsradius;int main()simplecircle circleone, circletwo(9);cout circleone: circleone.getradius() endl;cout circletwo: circletwo.getradius() endl;return 0;运行结果:心得体会:理解友元函数的使用题目6-3题目要求:编写一个函数,统计一个英文句子中字母的个数,在主程序中实现输入、输出。设计思路:逐字统计程序代码:#include#include using namespace std ;int main()string str;char ch ;int i=0,cnt=0; cout input str: ; getline(cin , str ); for ( i=0;i=a & stri=a & stri=z ) cnt+; cout str=strendl ; cout 字母个数: cnt endl ; system(pause);return 0;运行结果:心得体会:题目6-4题目要求: 定义一个employee类,其中包括表示姓名、街道地址、城市和邮编等属性,包括setname()和display()等函数;display()使用cout语句显示姓名、街道地址、城市和邮编等属性,函数setname()改变对象的姓名属性,实现并测试这个类。设计思路:构造类程序代码:#include using namespace std;class employee private: char *name,*address,*city,*postcode;public: employee(char *_name,char *_address,char *_city,char *_postcode) name = _name; address = _address; city = _city; postcode = _postcode; void setname(char *_name) name = _name; void display() coutname : nameendl; coutaddress : addressendl; coutcity : cityendl; coutpostcode : postcodedisplay(); e-setname(李四); e-display(); 运行结果:心得体会:熟悉指针的使用题目7-1题目要求:如果在派生类b已经重载了基类a的一个成员函数fn1(),没有重载成员函数fn2(),如何调用基类的成员函数fn1()、fn2()?答: 调用方法为: a:fn1(); fn2();题目7-2题目要求:定义一个document类,有name成员变量,从document派生出book类,增加pagecount变量。设计思路:构造类程序代码:#include#includeusing namespace std;class document public:document(string name) name=name;void display() /coutname=nameendl;private:string name; ;class book:public document public:book(string nam,int page):document(nam) pagecount=page; void show() /显示book类数据的函数coutpagecount=pagecountendl;private:int pagecount; /该类有数据成员pagecount;int main() book a(张三,24); a.display(); /显示数据name a.show(); /显示数据pagecountreturn 0;运行结果:心得体会:熟悉并理解类习题7-3题目要求:cube类:从点point类,派生矩形rectangle类,再rectangle类从派生出立体cube类,要求计算立体的表面积,和体积,并输出显示。设计思路:改造上一个rectangle类程序代码:#includeusing namespace std;class cubepublic:cube(); float area(); float volume(); void show();private:float a;float b;float c;cube:cube() do cout输入长宽高 :abc;while(a= 0 | b= 0|c=0); float cube:area()return 2*(a*b+b*c+a*c);float cube:volume()return a*b*c;void cube:show()cout长=a,宽=b,高=cendl;cout表面积=area()endl;cout体积=volume()endl;int main()cube c;c.show();return 0;运行结果:心得体会:熟悉派生类题目7-4题目要求:定义object类,有weight属性及相应的操作函数,由此派生出box类,增加height和width属性及相应的操作函数,声明一个box对象,观察构造函数与析构函数的调用顺序。设计思路:利用类的派生思想程序代码:#includeusing namespace std;class objectpublic:object()coutcall the objects constructorendl;void set_weight(int neww=0,int newn=0)weight=neww;num=newn;void total_weight()coutthe total weight is weight*numendl;int getw()return weight;object()coutcall the objects desconstructorendl;private:int weight,num;class box:public objectpublic:box() coutcall the boxs constructorendl;void set_digital(int h=0,int w=0,int l=0)height=h;width=w;length=l;void showbox()coutboxs height:heightendl;coutboxs width:widthendl; coutboxs length:lengthendl;coutboxs weightgetw()endl;box()coutcall the boxs desconstructorendl;private:int height,width,length;void main()box a;a.set_weight(4,5);a.total_weight();a.set_digital(1,2,3);a.showbox();运行结果:心得体会:理解派生类题目7-5题目要求:定义一个基类baseclass,从它派生出类derivedclass,baseclass有成员函数fn1()、fn2(),derivedclass也有成员函数fn1()、fn2(),在主程序中定义一个derivedclass的对象,分别用derivedclass的对象以及baseclass和derivedclass的指针来调用fn1()、fn2(),观察运行结果。设计思路:程序代码:#includeusing namespace std;class baseclasspublic:void fun1()cout1endl;void fun2()cout2endl;class derivedclass:public baseclasspublic:void fun1()cout3endl;void fun2()cout4endl;void main()derivedclass a;cout通过derivedclass的对象调用函数endl;a.baseclass:fun1(); a.baseclass:fun2(); a.fun1(); a.fun2(); baseclass &b=a; cout通过baseclass的指针调用函数endl; b.fun1(); b.fun2(); derivedclass &t=a; cout通过derivedclass的指针调用函数endl; t.baseclass:fun1(); t.baseclass:fun2(); t.fun1(); t.fun2();运行结果:心得体会:题目8-1题目要求:定义一个shape抽象类,在此基础上派生出rectangle和circle,二者都有getarea()函数计算对象的面积,getperim()函数计算对象的周长。设计思路:利用派生类知识程序代码:#include using namespace std;class shape /定义抽象类shapeprotected: double x,y;public: void set(double i=0,double j=0) x=i; y=j; virtual void area()=0; /声明纯虚函数area,用于求各种图形的表面积 virtual void perim()=0; ;class rectangle: public shape public: void area() /定义纯虚函数area cout矩形的面积是:x*yendl; void perim() cout矩形的周长:2*(x+y)endl; ;class circle: public shape public: void area() /定义纯虚函数area cout圆的面积是:3.14*x*xendl; void perim() cout圆的周长是:2*3.14*xset(5);p-area();p-perim();p=&r;p-set(3,2);p-area();p-perim();return 0;运行结果:心得体会:在派生类的基础上在派生,相当于把派生看作是基类习题8-2题目要求:分数类的运算符重载设计思路:程序代码:#include using namespace std; class fen public: /fen()a=0;b=0; fen(int x=1,int y=1)a=x;b=y; fen() void show()couta/bendl; fen operator+(fen &f1); fen operator=(fen &f1); private: int a,b; ; fen fen:operator +(fen &f1) int x,y,g(1); x=a*f1.b+b*f1.a; y=b*f1.b; for(int j=2;jx;j+) if(x%j=0&y%j=0&jj?g:j); a=x/g; b=y/g; return *this; fen fen:operator =(fen &f1) a=f1.a; b=f1.b; return *this; int main() fen f1(1,4),f2(3,5),f3=f1+f2; f3.show(); return 0; 运行结果:心得体会:题目8-3题目要求:定义一个基类baseclass,从它派生出类derivedclass,baseclass有成员函数fn1()、fn2(),fn1()是虚函数,derivedclass也有成员函数fn1()、fn2(),在主程序中定义一个derivedclass的对象,分别用baseclass和derivedclass的指针来调用fn1()、fn2(),观察运行结果。设计思路:程序代码:#includeusing namespace std;class baseclasspublic:void fun1()cout1endl;void fun2()cout2endl;class derivedclass:public baseclasspublic:void fun1()cout3endl;void fun2()cout4endl;int main()derivedclass a;cout通过derivedclass的对象调用函数endl;a.baseclass:fun1(); a.baseclass:fun2(); a.fun1(); a.fun2(); baseclass &b=a; cout通过baseclass的指针调用函数endl; b.fun1(); b.fun2(); derivedclass &t=a; cout通过derivedclass的指针调用函数en
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论