江南大学《面向对象的程序设计》大作业报告.doc

上传人:文库蛋蛋多 文档编号:2386073 上传时间:2023-02-17 格式:DOC 页数:39 大小:495KB
返回 下载 相关 举报
江南大学《面向对象的程序设计》大作业报告.doc_第1页
第1页 / 共39页
江南大学《面向对象的程序设计》大作业报告.doc_第2页
第2页 / 共39页
江南大学《面向对象的程序设计》大作业报告.doc_第3页
第3页 / 共39页
江南大学《面向对象的程序设计》大作业报告.doc_第4页
第4页 / 共39页
江南大学《面向对象的程序设计》大作业报告.doc_第5页
第5页 / 共39页
点击查看更多>>
资源描述

《江南大学《面向对象的程序设计》大作业报告.doc》由会员分享,可在线阅读,更多相关《江南大学《面向对象的程序设计》大作业报告.doc(39页珍藏版)》请在三一办公上搜索。

1、面向对象的程序设计大作业报告 班级 姓名 总学号 题目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是质数end

2、l; 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; 运

3、行结果:心得体会:举一反三题目: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

4、 = 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阶勒让德多项式的值,在主程序中实现输入、输出;设

5、计思路:程序代码: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)个数之和,要求分别用函数:缺省参数,重载和模板函数完成设计思路:定义不同的函数让两个数

6、相加,然后让其根据数据类型相加。 设计相加的函数,然后用主函数调用函数,运行结果。程序代码:函数重载: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,

7、 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 i

8、tsAge; 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题目要求:定义一个矩形类,有长、宽两个属性,有成员函数计算矩形的面积 。设计思路:通过设计类来实现程序代码:

9、#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.sho

10、w();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

11、=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 setbi

12、rthday(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,

13、&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.getnu

14、mber() 身份证号为 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:

15、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

16、类,拥有静态数据成员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 HowMa

17、nyCats;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两个类,二者都有weig

18、ht属性,定义二者的一个友元函数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

19、(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

20、 *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()

21、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 Circle

22、One: 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 &

23、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 privat

24、e: 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 : city

25、endl; 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;

26、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 ma

27、in() 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();priv

28、ate: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类,有

29、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*nu

30、mendl;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:widthe

31、ndl; 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

32、(),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(

33、)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();

34、运行结果:心得体会:题目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圆的面积是:

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号