当前位置:网站首页>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;
}
原网站

版权声明
本文为[Sy_ Faker]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206222259539430.html