当前位置:文档之家› 西南交通大学2012年面向对象程序设计

西南交通大学2012年面向对象程序设计

西南交通大学2012年面向对象程序设计试题及答案 开发环境为VC++6.0,运行结果有截图,若发现错误欢迎指正。 实验一、C++程序开发环境及c++简单程序设计。 题目1、简单c++程序 任务: 按提示的操作步骤输入下面的代码,编译连接并执行。 源程序代码: #include"iostream.h" void main() { cout<<"Hello!\n"; cout<<"Welcome to c++!"

题目2、 编写程序,从标准输入读入一个数,把它转化为英文单词输出,如输入123,这是输出“one two three”。 源程序代码: #include using namespace std; void main() { char ch; cout <<"请输入数字用来转化为英文单词:"; while(1) { cin>>ch;if(ch=='\n') break; switch(ch-48) { case 0:cout<<"zero "; break; case 1:cout <<"one "; break; case 2:cout <<"two "; break; case 3:cout <<"three ";break; case 4:cout <<"four "; break; case 5:cout <<"five "; break; case 6:cout <<"six "; break; case 7:cout <<"seven ";break; case 8:cout <<"eight ";break; case 9:cout <<"nine "; break; } } } 运行结果:

题目 3、循环结构程序设计 任务 把一张一元纸币换成一分、二分和五分的硬币,假如每一种至少一枚,文友多少种换法,编程将各种换法显示出来。 源程序代码: #include using namespace std; void main() { int i,j,k,x=0; cout<<"一角 "<<"两角 "<<"三角 "

(由于种数太多截图不方便所以只截了最后的部分!) 实验二、函数 题目1、内联函数 任务 (1)定义内敛函数max(),求两个整数中的最大值,然后在main()函数中惊醒调用 (2)定义内联函数inline-fun()和一般函数common-fun(),使整型参数值加1,然后在main()函数中惊醒调用; 源程序代码: #include using namespace std; inline int max(int x,int y); inline int inline_fun(int x); int common_fun(int x); void main() { int x=4,y=5; couty) return x; else return y; } inline int inline_fun(int x) { return ++x;} int common_fun(int x) { return ++x;} 运行结果:

题目2、函数参数的传递机制、重载函数 任务 (1)编写重载函数max1()可分别求2个整数、3个整数、2个双精度和3双精度数的最大值。 (2)定义两个名称都为sum()的函数,第一个函数支持整型数组,第二个函数支持浮点型数组,求数组元素的和。 源程序代码: #include using namespace std; int max1(int x,int y) { if(x>y) return x;else return y;} int max1(int x,int y,int z) { if(x>max1(y,z)) return x;else return max1(y,z); } double max1(double x,double y) { if(x>y) return x;else return y;} double max1(double x,double y,double z) { if(x>max1(y,z) ) return x;else return max1(y,z);} int sum(int *p,int n) { int i=0,s=0; for(;is+=p[i]; return s; } double sum(double *p,int n) { int i; double s=0; for(i=0;is+=p[i]; return s; } void main() { int a=1,b=2,c=3; double x=4.5, y=6.7, z=8.9; int p[5]={1,2,3,4,5}; double q[5]={4.5 ,6.7,8.9,2.4,1.2}; cout

} 运行结果:

题目 3、带默认参数的函数 任务 定义函数volume(),计算立体的体积,要求在主函数中以5中不同的形式调用此函数。 源程序代码: #include using namespace std; double volume(double x=1,double y=2,double z=3); void main() { double x=4,y=5,c=6; cout

实验三、类于对象 题目1、私有成员的访问 任务 下面的程序中用ERROR标明的语句有错误,在不删除和增加代码行的情况下,改正错误的语句,使其正确运行。 错误代码及改正方法: #include using namespace std; class Aa { public : Aa(int i=0){ a=i; cout<<"Constructor"<~Aa() { cout<<"Destructor"

问题: (1)在该程序中将Topint类的带有两个参数的构造函数进行修改,在函数体内添加下述语句:cout<<"Constructor is Called.\n"。 对程序的输出结果进行分析和说明。 运行结果:

相关主题