7-4 期刊收费 (10分)
发布时间
阅读量:
阅读量
邮局提供两种期刊的订阅:杂志和报纸。 给出下面基类的框架:
class Periodical {
protected:
string title; //名称
public:
virtual void display()=0;//打印收费
}
以Periodical为基类,构建Magazine和Newspaper类。
生成上述类并编写主函数,要求主函数中有一个基类Periodical指针数组,数组元素不超过10个。
Periodical *pp[10];
主函数根据输入的信息,相应建立Magazine, Newspaper类对象,对于Magazine给出订阅期数和每期价格,对于Newspaper给出订阅周数,每周出版次数和每份价格。
输入格式:每个测试用例占一行,第一项为类型,1为Magazine,2为Newspaper,第二项是名称,第三项是单价,Magazine的第四项是期数,Newspaper的第四项是订阅周数,第五项是每周出版次数。
输出时,依次打印各期刊的名称和收费(小数点后保留一位)。
输入样例:
1 AAA 12.8 6
1 BB 15 3
2 CCCC 2.1 16 3
2 DD 0.7 55 7
1 EEE 18 3
0
输出样例:
AAA 76.8
BB 45.0
CCCC 100.8
DD 269.5
EEE 54.0
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
//基类Periodical
class Periodical {
public:
//构造函数
Periodical(int a, string t, float p) :amount(a), title(t), price(p) { }
virtual void display() = 0;//打印收费
protected:
int amount;//数量
string title; //名称
float price; //价格
};
//杂志类
class Magazine :public Periodical
{
public:
Magazine(int a, string t, float p) :Periodical(a, t, p) {}
//重写父类中的纯虚函数
void display()
{
//打印各期刊的名称和收费
cout << title << " " << setiosflags(ios::fixed) << setprecision(1) << price * amount << endl;
}
};
//报纸类
class Newspaper :public Periodical
{
public:
Newspaper(int a, string t, float p,int w) :Periodical(a, t, p), week(w) {}
//重写父类中的纯虚函数
void display()
{
//依次打印各期刊的名称和收费(小数点后保留一位)。
cout << title << " " << setiosflags(ios::fixed) << setprecision(1) << price * amount * week << endl;
}
private:
int week;
};
//main函数
int main()
{
Periodical* pp[10]; //基类Periodical指针数组
int type, amount, week, i = 0;
string name;
float price;
cin >> type;
while (type != 0)
{
cin >> name >> price >> amount;
if (type == 1) //杂志
{
pp[i] = new Magazine(amount,name,price);
pp[i++]->display();
}
else if(type==2) //报纸
{
cin >> week;
pp[i] = new Newspaper(amount,name,price,week);
pp[i++]->display();
}
cin >> type; //输入停止符号 0
}
return 0;
delete* pp;
}
全部评论 (0)
还没有任何评论哟~
