您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > C++程序设计考试题及答案
C++程序设计1、声明个CPU类,包含等级(rank),频率(frequency),电压(voltage)属性,有两个公有成员函数run(函数功能:输出“cpu正在运行”),stop(函数功能:输出“cpu停止运行”)。其中rank为枚举型CPU_Rank,声明为enumCPU_Rank{P1=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、#includeiostream.henumCPU_Rank{P1=1,P2,P3,P4,P5,P6,P7};classCPU{private:CPU_Rankrank;intfrequency;floatvoltage;public:voidrun();voidstop();CPU(CPU_Rankr,intf,floatv);~CPU();CPU(CPU&c);};voidCPU::run(){coutCPU正在运行endl;}voidCPU::stop(){coutCPU停止运行endl;}(1分)CPU::CPU(CPU_Rankr,intf,floatv){rank=r;frequency=f;voltage=v;cout构造了一个CPU:endl;}CPU::~CPU(){cout析构了一个CPU:endl;}voidmain(){CPUa(P1,300,2.8);a.run();a.stop();}2、classCOMPLEX{public:COMPLEX(doubler=0,doublei=0);COMPLEXoperator+(constCOMPLEX&other);COMPLEXoperator-(constCOMPLEX&other);COMPLEXoperator=(constCOMPLEX&other);private:doublereal,image;//复数的实部与虚部};COMPLEX::COMPLEX(doubler,doublei){real=r;image=I;}COMPLEX::COMPLEXoperator+(constCOMPLEX&other){COMPLEXtemp;temp.real=real+other.real;temp.image=image+other.image;returntemp}COMPLEX::COMPLEXoperator-(constCOMPLEX&other){COMPLEXtemp;temp.real=real-other.real;temp.image=image-other.image;returntemp}COMPLEX::COMPLEXoperator=(constCOMPLEX&other){real=other.real;image=other.image;return*this;}3、classvehicle{protected:intwheels;//车轮数floatweight;//重量public:voidinit(intwheels,floatweight);intget_wheels();floatget_weight();voidprint();};voidvehicle::init(intwheels,floatweight){this-wheels=wheels;this-weight=weight;coutwheelsendl;}intvehicle::get_wheels(){returnwheels;}floatvehicle::get_weight(){returnweight;}voidvehicle::print(){cout车轮数:wheels,重量:weightendl;}classcar:publicvehicle{private:intpassengers;public:voidinit(intwheels,floatweight,intpass);intgetpassenger();voidprint();};voidcar::init(intwheels,floatweight,intpass){vehicle::init(wheels,weight);passengers=pass;}intcar::getpassenger(){returnpassengers;}voidcar::print(){vehicle::print();cout可载人数:passengersendl;}4、#includeiostream.hclassShape{public:virtualdoublearea()const=0;};classSquare:publicShape{public:Square(doubles):side(s){}doublearea()const{returnside*side;}private:doubleside;};classTrapezoid:publicShape{public:Trapezoid(doublei,doublej,doublek):a(i),b(j),h(k){}doublearea()const{return((a+b)*h/2);}private:doublea,b,h;};classTriangle:publicShape{public:Triangle(doublei,doublej):w(i),h(j){}doublearea()const{return(w*h/2);}private:doublew,h;};voidmain(){Shape*p[5];Squarese(5);Trapezoidtd(2,5,4);Trianglete(5,8);p[0]=&se;p[1]=&td;p[2]=&te;doubleda=0;for(inti=0;i3;i++){da+=p[i]-area();}cout总面积是:daendl;}5、#includeiostream.hclassvehicle{private:intmaxspeed;intweight;public:vehicle(intm,intw){maxspeed=m;weight=w;coutvehicle构造函数endl;}~vehicle(){coutvehicle析构函数endl;}voidrun(){cout车在行进中……endl;}voidstop(){cout车停止endl;}};classbicycle:virtualpublicvehicle{private:doubleheight;public:bicycle(intm,intw,doubleh):vehicle(m,w){height=h;coutbicycle构造函数endl;}~bicycle(){coutbicycle析构函数endl;}};classmotorcar:virtualpublicvehicle{private:intseatnum;public:motorcar(intm,intw,ints):vehicle(m,w){seatnum=s;coutmotorcar构造函数endl;}~motorcar(){coutmotorcar析构函数endl;}};classmotorcycle:publicbicycle,publicmotorcar{public:motorcycle(intm,intw,doubleh,ints):bicycle(m,w,h),motorcar(m,w,s),vehicle(m,w){coutmotorcycle构造函数endl;}~motorcycle(){coutmotorcycle析构函数endl;}};voidmain(){vehiclea(200,3);a.run();a.stop();bicycleb(50,1,1.5);b.run();b.stop();motorcarc(120,2,5);c.run();c.stop();motorcycled(100,1,1.3,3);d.run();d.stop();}6、#includeiostream.hclassX;classY{public:voidg(X*x);};classZ{public:voidf(X*x);};classX{private:inti;public:friendvoidY::g(X*);friendclassZ;friendvoidh(X*);X(inti1){i=i1;cout在X类中i的值是:iendl;}};voidY::g(X*x){x-i++;cout在Y类中i的值是:x-iendl;}voidZ::f(X*x){x-i+=5;cout在Z类中i的值是:x-iendl;}voidh(X*x){x-i+=10;cout在函数h中i的值是:x-iendl;}intmain(){Xx(1);Yy;Zz;y.g(&x);z.f(&x);h(&x);}
本文标题:C++程序设计考试题及答案
链接地址:https://www.777doc.com/doc-5229097 .html