当前位置:网站首页>Pta:7-31 journal charges
Pta:7-31 journal charges
2022-06-23 04:37:00 【Sy_ Faker】
The post office offers subscriptions to two periodicals : Magazines and newspapers . Give the framework of the following base class :
class Periodical {
protected:
string title; // name
public:
virtual void display()=0;// Print charges
}
With Periodical Base class , structure Magazine and Newspaper class .
Generate the above class and write the main function , The main function is required to have a base class Periodical Pointer array , Array elements do not exceed 10 individual .
Periodical *pp[10];
The main function is based on the input information , Corresponding establishment Magazine, Newspaper Class object , about Magazine Give the number of subscription periods and the price of each period , about Newspaper Give the number of subscription weeks , Number of publications per week and price per copy .
Input format : One line per test case , The first item is type ,1 by Magazine,2 by Newspaper, The second item is the name , The third item is the unit price ,Magazine The fourth item of is the number of periods ,Newspaper The fourth item is the number of subscription weeks , The fifth item is the number of publications per week .
When the output , Print the name and charge of each journal in turn ( Keep one place after the decimal point ).
sample input :
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
sample output :
AAA 76.8
BB 45.0
CCCC 100.8
DD 269.5
EEE 54.0
#include<iostream>
#include<cstring>
#include<iomanip>
using namespace std;
class Periodical
{
protected:
string title;
public:
virtual void display()=0;// Pure virtual function
};
class Magazine:public Periodical
{
int num;
double price;
public:
Magazine(string p,double a,int b)
{
title=p;// Assignment between strings , The same below
price=a;
num=b;
}
virtual void display()// Output , The practice of keeping one decimal place , The same below
{
cout<<setiosflags(ios::fixed)<<title<<setprecision(1)<<" "<<num*price<<endl;
}
};
class Newspaper:public Periodical
{
int num;
int times;
double price;
public:
Newspaper(string p,double a,int b,int t)
{
times=t;
num=b;
price=a;
title=p;
}
virtual void display()
{
cout<<setiosflags(ios::fixed)<<title<<" "<<setprecision(1)<<num*price*times<<endl;
}
};
int main()
{
Periodical *pp[10];// Create a base class pointer array
int k,b,t,count=0;
string str;
double a;
cin>>k;
getchar();// Remember to return " eliminate "
while(k!=0)
{
switch(k)// use switch To determine which category it belongs to
{
case 1:
cin>>str>>a>>b;
pp[count]=new Magazine(str,a,b);// Initialize after derivation
break;
case 2:
cin>>str>>a>>b>>t;
pp[count]=new Newspaper(str,a,b,t);
break;
}
pp[count]->display();// Directly call to output
cin>>k;
getchar();
count++;// Use next pointer
}
return 0;
}
边栏推荐
猜你喜欢

Xiaojinwei, chairman of Chenglian Technology: implement the national strategy of data economy and lead the development of new consumption in the digital era!

最新编程语言排行榜

【多模态】UNIMO

Online JSON to CSharp (c) class tool

Mobile terminal city list sorting JS plug-in vertitylist js

【二叉树进阶】AVLTree - 平衡二叉搜索树
![3D数学基础[十六] 匀加速直线运动的公式](/img/51/5b05694bbd0f4fd01dd26cf55b22c7.png)
3D数学基础[十六] 匀加速直线运动的公式

8 key indicators to measure technology debt in 2022

Leetcode 1208. 尽可能使字符串相等(终于解决,晚安)

一篇文章学会er图绘制
随机推荐
If you want to understand PostgreSQL, you must first brush the architecture
Basic skills of x64dbg
Prince language under insect date category
[从零开始学习FPGA编程-40]:进阶篇 - 设计-竞争与风险Risk或冒险
[deep learning] deep learning reasoning framework tensorrt MNN openvino onnxruntime
Pytoch --- pytoch customizes the dataset
Halcon知识:binocular_disparity 知识
靜態查找錶和靜態查找錶
Monitoring artifact ZABBIX, from deployment to application, goes deep layer by layer
A summary of PostgreSQL data types. All the people are here
2022年的软件开发:首席信息官应该知道的五个现实
【二叉树】二叉树的完全性检验
PTA:7-67 友元很简单2016final
② cocoapods原理及 PodSpec 文件上传操作
Similar to RZ / SZ, trzsz supporting TMUX has released a new version
静态查找表和静态查找表
OpenJudge NOI 1.13 49:计算对数
How does flutter achieve different zoom animation effects
PTA:7-87 集合的模拟实现(类模板)
P1347 sorting (TOPO)