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

上传人:s9****2 文档编号:565044300 上传时间:2023-12-11 格式:DOC 页数:20 大小:82KB
返回 下载 相关 举报
《面向对象程序设计》答案_第1页
第1页 / 共20页
《面向对象程序设计》答案_第2页
第2页 / 共20页
《面向对象程序设计》答案_第3页
第3页 / 共20页
《面向对象程序设计》答案_第4页
第4页 / 共20页
《面向对象程序设计》答案_第5页
第5页 / 共20页
点击查看更多>>
资源描述

《《面向对象程序设计》答案》由会员分享,可在线阅读,更多相关《《面向对象程序设计》答案(20页珍藏版)》请在金锄头文库上搜索。

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程序二:#include vo

2、id 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 21程序五:#include vo

3、id 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、了解在面向对象程序设计过程中C+对C功能的扩

4、充与增强,并善于在编写程序的过程中应用这些新功能。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 namespace std;int ma

5、x(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 main()int max(int a,

6、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 sorted strings :

7、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) return ab?a:b;int ma

8、in() 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;class Datepublic:Da

9、te(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 Datedisplay( )cout

10、month/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() cout -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的定义如下,请实现该类,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 习题/试题

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号