C++程序设计考试题及答案(共8页).doc
精选优质文档-倾情为你奉上C+程序设计1、声明个CPU类,包含等级(rank),频率(frequency),电压(voltage)属性,有两个公有成员函数run(函数功能:输出“cpu正在运行”),stop(函数功能:输出“cpu停止运行”)。其中rank为枚举型CPU_Rank,声明为enum CPU_RankP1=1,P2,P3,P4,P5,P6,P7,frequency为单位是MHz的整型数,voltage为double型电压值。编写构造函数和析构函数,在主函数中显示它们的调用顺序。2、 定义复数类COMPLEX,并用成员函数为复数类COMPLEX重载加、减及赋值运算符操作。3、写一个程序,定义一个抽象类Shape,由它派生3个类:Square(正方形)、Trapezoid(梯形)和Triangle三角形。用虚函数分别计算几种图形面积、并求它们的和。要求用基类指针数组,使它每一个元素指向一个派生类对象。4、已知交通工具类定义如下。要求:(1)实现这个类;(2)定义并实现一个小车类car,是它的公有派生类,小车本身的私有属性有载人数,小车的函数有init(设置车轮数,重量和载人数),getpassenger(获取载人数),print(打印车轮数,重量和载人数)。5、声明一个车(vehicle)基类,具有maxspeed、weight成员变量,run、stop成员函数(简单输出提示“车正在行进”,“车停止”),由此派生出自行车类(bicycle)、汽车类(motorcar)。自行车类有高度(height)属性,汽车(motorcar) 类有座位数(seatnum)。从bicycle和motorcar派生出摩托车类(motorcycle),在继承过程中,注意把vehicle 设置为虚基类,同时编写各个类的构造函数和析构函数,在主函数中建立各个类对象,观察执行情况。6、定义类X、Y、Z,函数h(X *),满足:类X有私有成员i,Y的成员函数g(X *)是X的友元函数,实现对X的成员i加1;类Z是类X的友元类,其成员函数f(X *)实现对X的成员i加5;函数h(X *)是X的友元函数,实现对X的成员i加10。在一个文件中定义和实现类,在另一个文件中实现main()函数。提示:按Y、Z、X顺序定义类,在Y、Z类前 前向声明X , X * x表示X类对象指针x,访问x对象数据成员i变量的形式为 x->i。答案1、#include <iostream.h>enum CPU_Rank P1=1,P2,P3,P4,P5,P6,P7;class CPUprivate: CPU_Rank rank; int frequency; float voltage;public: void run(); void stop(); CPU(CPU_Rank r,int f,float v); CPU(); CPU(CPU &c); ;void CPU:run() cout<<"CPU正在运行"<<endl; void CPU:stop() cout<<"CPU停止运行"<<endl;(1分) CPU:CPU(CPU_Rank r,int f,float v) rank=r; frequency=f;voltage=v;cout<<"构造了一个CPU:"<<endl; CPU:CPU() cout<<"析构了一个CPU:"<<endl; void main()CPU a(P1,300,2.8); a.run(); a.stop();2、class COMPLEX public:COMPLEX(double r = 0, double i = 0); COMPLEX operator +(const COMPLEX& other); COMPLEX operator -(const COMPLEX& other); COMPLEX operator =(const COMPLEX& other); private:double real, image;/ 复数的实部与虚部; COMPLEX:COMPLEX(double r, double i)real=r;image=I;COMPLEX:COMPLEX operator +(const COMPLEX& other) COMPLEX temp; temp.real=real+other.real;temp.image=image+other.image;return tempCOMPLEX:COMPLEX operator -(const COMPLEX& other) COMPLEX temp; temp.real=real-other.real;temp.image=image-other.image;return tempCOMPLEX:COMPLEX operator =(const COMPLEX& other) real=other.real;image=other.image;return *this;3、class vehicleprotected:int wheels;/车轮数float weight;/重量public:void init(int wheels,float weight);int get_wheels();float get_weight();void print();void vehicle:init(int wheels,float weight)this->wheels=wheels;this->weight=weight;cout<<wheels<<endl;int vehicle:get_wheels()return wheels;float vehicle:get_weight()return weight;void vehicle:print()cout<<"车轮数:"<<wheels<<","<<"重量:"<<weight<<endl;class car:public vehicleprivate:int passengers;public:void init(int wheels,float weight,int pass);int getpassenger();void print();;void car:init(int wheels,float weight,int pass)vehicle:init(wheels,weight);passengers=pass;int car:getpassenger()return passengers;void car:print()vehicle:print();cout<<"可载人数:"<<passengers<<endl;4、#include <iostream.h>class Shapepublic:virtual double area()const=0;class Square:public Shapepublic:Square(double s):side(s)double area() constreturn side*side;private:double side;class Trapezoid:public Shapepublic:Trapezoid(double i,double j,double k):a(i),b(j),h(k)double area() constreturn (a+b)*h/2);private:double a,b,h;class Triangle:public Shapepublic:Triangle(double i,double j):w(i),h(j)double area() constreturn(w*h/2);private:double w,h;void main()Shape *p5;Square se(5);Trapezoid td(2,5,4);Triangle te(5,8);p0=&se;p1=&td;p2=&te;double da=0;for(int i=0;i<3;i+)da+=pi->area();cout<<"总面积是:"<<da<<endl;5、#include <iostream.h>class vehicleprivate: int maxspeed; int weight;public: vehicle(int m,int w) maxspeed=m; weight=w; cout<<"vehicle构造函数"<<endl; vehicle() cout<<"vehicle析构函数"<<endl; void run() cout<<"车在行进中"<<endl; void stop() cout<<"车停止"<<endl;class bicycle:virtual public vehicleprivate: double height;public:bicycle(int m,int w,double h):vehicle(m,w)height=h;cout<<"bicycle构造函数"<<endl;bicycle()cout<<"bicycle析构函数"<<endl;class motorcar:virtual public vehicleprivate: int seatnum;public:motorcar(int m,int w,int s):vehicle(m,w)seatnum=s;cout<<"motorcar构造函数"<<endl;motorcar()cout<<"motorcar析构函数"<<endl;class motorcycle:public bicycle,public motorcarpublic: motorcycle(int m,int w,double h,int s):bicycle(m,w,h),motorcar(m,w,s),vehicle(m,w) cout<<"motorcycle构造函数"<<endl; motorcycle() cout<<"motorcycle析构函数"<<endl;void main()vehicle a(200,3); a.run(); a.stop(); bicycle b(50,1,1.5); b.run(); b.stop(); motorcar c(120,2,5); c.run(); c.stop(); motorcycle d(100,1,1.3,3); d.run(); d.stop();6、#include <iostream.h>class X;class Y public: void g(X* x); ;class Zpublic: void f(X* x);class Xprivate: int i; public: friend void Y:g(X*); friend class Z; friend void h(X*); X(int i1) i=i1; cout<<"在X类中i的值是:"<<i<<endl;void Y:g(X *x)x->i+;cout<<"在Y类中i的值是:"<<x->i<<endl;void Z:f(X *x)x->i+=5;cout<<"在Z类中i的值是:"<<x->i<<endl;void h(X *x)x->i+=10;cout<<"在函数h中i的值是:"<<x->i<<endl;int main()X x(1); Y y; Z z; y.g(&x); z.f(&x); h(&x);专心-专注-专业