我走过最长的路,就是教主的套路#include <iostream> #include <string> using namespace std; class Fruit { public: double price; double weight; double income; virtual void input(){cin>>price>>weight;} virtual double total(){return income;} }; class Apple:public Fruit { int m,n; public: void input() { double a,b; cin>>a>>b; price=a; weight=b; income=price*weight*30; } virtual double total() { return income; } }; class Banana:public Fruit { int m,n; public: virtual void input() { double a,b; cin>>a>>b; price=a; weight=b; income=price*weight*25; } virtual double total() { return income; } }; class Pear:public Fruit { int m,n; public: virtual void input() { double a,b; cin>>a>>b; price=a; weight=b; income=price*weight*20; } virtual double total() { return income; } }; int main() { Fruit* fruit; string fruit_name; double sum = 0.0; int cases; cin >> cases; for(int i = 1; i <= cases; i++) { cin >> fruit_name; if(fruit_name == "Apple") fruit = new Apple(); if(fruit_name == "Banana") fruit = new Banana(); if(fruit_name == "Pear") fruit = new Pear(); fruit->input(); sum += fruit->total(); delete fruit; } cout << "Total Price : " << sum << endl; return 0; }
时间: 2024-11-18 11:34:23