C++ - Parcel Packaging

Delivery costs for parcels and letters.

Header File

The header contains one parent class and two child classes. Box and Letter are child classes of Package.

#include <string>
using namespace std;

class Package{
    public:
        Package();
        Package(string, string);
        virtual ~Package();
        virtual double computeCost(){return 0.0;}
        string getSource()const{return *source;}
        string getDestination(){return *destination;}
        friend void sameDestination(Package*,Package*/*first and second*/);
    protected:
        string *source, *destination;
};

class Box:public Package{
    public:
        Box();
        Box(double/*package weight*/,string /*source*/,string /*destination*/);
        double computeCost();
        double getWeight(){return weight;}
    private:
        double weight;
};

class Letter:public Package{
    public:
        Letter();
        Letter(int/*number of pages*/,string /*source*/,string /*destination*/);
        double computeCost();
        int getNoPages(){return noPages;}
    private:
        int noPages;
};

Constructors

For testing purposes, we defined destinations on the server side. No user input was required for this assignment.

Package::Package(){
    source = new string("Mississauga","Kingston");
    assert(source!=NULL);
    destination = new string("Ottawa","Toronto");
    assert(destination!=NULL);
}

Package::Package(string newSource, string newDestination){
    source = new string(newSource);
    assert(source!=NULL);
    destination = new string(newDestination);
    assert(destination!=NULL);
}

Box::Box(){
    source = new string("Mississauga","Kingston");
    assert(source!=NULL);
    destination = new string("Ottawa","Kingston");
    assert(destination!=NULL);
}

Box::Box(double newWeight, string newSource, string newDestination)
    :Package(newSource, newDestination){
    weight = newWeight < 0.0 ? 0.0 : newWeight;
}

Letter::Letter(){
    source = new string("Mississauga","Kingston");
    assert(source!=NULL);
    destination = new string("Ottawa","Kingston");
    assert(destination!=NULL);
}

Letter::Letter(int newNoPages, string newSource, string newDestination)
    :Package(newSource, newDestination){
    noPages = newNoPages < 0 ? 0 : newNoPages;
}

Package::~Package(){
    delete source;
    delete destination;
}

Functions

As mentioned above, these functions do not require user input for the purpose of the assignment.

double Box::computeCost(){
    double cost;

    if(weight < 5.0)
        cost = .79;
    else if(weight > 15.0)
        cost = 4.75;
    else
        cost = 2.75;
    return cost;
}

double Letter::computeCost(){
    double cost;

    if(noPages < 10)
        cost = .45;
    else if(noPages > 85.0)
        cost = 2.89;
    else
        cost = .70;
    return cost;
}

void sameDestination(Package*first, Package*second){
    if(*first->destination == *second->destination)
        cout << "Box and Letter are same destinations." << endl;
    else
        cout << "Box and Letter are NOT same destinations." << endl << endl; //testing purposes
}

Client Side

#include <iostream>
#include "package.h"
#include <cassert>
#include <iomanip>

//STEVEN BARTSCH
//COMP 333
//ASSIGNMENT 2

//Prototypes
void displayCost(const Package*);

int main(){
    Package * objPackage = new Package("Mississauga","Kingston");
    assert(objPackage != NULL);
    Box * objBox = new Box(20.0,"Kingston","Boston");
    assert(objBox != NULL);
    Letter * objLetter = new Letter(2.0,"Ottawa","Toronto");
    assert(objLetter != NULL);

    //Friend Function
    sameDestination(objBox, objLetter);

    //Polymorphic Call
    displayCost(objBox);


    //Default Constructors
    Package * objPackage2 = new Package();
    assert(objPackage2 != NULL);
    Box * objBox2 = new Box();
    assert(objBox2 != NULL);
    Letter * objLetter2 = new Letter();
    assert(objLetter2 != NULL);

    //Show Results
    cout << setprecision(3) << "Box: " << objBox->getSource() << " to " << objBox->getDestination() << " costs: $" << objBox->computeCost() << endl;
    cout << setprecision(3) << "Letter: " << objLetter->getSource() << " to " << objLetter->getDestination() << " costs: $" << objLetter->computeCost() << endl << endl;

    //Delete Ptrs
    delete objPackage;
    delete objBox;
    delete objLetter;
    delete objPackage2;
    delete objBox2;
    delete objLetter2;

    //Stop
    system("pause");
}

//Polymorphic Function
void displayCost(const Package*ptr){
    cout << ptr->computeCost();
}

Output

Box and Letter are NOT same destinations.

Box: Kingston to Boston costs: $4.75
Letter: Ottawa to Toronto costs: $0.45

Press any key to continue . . .