虚函数–交通工具
【问题描述】
定义一个交通工具vehicle,将他作为基类派生校车类car,卡车类truck和轮船类boat,定义类并定义虚函数来显示各类信息
【输入形式】
【输出形式】
【样例输入】
【样例输出】
【样例说明】
【评分标准】
#include <iostream> using namespace std; class vehicle { protected: double speed; //速度,公里/小时 int wheels; //轮子数 double weight; //重量 public: vehicle(double speed=80,int wheels=4,double weight=1000); virtual void show(void)=0; }; vehicle::vehicle(double speed , int wheels , double weight ) { this->speed = speed; this->wheels = wheels; this->weight = weight; } void vehicle::show() { cout << "Vehicle message\n"; cout << speed << " " << wheels << " " << weight << endl; } class car : public vehicle { int passenger_load; public: car(double speed=80,int wheels=4,double weight=1000,int passenger_load=4); virtual void show(void); }; void car::show(void) {cout << "Car message\n"; cout<<speed<<" "<<wheels<<" "<<weight<<" "<<passenger_load<<endl; } class truck : public vehicle { double rated_load; //额定载重 public: truck(double speed=80,int wheels=4,double weight=2500,double rated_load=3000); virtual void show(void); }; car::car(double speed , int wheels , double weight ,int passenger_load) { this->speed = speed; this->wheels = wheels; this->weight = weight; this->passenger_load = passenger_load; } void truck::show() { cout << "truck message\n"; cout << speed << " " << wheels << " " << weight << " " << rated_load<<endl; } truck::truck(double speed, int wheels, double weight, double rated_load) { this->speed = speed; this->wheels = wheels; this->weight = weight; this->rated_load = rated_load; } class boat : public vehicle { char kind; //轮船类别,如客轮为'k' public: boat(double speed=30,int wheels=0,double weight=12000,char kind='k'); virtual void show(void); }; void boat::show() { cout << "boat message\n"; cout << speed <<" "<< wheels <<" "<< weight <<" "<< kind<<endl; } boat::boat(double speed, int wheels, double weight, char kind) { this->speed = speed; this->wheels = wheels; this->weight = weight; this->kind = kind; } int main() { vehicle *unicycle; car *BMW; unicycle = new car; unicycle ->show(); BMW= (car *) unicycle; BMW ->show(); delete unicycle; unicycle = new truck; unicycle ->show(); delete unicycle; unicycle = new boat; unicycle ->show(); delete unicycle; return 0; }
2. 求员工工资
【问题描述】
设公司有普通员工和经理两类人员。主函数已给出,编程设计普通员工类CommonWorker和经理类Manager,经理类除具有普通员工类的属性外。普通员工类的收入由基本工资和奖金构成,经理类的收入除基本工资和奖金外,还包括职务津贴。实现求两类员工工资的Pay函数。
【输入形式】
【输出形式】
【样例输入】
【样例输出】
【样例说明】
【评分标准】
#include <iostream> #include<iostream> using namespace std; class CommonWorker { public: virtual void Pay() { earning = wage + reward; cout << earning<<endl; } friend istream& operator>>(istream& inStream, CommonWorker& data) { inStream >> data.wage; inStream >> data.reward; return inStream; } protected: double wage; double reward; double earning; }; class Manager :public CommonWorker { public: virtual void Pay() { earning = wage + reward + place; cout << earning<<endl; } friend istream& operator>>(istream& instream, Manager& data) { instream >> data.wage; instream >> data.reward; instream >> data.place; return instream; } protected: double place; }; int main() { CommonWorker c1, * pc; Manager m1; //分别输入员工的基本工资、奖金和津贴 cin >> c1 >> m1; pc = &c1; pc->Pay(); //调用Pay()函数输出该普通员工工资,如:5000 pc = &m1; pc->Pay(); //调用Pay()函数输出该经理工资,如:8000 return 0; }
3. 求图形周长和面积
【问题描述】
根据给出的程序片段编程,使程序正确运行。由基类Shape派生出圆类Circle(数据成员:半径)、正方形类Square(数据成员:边长)、三角形类Triangle(数据成员:三条边边长),3个派生类都有输入和显示信息函数Input、Output,计算面积的函数Area,计算周长的函数Perim。【输入形式】
【输出形式】
【样例输入】
【样例输出】
【样例说明】
【评分标准】
#include <iostream> #include<iomanip> #include <cmath> using namespace std; const double PI = 3.14159; //定义基类Shape class Shape { public: virtual double Area() const { return 0; } virtual double Perim() const { return 0; } virtual void Input() {} virtual void Output() { } static void SetFormat() { cout << setiosflags(ios::fixed) << setprecision(2); } protected: Shape(){} }; class Circle:public Shape { private: double mr; public: virtual void Input() { double r; cin >> r; this->mr = r; } Circle(double r = 0) { mr = r; } virtual void Output() { cout << PI * 2 * mr << "," << PI * mr * mr<<endl; } }; class Square :public Shape { private: double ma; public: Square(double a = 0, double b = 0) { ma = a; } virtual void Input() { double a; cin >> a; this->ma = a; } virtual void Output() { cout<<ma*4<<","<<ma*ma<<endl; } }; class Triangle :public Shape { private: double ma, mb, mc; public: Triangle(double a = 0, double b = 0, double c = 0) { ma = a; mb = b; mc = c; } virtual void Input() { double a, b, c; cin >> a >> b >> c; ma = a; mb = b; mc = c; } virtual void Output() { cout << ma + mb + mc << ","; double p, s; p = (ma + mb + mc) / 2; s = sqrt(p * (p - ma) * (p - mb) * (p - mc)); cout << s<<endl; } }; int main() { Circle circle; Square square; Triangle triangle; Shape* pt[3] = { &circle,&square,&triangle }; Shape::SetFormat(); //输入各图形数据,空白字符隔开 for (int i = 0; i < 3; i++) pt[i]->Input(); //输出各图形周长和面积,周长和面积值之间逗号隔开 for (int i = 0; i < 3; i++) pt[i]->Output(); return 0; }