C++ 程序设计试卷
{ int x=0,y=5,z=3; while(z>0 && ++x<3) y=y-1;
cout< 4、 #include cout<<\x++; cout<<\y++; cout<<\} 5、 #include 6、 #include a=new int(x); cout<<\} }; void main() { A x(3),*p; p=new A(5); delete p; } 第16页共29页 C++ 程序设计试卷 7、 # include virtual int func () { return 0; }}; class derived: public base { public:int func() { return 100; } }; void main() { derived d; base& b = d; cout << b.func() << endl; cout << b.base::func() << endl; } 8、 #include float s1,s2,c1,c2,r1; c1=pi*r*r; s1=2*pi*r; r=2.8; c2=pi*r*r; s2=2*pi*r; cout 1) #define pi=3.1416; 应改为 #define pi 3.1416 2) 修改了 const 类型的变量 r 应改为 static float r=3.2; 3) cout 九、编程题: 6、设计一个汽车类 Vehicle ,包含数据成员车轮数和车重,由它派生出类 前者包含载客数,后者包含载重量。编写程序实现。 #include protected: 定义汽车类 Car 和类 Truck , 第17页共29页 C++ 程序设计试卷 int wheels; // float weight; // public: 车轮数 重量 vehicle(int wheels,float weight); int get_wheels(); float get_weight(); float wheel_load(); void show(); }; class car:public vehicle // { int passenger_load; // public: car(int wheels,float weight,int passengers=4); int get_passengers(); void show(); }; class truck:public vehicle // { int passenger_load; // float payload; // public: truck(int wheels,float weight,int passengers=2,float max_load=24000.00); 载人数 载重量 定义卡车类 载人数 定义小车类 int get_passengers(); float efficiency(); void show(); }; vehicle::vehicle(int wheels,float weight) { 第18页共29页 C++ 程序设计试卷 vehicle::wheels=wheels; vehicle::weight=weight; } int vehicle::get_wheels() { return wheels; } float vehicle::get_weight() { return weight/wheels; } void vehicle::show() { cout << \ 车轮 :\ 个 \公斤 \ cout << \ 重量 :\ } car::car(int wheels, float weight, int passengers) :vehicle (wheels, weight) { passenger_load=passengers; } int car::get_passengers () { return passenger_load; } void car::show() { cout <<\ 车型 : 小车 \ vehicle::show(); 第19页共29页 C++ 程序设计试卷 cout << \ 载人 :\ 人 \ cout << endl; } truck:: truck(int wheels, float weight,int passengers, float max_load):vehicle(wheels,weight) { passenger_load=passengers; payload=max_load; } int truck::get_passengers() { return passenger_load; } float truck::efficiency() { return payload/(payload+weight); } void truck::show() { cout <<\ 车型 : 卡车 \vehicle:: show (); cout << \ 载人 :\ cout << \ 效率 :\cout << endl; } void main () { car car1(4,2000,5); truck tru1(10,8000,3,340000); 人 \ 第20页共29页