我是一名初学者,正在制作一个程序,使用继承打印出三种运输方式。我已经完成了所有的计算,但现在我只想弄清楚如何将它们全部打印出来。如果有人可以建议可以打印输入的内容,同时打印出每个运输选项会花费多少的费用。
// FDHPackage: a program that creates 3 package classes that calculate the
//shipping costs based on the weight and the type of package it is (regular, two day, or overnight.)
#include <string> // C++ standard string class
#include "stdafx.h"
#include <iostream>
using namespace std;
class Package // Base Class
{
private: // data members
string infoS, infoR; //Sender and Recpient information
double weight; // in ounce
double cost; // per ounce
public: //member functions
void setInfoS(string infoS);
void setInfoR(string infoR);
string getInfoS()
{
return infoS;
}
string getInfoR()
{
return infoR;
}
double calculateCost(double weight, double costPerOunce) //function that calculate the cost z
{
double zz; //total cost = weight*cost per ounce
zz = weight * costPerOunce; //the cost zz
cout << "The Regular Cost = " << zz << endl << endl;
return zz;
}
}; // end class Package
void Package::setInfoS(string senInfo)
{
infoS = senInfo;
}
void Package::setInfoR(string recInfo)
{
infoR = recInfo;
}
class TwoDayShipping : public Package // derived class
{
public:
double calcShippingCost(double weight, double costPerOunce, double regularFee)
/* function that calculate shipping cost by adding the flat fee to the weight-based cost
calculated by base class Package’s calculateCost function*/
{
double z; //shippingcost of Two day Package class
z = calculateCost(weight, costPerOunce) + regularFee;
cout << "The TwoDayPackage Cost = " << z << endl;
return z;
}
private:
double regularFee;
}; // end TwoDayShipping
class OvernightShipping : public Package //derived class that adds the additional fee per ounce
{
public:
double calcCostOvernight(double weight, double costPerOunce, double additionalCost)
{
double cc; //shippingcost of overnight class
cc = calculateCost(weight, costPerOunce) + (additionalCost * weight);
cout << "The OvernightShipping Cost = " << cc << endl;
return cc;
}
private:
double overnightCost; //per ounce
}; // end class OvernightShipping
int main()
{
string customerInfo;
float packageWeight;
double costPerOunce;
double regularFee;
double additionalCost;
Package base; //the object base of the package class
TwoDayShipping twoday; //the object twoday of the first inherited class
OvernightShipping overnight; //the object overnight of the second inherited calss
cout << " *-*-Welcome to United Postal Services-*-*" << endl << endl;
cout << "Please Enter Required Data: " << endl;;
cout << "Enter Weight" << endl;
cin >> packageWeight;
cout << endl;
cout << "Enter Cost Per Ounce" << endl;
cin >> costPerOunce;
cout << endl;
cout << "Regular rate would cost: ";
}