ImageVerifierCode 换一换
格式:DOCX , 页数:15 ,大小:90.05KB ,
资源ID:11337241      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/11337241.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C语言程序设计实验报告10.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C语言程序设计实验报告10.docx

1、C语言程序设计实验报告10C+语言程序设计实验报告基础题一实验任务:定义一个哺乳动物Mammal类,再由此派生出狗Dog类,二者都定义 Speak()成员函数,基类中定义为虚函数,定义一个Dog类的对象,调用Speak函数,观察运行结果。代码:#includeusing namespace std;class Animalpublic:virtual void Speak(); ;void Animal:Speak()coutAnimal中的SPEAK()函数被调用;class Dog:virtual public Animalpublic:virtual void Speak(); ;voi

2、d Dog:Speak()coutDog中的SPEAK()函数被调用endl;void main()Dog Dog1;Dog1.Speak();结果:基础题二实验任务:对Point类重载+(自增)、-(自减)运算符代码:#includeusing namespace std;class Pointpublic:Point(int x=0,int y=0)this-x=x;this-y=y;void show();Point& operator+();Point operator+(int);Point& operator-();Point operator-(int);private:int

3、x,y; ;void Point:show()cout该点坐标为(x,y)endl;Point& Point:operator+()x+;y+;return *this;Point Point:operator+(int)Point old=*this;+(*this);return old;Point& Point:operator-()x-;y-;return *this;Point Point:operator-(int)Point old=*this;-(*this);return old;int main()Point myPoint(1,3);cout第一次输出 ;myPoint.

4、show();coutShow myPoint+: ;(myPoint+).show();coutShow +myPoint: ;(+myPoint).show();coutShow myPoint-: ;(myPoint-).show();coutShow -myPoint: ;(-myPoint).show();return 0;结果:提高题一实验任务:设计一个字符串类MyString,具有构造函数、析构函数、拷贝构造函数,重载运算符+,尽可能地完善它,使之能满足各种需要。代码:#include #include class person public: person(char *pn);

5、 person(person &p); person(); private: char *pname; ; person:person(char *pn) cout构造函数: pnendl; pname=new charstrlen(pn)+1; if(pname!=0) strcpy(pname,pn); person:person(person &p) cout拷贝 p.pname 到新的堆空间n; pname=new charstrlen(p.pname)+1; if(pname!=0) strcpy(pname,p.pname); person:person() cout析构函数: p

6、nameendl; pname0=0; delete pname; void main() person p1(jack); person p2(p1); 结果:提高题二实验任务:声明一个Shape抽象类,再此基础上派生出Rectangle和Circle类,二者都有GetArea()函数计算对象的面积,GetPerim()函数计算对象的周长。代码:#includeusing namespace std;#define n 3.14class Shapepublic:virtual void GetArea()=0;virtual void GetPerim()=0;class Rectangl

7、e:public Shapepublic:Rectangle(int a,int b)this-a=a;this-b=b;void GetArea();void GetPerim();private:int a,b;void Rectangle:GetArea()cout矩形面积为a*bendl; void Rectangle:GetPerim()cout矩形周长为a)+(2*this-b)c=c;void GetArea();void GetPerim();private:int c;void Circle:GetArea()cout圆形面积为c*c*nendl; void Circle:G

8、etPerim()cout圆形周长为c)GetArea();void fun1(Shape *ptr)ptr-GetPerim();void main()Rectangle obj(3,4);Circle obj1(2); fun(&obj);fun1(&obj);fun(&obj1);fun1(&obj1);结果:选做题一实验任务:编写一个程序,计算正方体、球体和圆柱体的表面积和体积。(要求:抽象出一个公共基类作为虚基类,运用虚函数的知识设计程序)代码:#includeusing namespace std;#define n 3.14class Shujupublic:virtual vo

9、id biaomianji()=0;virtual void tiji()=0;class zhengfangti:public Shujupublic:zhengfangti(int a)this-a=a;void biaomianji();void tiji();private:int a ;void zhengfangti:biaomianji()cout正方形的表面积为6*a*aendl;void zhengfangti:tiji()cout正方形的体积为a*a*ab=b;void biaomianji();void tiji();private:int b ;void qiuti:b

10、iaomianji()cout球体的表面积为4*n*b*bendl;void qiuti:tiji()cout球体的体积为(4.0/3.0)*n*b*b*br=r;this-d=d;void biaomianji();void tiji();private:int r,d;void yuanzhuti:biaomianji()cout圆柱体的表面积为2*n*r*r+2*n*r*dendl;void yuanzhuti:tiji()cout圆柱体的体积为n*r*r*dbiaomianji();void fun1(Shuju *ptr)ptr-tiji();void main()zhengfang

11、ti zheng(2);qiuti qiu(3);yuanzhuti yuanzhu(4,5);fun(&zheng);fun1(&zheng);fun(&qiu);fun1(&qiu);fun(&yuanzhu);fun1(&yuanzhu);结果:选做题二实验任务:编写一个分数类fraction ,其分子、分母为整数,通过重载运算符+、-、*、/ ,实现该类数据之间的四则运算,要求计算结果为最简分数,如(7/3)*(9/14)=(3/2)4*(7/8)=(7/2)代码:#include using namespace std;class fractionpublic:fraction(in

12、t n=1,int d=1)num=n;den=d;divideZero();fraction:divideZero()if (0 = den)cout 输入非法,分母为零! endl;return true;return false; fraction& operator=(const fraction& other)num=other.num;den=other.den;return *this;fraction operator+(const fraction& other)fraction res;res.num=num*other.den+den*other.num;res.den=

13、den*other.den;return res;fraction operator-(const fraction& other)fraction res;res.num=num*other.den-den*other.num;res.den=den*other.den;return res;fraction operator*(const fraction& other)return fraction(num*other.num,den*other.den);fraction operator/(const fraction& other)return fraction(num*other.den,den*other.num);void display()coutnum/denendl;private:int num,den;void main()fraction a(2,5);fraction b(a);fraction c;b=fraction(3,7);c=a+b;c.display();c=a-b;c.display();c=a*b;c.display();c=a/b;c.display();结果:

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1