1、C++面向对象程序设计上机考试题库
一、第一类题目〔20道,每题7分,在word中保存代码并将输出结果窗口保存〕
1.定义盒子Box类,要求具有以下成员:长、宽、高分别为x,y,z,可设置盒子形状;可计算盒子体积;可计算盒子的外表积。
#include
2、ea() {s=2*(x*y+x*z+y*z);}
void show()
{cout<<"x= "< 5、ox2.volume()< 6、ght(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<<"The volume of box1 is "< 7、
cout<<"The volume of box2 is "< 8、 numtype max()
{return (x>y)?x:y;}
numtype min()
{return (x 9、
Compare 10、Minimum of two characters."< 11、oid Student::display()
{cout< 12、设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。初值自拟。
#include 13、
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< 14、
7. 用new建立一个动态一维数组,并初始化int[10]={1,2,3,4,5,6,7,8,9,10},用指针输出,最后销毁数组所占空间。
#include 15、数类Complex,重载运算符“+〞,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。编写程序,求两个复数之和。初值自拟。
#include 16、 double real;
double imag;
};
double Complex::get_real()
{return real;}
double Complex::get_imag()
{return imag;}
void Complex::display()
{cout<<"("< 17、et_imag()+c2.get_imag());
}
int main()
{Complex c1(3,4),c2(5,-10),c3;
c3=c1+c2;
cout<<"c3=";
c3.display();
return 0;
}
9. 定义一个复数类Complex,重载运算符“+〞,“—〞,使之能用于复数的加,减运算,运算符重载函数作为Complex类的成员函数。编程序,分别求出两个复数之和,差。初值自拟。
using namespace std;
class Complex
{public:
Complex(){real=0;imag=0;} 18、
Complex(double r,double i){real=r;imag=i;}
Complex operator+(Complex &c2);
Complex operator-(Complex &c2);
void display();
private:
double real;
double imag;
};
Complex Complex::operator+(Complex &c2)
{Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
retur 19、n c;}
Complex Complex::operator-(Complex &c2)
{Complex c;
c.real=real-c2.real;
c.imag=imag-c2.imag;
return c;}
void Complex::display()
{cout<<"("< 20、t<<"c1-c2=";
c3.display();
return 0;
}
10. 定义一个复数类Complex,重载运算符 “*〞,“/〞,使之能用于复数的乘,除。运算符重载函数作为Complex类的成员函数。编程序,分别求出两个复数之积和商。初值自拟。提示:两复数相乘的计算公式为:(a+bi)*(c+di)=(ac-bd)+(ad+bc)i。两复数相除的计算公式为:(a+bi)/(c+di)=(ac+bd)/(c*c+d*d)+(bc-ad) /(c*c+d*d)i。
#include 21、lex
{public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
Complex operator*(Complex &c2);
Complex operator/(Complex &c2);
void display();
private:
double real;
double imag;
};
Complex Complex::operator*(Complex &c2)
{Complex c;
c.real= 22、real*c2.real-imag*c2.imag;
c.imag=imag*c2.real+real*c2.imag;
return c;}
Complex Complex::operator/(Complex &c2)
{Complex c;
return c;}
void Complex::display()
{cout<<"("< 23、display();
c3=c1/c2;
cout<<"c1/c2=";
c3.display();
return 0;
}
11. 定义一个复数类Complex,重载运算符“+〞,使之能用于复数的加法运算。参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。例如:c1+c2,i+c1,c1+i均合法〔设i为整数,c1,c2为复数〕。编程序,分别求两个复数之和、整数和复数之和。初值自拟。
#include 24、0;}
Complex(double r,double i){real=r;imag=i;}
Complex operator+(Complex &c2);
Complex operator+(int &i);
friend Complex operator+(int&,Complex &);
void display();
private:
double real;
double imag;
};
Complex Complex::operator+(Complex &c)
{return Complex(real+c 25、real,imag+c.imag);}
Complex Complex::operator+(int &i)
{return Complex(real+i,imag);}
void Complex::display()
{cout<<"("< 26、1+c2;
cout<<"c1+c2=";
c3.display();
c3=i+c1;
cout<<"i+c1=";
c3.display();
c3=c1+i;
cout<<"c1+i=";
c3.display();
return 0;
}
12. 有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+〞,使之能用于矩阵相加。如c=a+b。初值自拟。
#include 27、trix();
friend Matrix operator+(Matrix &,Matrix &);
void input();
void display();
private:
int mat[2][3];
};
Matrix::Matrix() 28、
{for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
mat[i][j]=0;
}
Matrix operator+(Matrix &a,Matrix &b)
{Matrix c;
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
{c.mat[i][j]=a.mat[i][j]+b.mat[i][j];}
return c;
}
void Matrix::input() 29、
{cout<<"input value of matrix:"< 30、a,b,c;
a.input();
b.input();
cout< 31、而放在类外,作为Complex类的友元函数。初值自拟。
#include 32、 double imag;
};
Complex operator+ (Complex &c1,Complex &c2)
{
return Complex(c1.real+c2.real, c1.imag+c2.imag);
}
void Complex::display()
{cout<<"("< 33、isplay();
cout<<"c1+c2="; c3.display();
return 0;
}
14. 定义一个字符串类String,用来存放不定长的字符串,重载运算符“==〞,,用于两个字符串的等于比拟运算。初值自拟。
#include 34、oid display();
private:
char *p;
};
String::String(char *str)
{p=str;
}
void String::display()
{cout< 35、string2)
{if(operator==(string1,string2)==1)
{string1.display();cout<<"=";string2.display();}
cout< 36、am.h>
#include 37、ator<(String &string1,String &string2)
{if(strcmp(string1.p,string2.p)<0)
return true;
else
return false;
}
void compare(String &string1,String &string2)
{if(operator<(string1,string2)==1)
{string1.display();cout<<"<";string2.display();}
cout< 38、ring1("Book"),string2("Computer");
compare(string1,string2);
return 0;
}
16. 定义一个字符串类String,用来存放不定长的字符串,重载运算符">",用于两个字符串的大于的比拟运算。初值自拟。
#include 39、ng1,String &string2);
void display();
private:
char *p;
};
String::String(char *str)
{p=str;
}
void String::display()
{cout< (String &string1,String &string2)
{if(strcmp(string1.p,string2.p)>0)
return true;
else
return false;
}
void compare 40、String &string1,String &string2)
{if(operator>(string1,string2)==1)
{string1.display();cout<<">";string2.display();}
cout< 41、均成绩。数据自拟。
#include"string.h"
#include 42、core[0] = s0;
fScore[1] = s1;
fScore[2] = s2; }
void print()
{ cout<<
cout<<":"< 43、Score[2])); }
float GetAverage();
private:
float fScore[3];
};
float CStuScore::GetAverage()
{ return (float)((fScore[0] + fScore[1] + fScore[2])/3.0); }
void main()
{ CStuScore one;
float a,b,c;
char Name[12];
char StuNO[9];
cout 44、<<":";
cin>>Name;
cout<<"学号:";
cin>>StuNO;
cout<<" 成绩1:"<<" 成绩2: "<<" 成绩3: "<<"\n";
cin>>a>>b>>c;
one.SetScore(Name,StuNO,a,b,c);
one.print();
cout<<"平均成绩为 "< 45、cle类为直接基类,派生出一个Cylinder〔圆柱体〕类,在增加数据成员h〔高〕。编写程序,重载运算符“<<〞和“>>〞,使之能够用于输出以上类对象。
#include 46、t &);
protected:
float x,y;
};
Point::Point(float a,float b)
{x=a;y=b;}
void Point::setPoint(float a,float b)
{x=a;y=b;}
ostream & operator<<(ostream &output,const Point &p)
{output<<"["< 47、t x=0,float y=0,float r=0);
void setRadius(float);
float getRadius() const;
float area () const;
friend ostream &operator<<(ostream &,const Circle &);
protected:
float radius;
};
Circle::Circle(float a,float b,float r):Point(a,b),radius(r){}
void Circle::setRadius(float r)
{ 48、radius=r;}
float Circle::getRadius() const {return radius;}
float Circle::area() const
{return 3.14159*radius*radius;}
ostream &operator<<(ostream &output,const Circle &c)
{output<<"Center=["<