简体   繁体   English

如何将此代码拆分为两个不同的文件,header 和 .cpp?

[英]How can I split this code into two different files, header and .cpp?

How can I split this into two different files, such as a header file and .cpp, while keeping the same output?如何将其拆分为两个不同的文件,例如头文件和 .cpp,同时保持相同的输出?

#include <iostream>
using namespace std;
    
class Package // base class
{
private:
    string nameSender, addressSender, citySender, stateSender, ZIPSender,nameRecipient, addressRecipient, cityRecipient, stateRecipient, ZIPRecipient;
    double weight,cost;
    
public:
    //constructor
    Package(string nameSender, string addressSender, string citySender, string stateSender, string ZIPSender,string nameRecipient, string addressRecipient, string cityRecipient, string stateRecipient, string ZIPRecipient,double weight,double cost)
    {
        this->nameSender = nameSender;
        this->addressSender = addressSender;
        this->citySender = citySender;
        this->stateSender = stateSender;
        this->ZIPSender = ZIPSender;
        this->nameRecipient = nameRecipient;
        this->addressRecipient = addressRecipient;
        this->cityRecipient = cityRecipient;
        this->stateRecipient = stateRecipient;
        this->ZIPRecipient = ZIPRecipient;
        if(weight > 0)
            this->weight = weight;
        if(cost > 0)
            this->cost = cost;
    }

    //get methods
    double getWeight()
    {
        return weight;
    }

    double getCost()
    {
        return cost;
    }

    double calculateCost()
    {
        return weight*cost;
    }
    
    void display()
    {
        cout<<"\nName of sender : "<<nameSender;
        cout<<"\nAddress : "<<addressSender;
        cout<<"\nCity : "<<citySender;
        cout<<"\nState : "<<stateSender;
        cout<<"\nZIP : "<<ZIPSender;
        cout<<"\nNameof Recipient : "<<nameRecipient;
        cout<<"\nAddress : "<<addressRecipient;
        cout<<"\nCity : "<<cityRecipient;
        cout<<"\nState : "<<stateRecipient;
        cout<<"\nZIP : "<<ZIPRecipient;
        cout<<"\nWeight of Package : "<<weight;
        cout<<"\nCost per ounce : "<<cost;
    }
};

class TwoDayPackage : public Package // derived class
{
private :
    double flatFee;
    
public:
    //sending arguments to base class constructor
    TwoDayPackage(string nameSender, string addressSender, string citySender, string stateSender, string ZIPSender,string nameRecipient, string addressRecipient, string cityRecipient, string stateRecipient, string ZIPRecipient,double weight,double cost,double flatFee):Package(nameSender,addressSender,citySender,stateSender,ZIPSender,nameRecipient,addressRecipient,cityRecipient,stateRecipient,ZIPRecipient,weight,cost)
    {
        this->flatFee = flatFee;
    }
    
    double calculateCost()
    {
        return flatFee + getWeight()*getCost();
    }
    
    void displayInfo()
    {
        cout<<"\nTwo Day Package ";
        display();
        cout<<"\nFlat Fee : "<<flatFee;
    }
};
    
class OverNightPackage : public Package
{
private :
    double additionalFee;
    
public:
    OverNightPackage(string nameSender, string addressSender, string citySender, string stateSender, string ZIPSender,string nameRecipient, string addressRecipient, string cityRecipient, string stateRecipient, string ZIPRecipient,double weight,double cost,double additionalFee):Package(nameSender,addressSender,citySender,stateSender,ZIPSender,nameRecipient,addressRecipient,cityRecipient,stateRecipient,ZIPRecipient,weight,cost)
    {
        this->additionalFee = additionalFee;
    }
    
    double calculateCost()
    {
        return additionalFee + getWeight()*getCost();
    }
    
    void displayInfo()
    {
        cout<<"\nOvernight Package ";
        display();
        cout<<"\nAdditional Fee : "<<additionalFee;
    }    
};

int main() {
    
    TwoDayPackage p1("John","234,New Street","ALBANY", "NY" ,"12261-0001","Smith","307"," Trenton", "NJ"," 08625-0307",4.5,12.5,15.5);
    
    p1.displayInfo();
    
    cout<<"\nTotal Cost : "<<p1.calculateCost();
    
    OverNightPackage p2("Candy","234,New Street","ALBANY", "NY" ,"12261-0001","Nancy","307"," Trenton", "NJ"," 08625-0307",4.8,17.5,23.5);
    
    p2.displayInfo();
    
    cout<<"\nTotal Cost : "<<p2.calculateCost();
    return 0;
}

Here's an example.这是一个例子。
package.hpp包.hpp

#include <string>  

class Package // base class
{
 private:
    std::string m_nameSender;
    std::string m_addressSender;
    std::string m_citySender;
    std::string m_stateSender;
    std::string m_ZIPSender;
    std::string m_nameRecipient;
    std::string m_addressRecipient;
    std::string m_cityRecipient;
    std::string m_stateRecipient;
    std::string m_ZIPRecipient;
    double m_weight;
    double m_cost;

public:
    //constructor
    Package(const std::string&  nameSender,
            const std::string&  addressSender,
            const std::string&  citySender,
            const std::string&  stateSender,
            const std::string&  ZIPSender,
            const std::string&  nameRecipient,
            const std::string&  addressRecipient,
            const std::string&  cityRecipient,
            const std::string&  stateRecipient,
            const std::string&  ZIPRecipient,
       //get methods
        double getWeight() const;
        double getCost() const;
        double calculateCost() const;
    
        void display() const;
};

package.cpp包.cpp

#include "package.hpp"
#include <iostream>

using std::cout;  
using std::string;

Package :: Package(const string&  nameSender,
                   const string&  addressSender,
                   const string&  citySender,
                   const string&  stateSender,
                   const string&  ZIPSender,
                   const string&  nameRecipient,
                   const string&  addressRecipient,
                   const string&  cityRecipient,
                   const string&  stateRecipient,
                   const string&  ZIPRecipient,
                   double         weight,
                   double         cost)
: m_nameSender(nameSender),
  m_addressSender(addressSender),
  m_citySender(citySender),
  m_stateSender(stateSender),
  m_nameRecipient(nameRecipient),
  m_addressRecipient(addressRecipient),
  m_cityRecipient(cityRecipient),
  m_ZIPRecipient(ZIPRecipient),
  m_weight(weight),
  m_cost(cost)
{
}

//get methods
double Package::getWeight()
{
    return m_weight;
}
double Package::getCost()
{
    return m_cost;
}
double Package::calculateCost()
{
    return m_weight*cost;
}

void Package::display()
{
    cout << "\nName of sender : " << m_nameSender;
    cout << "\nAddress : " << m_addressSender;
    cout << "\nCity : " << m_citySender;
    cout << "\nState : " << m_stateSender;
    cout << "\nZIP : " << m_ZIPSender;
    cout << "\nNameof Recipient : " << m_nameRecipient;
    cout << "\nAddress : " << m_addressRecipient;
    cout << "\nCity : " << m_cityRecipient;
    cout << "\nState : " << m_stateRecipient;
    cout << "\nZIP : " << m_ZIPRecipient;
    cout << "\nWeight of Package : " << m_weight;
    cout << "\nCost per ounce : " << m_cost;
}

I took liberties to show the "m_prefix", and an initialization list.我随意地展示了“m_prefix”和一个初始化列表。
I changed the constructor to pass the strings by constant reference.我更改了构造函数以通过常量引用传递字符串。

I also applied the common coding guideline of one variable declaration per line and one parameter per line (when there are many parameters).我还应用了每行一个变量声明和每行一个参数的通用编码指南(当有很多参数时)。 These make the code easier to maintain and easier to read.这些使代码更易于维护和阅读。

Edit 1: Add #include files to header file.编辑 1:将 #include 文件添加到头文件。
Edit 2: Changed string to std::string in header file.编辑2:改stringstd::string的头文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM