C++ 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. also if anything can be modified to make it closer to this output.

Package weight: 8.50

Standard Shipping Option

Sender:

Max Ryder

1000 San Diario

San Jose, CA 75555

Recipient:

Mark Smith

7 Elm St

New York, NY 22222

Cost: $4.25

========================================

Two-Day Shipping Option

Sender:

Max Ryder

1000 San Diario

San Jose, CA 75555

Recipient:

Mark Smith

7 Elm St

New York, NY 22222

Cost: $6.25

========================================

Overnight Shipping Option

Sender:

Max Ryder

1000 San Diario

San Jose, CA 75555

Recipient:

Mark Smith

7 Elm St

New York, NY 22222

Cost: $6.38

Press any key to continue . . .

Sample Code!!!!!!!

#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;
}


Read more here: https://stackoverflow.com/questions/64902751/c-how-can-i-split-this-code-into-two-different-files-header-and-cpp

Content Attribution

This content was originally published by felipedrivas at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: