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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

《面向对象程序的设计》答案.docx

1、面向对象程序的设计答案实验一 熟悉VC+IDE开发环境一、实验目的1、熟悉VC+6.0集成开发环境,熟练掌握VC+6.0项目工作区、各种编辑器、菜单栏和工具栏的使用。2、掌握如何编辑、编译、连接和运行一个C+程序。3、通过运行简单的C+程序,初步了解C+源程序的结构和特点。二、实验要求1、分析下列程序运行的结果。程序一:#include int add(int x,int y=8);void main() int x=4; coutadd(x),; coutadd(x,add(add(x,add(x)endl;int add(int x,int y) return x+y;/12,28程序二:

2、#include void main() int *p,i; i=5;p=&i;i=*p+10;couti=iendl;/i=15程序三:#include void main(void) int i=10; int &r=i; r+; couti=i, r=rn; i=88; couti=i, r=rn;/i=11,r=11i=88,r=88程序四:#include int f(int i) static int k=1; for(;i0;i-) k +=i; return k; void main() int i; for(i=0;i5;i+) coutf(i) ; / 1 2 5 11 2

3、1程序五:#include void func();int n=1; void main() static int a; int b= -9; cout a:a b:b n: nendl;b+=4; func();cout a:a b:b n:nendl;n+=10; func();void func() static int a=2; int b=5; a+=2;n+=12;b+=5; cout a: a b: b n: n endl; / a:0 b:-9 n:1a:4 b:10 n:13a:0 b:-5 n:13a:6 b:10 n:35实验二 C+对C的扩充一、实验目的1、了解在面向对

4、象程序设计过程中C+对C功能的扩充与增强,并善于在编写程序的过程中应用这些新功能。2、进一步熟悉编辑、编译、连接和运行C+程序的方法。3、进一步熟悉C+程序的结构和编程方法。二、实验要求1、分析下列程序运行的结果。#include int amount=123; void main() int amount=456; cout:amount,; coutamount,; :amount=789; cout:amount,; coutamountn; / 123,456,789,4562、编写一个程序,用来求2个或3个正整数中的最大数。用不带默认参数的函数实现。include using nam

5、espace std;int max(int a,int b,int c) /求3个整数中的最大者if (ba) a=b; if (ca) a=c; return a; int max(int a, int b) /求两个整数中的最大者if (ab) return a; else return b;int main( )int a=7,b=-4,c=9; coutmax(a,b,c)endl; /输出3个整数中的最大者 coutmax(a,b)endl; /输出两个整数中的最大者 return 0;用带默认参数的函数实现。#include using namespace std;int mai

6、n()int max(int a,int b,int c=0); int a,b,c; cinabc; coutmax(a,b,c)=max(a,b,c)endl; coutmax(a,b)=max(a,b)a) a=b; if(ca) a=c; return a;3、有5个字符串,要求对它们按由小到大顺序排列,用string方法。#include #include using namespace std;int main() int i; string str5=BASIC,C,FORTRAN,C+,PASCAL; void sort(string ); sort(str); coutthe

7、 sorted strings :endl; for(i=0;i5;i+) coutstri ; coutendl; return 0;void sort(string s)int i,j; string t; for (j=0;j5;j+) for(i=0;isi+1) t=si;si=si+1;si+1=t; 4、定义一个求两个数中较小值的函数模板min( ),要求在main( )函数中进行调用求两个浮点型数据和两个整型数据中较小的数。#include iostream#include stringusing namespace std;templateT min(T a,T b) ret

8、urn ab?a:b;int main() int a=1, b=9; float c=1.23471,d=32.431564; coutThe min of a and b is min(a,b)endl The min of c and d is min(c,d)endl; return 0;实验三 类和对象(一)一、实验目的1、掌握声明类的方法,类和类的成员的概念以及定义对象的方法。2、掌握类的构造函数与析构函数的概念和使用方法。3、初步掌握用类和对象编制基于对象的程序。二、实验要求1、分析下面的程序,写出其运行时的输出结果。#include using namespace std;cl

9、ass Datepublic:Date(int,int,int);Date(int,int);Date(int);Date( );void display( );private:int month;int day;int year;DateDate(int m,int d,int y):month(m),day(d),year(y) DateDate(int m,int d):month(m),day(d) year=2005; DateDate(int m):month(m) day=1;year=2005;DateDate( ) month=1;day=1;year=2005;void D

10、atedisplay( )coutmonth/day/yearendl;int main( ) Date d1(10,13,2005);Date d2(12,30);Date d3(10);Date d4;d1.display( );d2.display( );d3.display( );d4.display( );return 0;/ 10/13/2005 12/30/2005 10/1/2005 1/1/20052、建立一个名为Student的类,该类有以下几个私有成员变量:学生、学号、性别、年龄。还有以下两个成员变量:一个用于初始化学生、学号、性别和年龄的构造函数,一个用于输出学生信息的

11、函数。编写一个主函数,声明一个学生对象,然后调用成员函数在屏幕输出学生信息。#include iostream#include stringusing namespace std;class student public: student(); void display(); private: string sName,sNum; char chSex; int iAge;student:student(string na,string num,char s,int a):sName(na),sNum(num), chSex(s),iAge(a)void student:display() c

12、out -THE INFORMATION OF STUDENT-n; cout name: sName endl number: sNumendl sex: chSex endl age: iAge endl;int main() student s(WangFang,0811045263,w,20); s.display(); return 0;3、类Person的定义如下,请实现该类,并在主函数中创建对象obj,然后使用构造函数为obj赋予初始值(容自定)。class Person private: char name10; int age; int salary; char tel8;p

13、ublic: Person(char *xname,int xage,int xsalary,char *xtel); void disp();解:#include #include Person:person(char *Xname,int Xage,int Xsalary,char *Xtel) strcpy ( name, xname);age=xage;salary=xsalary;strcpy (tel,xtel);void Person:disp() cout“ :”nameendl;cout“ 年龄”:ageendl;cout“ 工资”:salaryendl:cout“ ”:te

14、lendl;void main() Person obj (“三”,25,850,“45678912”); obj.disp()实验四 类和对象(二)一、实验目的1、进一步加深对类和对象的理解。2、掌握对类的对象数组、对象的指针及其使用方法。3、掌握友元的概念和使用。4、了解类模板的使用方法。二、实验要求1、分析并比较下列程序运行的结果。程序一:#include#includeclass smallonepublic:smallone(int sma) coutsm constr:sman;void fn(int n) smallone sm(n);coutin function fn wit

15、h n=nendl;int main() fn(10); fn(20); return 0;/sm constr: 10in function fn with n=10sm constr: 20in function fn with n=20程序二:#include#includeclass smallonepublic:smallone(int sma) coutsm constr:sman;void fn(int n) static smallone sm(n);coutin function fn with n=nendl; int main() fn(10); fn(20); retu

16、rn 0;/sm constr:10in function fn with n=10in function fn with n=202、建立一个对象数组,放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。#include using namespace std;class Student public: Student(int n,float s):num(n),score(s) int num; float score; ;void main()Student stud5= Student(101,78.5)

17、,Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,95.5); void max(Student* ); Student *p=&stud0; max(p); reyurn 0; void max(Student *arr)float max_score=arr0.score; int k=0; for(int i=1;imax_score) max_score=arri.score;k=i; coutarrk.num max_scoreendl; 3、声明一个类模板,利用它分别实现两个整数、浮点数和字符的

18、比较,求出大数和小数。#include using namespace std;templateclass Compare public: Compare(numtype a,numtype b) x=a;y=b; numtype max() return (xy)?x:y; numtype min() return (xy)?x:y; private: numtype x,y; ;int main()Compare cmp1(3,7); coutcmp1.max() is the Maximum of two inteder numbers.endl; coutcmp1.min() is t

19、he Minimum of two inteder numbers.endlendl; Compare cmp2(45.78,93.6); coutcmp2.max() is the Maximum of two float numbers.endl; coutcmp2.min() is the Minimum of two float numbers.endlendl; Compare cmp3(a,A); coutcmp3.max() is the Maximum of two characters.endl; coutcmp3.min() is the Minimum of two ch

20、aracters.endl; return 0;实验五 运算符重载一、实验目的1、进一步了解运算符重载的概念和使用方法。2、掌握几种常用的运算符重载的方法。二、实验要求1、定义一个复数类Complex,重载运算法“+”,使之能用于复数的加法运算。将运算符重载为普通函数(非成员、非友元)、成员函数、友元函数。根据要求修改通过函数来实现复数相加的示例,分别编写程序,求两个复数之和。#include using namespace std;class Complex /定义Complex类public: Complex(float x=0,float y=0)real=x;imag=y; /构造函数

21、 Complex complex_add(Complex &c2); /声明复数相加函数 void display() coutreal+imagiendl; ; private: float real; /实部 float imag; /虚部;Complex Complex:complex_add(Complex &c2) Complex c;c.real = real +c2.real;c.imag=imag+c2.imag;return c;int main() Complex complex1(3.34f, 4.8f), complex2(12.8f, 5.2f);Complex co

22、mplex; /定义3个复数对象complex=plex_add(complex2); / 进行两个复数的加运算complex.display( ); return 0;/16.14+10i/普通函数(非成员、非友元)#include using namespace std;class Complex public: Complex()real=0;imag=0; Complex(double r,double i)real=r;imag=i; double get_real(); double get_imag(); void display(); private: double real;

23、 double imag; ; double Complex:get_real()return real;double Complex:get_imag()return imag;void Complex:display()cout(real,imagi)endl;Complex operator + (Complex &c1,Complex &c2) return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag();int main()Complex c1(3,4),c2(5,-10),c3; c3=c1+c2; c

24、outc3=; c3.display(); return 0; /运算符重载为成员函数#include using namespace std;class Complex public: Complex()real=0;imag=0; Complex(double r,double i)real=r;imag=i; Complex operator + (Complex &c2); void display(); private: double real; double imag; ;Complex Complex:operator + (Complex &c2)Complex c; c.re

25、al=real+c2.real; c.imag=imag+c2.imag;return c;void Complex:display()cout(real,imagi)endl;int main()Complex c1(3,4),c2(5,-10),c3; c3=c1+c2; coutc1=;c1.display(); coutc2=;c2.display(); coutc1+c2=;c3.display(); return 0;/将运算符重载为友元函数#include using namespace std;class Complex public: Complex()real=0;imag=0; Complex(double r)real=r;imag=0; Complex(double r,double i)real=r;imag=i; friend Complex operator+ (Complex &c1,Complex &c2); void display(); private: double real; double imag; ;Complex operator+ (Complex &c1,Complex &c2) return Complex(c1.real+c2.real, c1.imag+c2.i

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

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