面向对象程序设计26125_第1页
面向对象程序设计26125_第2页
面向对象程序设计26125_第3页
面向对象程序设计26125_第4页
面向对象程序设计26125_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

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

文档简介

1、目录一、项目名称(从cc+):27、设计一个函数求2二、项目名称:(类和对象)37、已知一个时钟类cclock定义如下3三、项目名称(继承与派生):51、已知类cstring和main()函数53、写一个程序,定义抽象基类cshape,由它派生出5个派生类:ccircle(圆形),csquare(正方形),crectangle(矩形),ctrapezoid(梯形),ctriangle(三角形),用一个虚函数分别计算几种图形的面积,并求它们的和。要求用基类指针数组,使它每一个元素指向一个派生类对象。10四、项目名称:(opp中的模板):133、设计一个函数模板mam(t)求一个数组中最大的元素,

2、并以整数数组和字符数组进行调用,并采用相关数据进行测试。13五、心得体会13一、项目名称(从cc+):7、设计一个函数求程序如下:#include <stdio.h>int fun(int n,int m)int i,j;float s=1.0;for (i=1;i<=m;i+)s=1.0*s*(n-i+1)/i;return (int)s;void main()int m,n,s;printf("input n m (n>m): ");scanf("%d%d",&n,&m);s=fun(n,m);printf(&

3、quot;s = %dn",s);printf("按任意键继续n");scanf("%c",&s);二、项目名称:(类和对象)7、已知一个时钟类cclock定义如下class cclockprivate:int hour,minute,second;public:.void display()cout<<hour<<':'<<minute<<':'<<second<<endl;完成相关成员函数的定义,达到模拟时钟的目的如下图:时钟cc

4、lock#include <iostream>#include <windows.h>using namespace std;class time              private: int h; int m; int s;public: time(); time(int h, int m, int s); time()  cout<<"des

5、truct"<<endl;  void show(); void tick(); void run();time:time()  h = 0; m = 0; s = 2;time:time(int h, int m, int s)  this->h = h; this->m = m; this->s = s;void time:show()  cout << h << ":" << m &l

6、t;< ":" << s << endl; fflush(stdout);void time:tick()  sleep(1000); if (-this->s < 0)   this->s = 59;  if (-this->m < 0)    this->m = 59;   if (-this->h < 0)    this-

7、>m = 0;   void time:run()  while (this->h > 0 | this->m > 0 | this->s > 0)   show();  tick(); int main()  time t(11, 20, 5); t.run(); return 0;三、项目名称(继承与派生):1、已知类cstring和main()函数-必做题class cstringchar *str;int size;publi

8、c:.;(1)、主程序如下:void main()cstring s1(“is a wondful ”);/调用有字符串参数的构造函数cstring s2(“programming language!”);cstring s3; /调用没有参数的构造函数s3=”c+”+(s1+s2) /调用复制(拷贝)构造函数,同时还要对“”进行重载试编写一个程序,以实现main()函数中的字符串拼接。输出结果如下:程序如下:#include<iostream>#include<string.h>#include<assert.h>using namespace std;c

9、lass cstringpublic: cstring(); cstring(const char* str); cstring(const cstring &other); cstring();public: cstring operator+(const char *lpsztext); cstring operator+(const cstring &other); cstring& operator+=(const char *lpsztext); cstring& operator+=(const cstring &other); cstrin

10、g& operator=(const char *lpsztext); cstring& operator=(const cstring &other); friend ostream & operator << (ostream &os, cstring &str) os<<str.str; return os; friend istream & operator >> (istream &is, cstring &str) char buf 1024 = 0 ; is>>

11、buf; str = buf; return is; private: char *str; int size; cstring:cstring() size = 140; str = new charsize; memset(str, 0, sizeof(char)*size);cstring:cstring(const char* str) assert(str); size = strlen(str)*2 + 1; this->str = new charsize; memcpy(this->str, str, strlen(str) + 1);cstring:cstring

12、(const cstring &other) size = other.size; this->str = new charsize; memcpy(this->str, other.str, size);cstring:cstring() if(str) delete str; size = 0;cstring cstring:operator+(const char *lpsztext) int nlen = strlen(lpsztext) + strlen(this->str) + 1; cstring str = *this; str += lpsztext

13、; return str;cstring cstring:operator+(const cstring &other) int nlen = strlen(other.str) + strlen(this->str) + 1; cstring str = *this; str += other.str; return str;cstring& cstring:operator+=(const char *lpsztext) int nlen = strlen(lpsztext) + 1; if (this->size < strlen(this->st

14、r) + nlen) this->str = (char*)realloc(this->str, (this->size) = 2 * (strlen(this->str) + nlen); strcat(this->str, lpsztext); return *this;cstring& cstring:operator+=(const cstring &other) int nlen = strlen(other.str) + 1; if (this->size < strlen(this->str) + nlen) thi

15、s->str = (char*)realloc(this->str, (this->size) = 2 * (strlen(this->str) + nlen); strcat(this->str, other.str); return *this;cstring& cstring:operator=(const char *lpsztext) int nlen = strlen(lpsztext) + 1; if(this->size < nlen) delete this->str; this->size = nlen * 2;

16、 this->str = new charsize; strcpy(this->str, lpsztext); return *this;cstring& cstring:operator=(const cstring &other) int nlen = strlen(other.str) + 1; if (this->size < nlen) delete this->str; this->size = nlen * 2; this->str = new charsize; strcpy(this->str, other.st

17、r); return *this;cstring operator+(const char *lpsztext, cstring &other) cstring str = lpsztext; str += other; return str;int main() cstring s1(" is a wondful "); cstring s2(" programming languages! "); cstring s3; s3 = "c+" + s1 + s2; cout<<s1<<endl; co

18、ut<<s2<<endl; cout<<s3<<endl; return 0;(2)、主程序如下: void main()cstring s1(“c+ is a wondful ”);/调用有字符串参数的构造函数cstring s2(“programming ”);cstring s3; /调用没有参数的构造函数s3= (s1+s2)+ “language!”/调用复制(拷贝)构造函数,同时还要对“”进行重载试编写一个程序,以实现main()函数中的字符串拼接。输出结果如(1)的结果程序如下:(前面相同)int main() cstring s1

19、(" c+ is a wondful "); cstring s2(" programming"); cstring s3; s3 = (s1 + s2)+" language! " cout<<s1<<endl; cout<<s2<<endl; cout<<s3<<endl; return 0;运行结果:(3)、主程序如下: void main()cstring s1(“c+ is a wondful ”);/调用有字符串参数的构造函数cstring s2( “

20、language!”);cstring s3; /调用没有参数的构造函数s3= s1+ “programming ”+s2 /调用复制(拷贝)构造函数,同时还要对“”进行重载试编写一个程序,以实现main()函数中的字符串拼接。输出结果如(1)的结果程序如下:int main() cstring s1(" c+ is a wondful "); cstring s2(" language!"); cstring s3; s3 = (s1 + "programming" + s2); cout<<s1<<endl

21、; cout<<s2<<endl; cout<<s3<<endl; return 0;运行结果:3、写一个程序,定义抽象基类cshape,由它派生出5个派生类:ccircle(圆形),csquare(正方形),crectangle(矩形),ctrapezoid(梯形),ctriangle(三角形),用一个虚函数分别计算几种图形的面积,并求它们的和。要求用基类指针数组,使它每一个元素指向一个派生类对象。程序如下:#include <iostream.h>class cshapepublic: virtual double getarea

22、() = 0;class ccircle:public cshapepublic: ccircle(double r):m_r(r) virtual double getarea() return (3.14 *m_r * m_r); private: double m_r;class csquare:public cshapepublic: csquare(double a):m_a(a) virtual double getarea() return (m_a * m_a); private: double m_a;class crectangle:public cshapepublic:

23、 crectangle(double a, double b):m_a(a), m_b(b) virtual double getarea() return (m_a * m_b); private: double m_a; double m_b;class ctrapeziod:public cshapepublic: ctrapeziod(double a, double b, double h):m_a(a), m_b(b), m_h(h) virtual double getarea() return (m_a + m_b) * m_h / 2); private: double m_

24、a; double m_b; double m_h;class ctriangle:public cshapepublic: ctriangle(double a, double h):m_a(a), m_h(h) virtual double getarea() return (m_a * m_h / 2); private: double m_h; double m_a;int main() cshape* pshape5; pshape0 = new ccircle(8); pshape1 = new ctriangle(4,5); pshape2 = new csquare(5); p

25、shape3 = new ctrapeziod(55,45,7); pshape4 = new crectangle(4,55); cout<<"圆形面积:"<<pshape0->getarea()<<endl; cout<<"三角形面积:"<<pshape1->getarea()<<endl; cout<<"正方形面积:"<<pshape2->getarea()<<endl; cout<<"梯琘形面积:"<<pshape3->getarea()<<endl; cout<<"长方形面积:"<<pshape4->getarea()<<endl; double sum = 0; cout<<"面积总和为:" fo

温馨提示

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

评论

0/150

提交评论