• 利用抽象类编写实现公交卡类售票管理程序。当输入为“老年卡”,“学生卡”,“普通卡”时,显示不同的卡类以及购票金额

    #include<iostream> using namespace std; class C { public: float card_fee; virtual void real_fee() = 0; virtual void show_the_real_fee() = 0; }; class student : public C { public: student(float fee) { card_fee = fee; } void real_fee(){ card_fee *= 0.5; }//计算学生卡的实际费用 void show_the_real_fee(){ cout << "学生卡实际的费用是:" << card_fee<<"元"<<endl; } }; class older : public C { public: older(float fee){ card_fee = fee; } void real_fee(){ card_fee *= 0.6; }//计算老人卡的实际费用 void show_the_real_fee(){ cout << "老人卡实际的费用是:" << card_fee << "元" << endl; } }; class normal : public C { public: normal(float fee){ card_fee = fee; } void real_fee(){ card_fee *= 0.95; }//计算一般卡的实际费用 void show_the_real_fee(){ cout << "一般市民卡实际的费用是:" << card_fee << "元" << endl; } }; void main() { cout << "请输入公交费原价:"; float fee; cin >> fee; cout <<"公交费原价为"<<fee<<"元"<< endl; cout << "请输入刷卡人群性质" << endl << "学生卡:请输 1;" << endl << "老人卡:请输 2;" << endl << "普通卡:请输 3;" << endl; int man; cin >> man;//2.定义了一个变量,用于了解用户刷的卡的种类,以便进行相应的计算 C *p_card;//3.定义了一个父类指针,为了方便对子类的操作(即:该指针指向"谁"时,用这个指针调用的函数就是"谁"的函数,因为子类的函数的名子都一样,函数里的内容不一样,调用不同子类的函数后,计算的结果就不同) switch (man)//4.根据用户的输入,开启相应的功能 { case 1: {student stu_card(fee); p_card = &stu_card; p_card->real_fee(); p_card->show_the_real_fee();}break; case 2: {older old_card(fee); p_card = &old_card; p_card->real_fee(); p_card->show_the_real_fee();}break; case 3: {normal normal_card(fee); p_card = &normal_card; p_card->real_fee(); p_card->show_the_real_fee();}break; default:cout <<"输入错误!"<< endl;//5.如果用户输入的不是1,2,3,而是其他字符,则报错 } getchar();getchar(); }

    0
    1049
    2KB
    2014-12-08
    25
上传资源赚积分or赚钱