这是一段使用C语言编写的代码,用于模拟公交卡系统的基本功能。
``` #include <stdio.h> #include <stdlib.h>
// 定义公交卡结构体 typedef struct { char type[10]; // 公交卡种类:普通卡、学生卡、老年卡 float balance; // 余额 float discount; // 折扣率 } boardingcard;
// 公交卡类 class Card { public: // 构造函数 Card(char *t, float b, float d) { strcpy(type, t); balance = b; discount = d; }
// 充值函数 void recharge(float r) { balance += r; }
// 余额查询函数 float check_balance() { return balance; }
// 刷卡消费函数 void consume(float c) { balance -= c * (1 - discount); }
// 显示刷卡种类的虚函数 virtual void show_type() = 0;
// 设置折扣率的虚函数 virtual void set_discount(float d) = 0;
protected: char type[10]; // 公交卡种类:普通卡、学生卡、老年卡 float balance; // 余额 float discount; // 折扣率 };
// 普通卡类,从公交卡类派生而来 class Acard : pub