一、第一类题目(20道,每题7分,在word中保留代码并将输出结果窗口保留) 1.定义盒子Box类,要求具有以下成员:长、宽、高分别为x,y,z,可设置盒子形状;可计算盒子体积;可计算盒子的表面积。 #include
int x,y,z; int v,s; public:
void int(int x1=0,int y1=0,int z1=0) {x=x1;y=y1;z=z1;} void volue() {v=x*y*z;}
void area() {s=2*(x*y+x*z+y*z);} void show()
{cout<<\ cout<<\ } };
void main() { Box a;
a.init(2,3,4); a.volue(); a.area(); a.show(); }
2.有两个长方柱,其长、宽、高分别为:(1)30,20,10;(2)12,10,20。分别求他们的体积。编一个基于对象的程序,在类中用带参数的构造函数。 #include
Box(int,int,int);//带参数的构造函数 int volume(); private: int length; int width; int height;
};
Box::Box(int len,int h,int w) {length=len; height=h; width=w; }
//Box::Box(int len,int w,int,h):length(len),height(h),width(w){} int Box::volume()
{return(length*width*height); }
【第 1 页 共 48 页】
int main() {
Box box1(30,20,10);
cout<<\ Box box2(12,10,20);
cout<<\ return 0; }
3.有两个长方柱,其长、宽、高分别为:(1)12,20,25;(2)10,30,20。分别求他们的体积。编一个基于对象的程序,且定义两个构造函数,其中一个有参数,一个无参数。
#include
using namespace std; class Box {public:
Box();
Box(int len,int w ,int h):length(len),width(w),height(h){} int volume(); private:
int length; int width; int height; };
int Box::volume()
{return(length*width*height); }
int main() {
Box box1(10,20,25); cout<<\ Box box2(10,30,20);
cout<<\ return 0; }
4.声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小数。
#include
template
【第 2 页 共 48 页】
Compare(numtype a,numtype b) {x=a;y=b;} numtype max()
{return (x>y)?x:y;} numtype min()
{return (x numtype x,y; }; int main() {Compare cout< cout< cout< cout< cout< 5.建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据。初值自拟。 #include Student(int n,double s):num(n),score(s){} void display(); private: int num; double score; }; void Student::display() {cout< int main() {Student stud[5]={ Student(101,78.5),Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,95.5)}; Student *p=stud; for(int i=0;i<=2;p=p+2,i++) p->display(); return 0; 【第 3 页 共 48 页】 } 6.建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。初值自拟。 #include Student(int n,float s):num(n),score(s){} int num; float score; }; void main() {Student stud[5]={ Student(101,78.5),Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,95.5)}; void max(Student* ); Student *p=&stud[0]; max(p); } void max(Student *arr) {float max_score=arr[0].score; int k=0; for(int i=1;i<5;i++) if(arr[i].score>max_score) {max_score=arr[i].score;k=i;} cout< 7.用new建立一个动态一维数组,并初始化int[10]={1,2,3,4,5,6,7,8,9,10},用指针输出,最后销毁数组所占空间。 #include p=new int[10]; for(int i=1;i<=10;i++) { *(p+i-1)=i; cout<<*(p+i-1)<<\ } cout< 【第 4 页 共 48 页】 } 8.定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。编写程序,求两个复数之和。初值自拟。 #include 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; double imag; }; double Complex::get_real() {return real;} double Complex::get_imag() {return imag;} void Complex::display() {cout<<\ 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; cout<<\ c3.display(); return 0; } 9.定义一个复数类Complex,重载运算符“+”,“—”,使之能用于复数的加,减运算,运算符重载函数作为Complex类的成员函数。编程序,分别求出两个复数之和,差。初值自拟。 using namespace std; class Complex {public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} 【第 5 页 共 48 页】