Jacob Moses
Jacob Moses

Reputation: 173

C++ Inheriting constructors with multiple parameters [Homework]

I really hope this is the last time I ask a homework question for a while. I have been working at this for the past seven hours, have thrown out all my code three times, and I cannot seem to overcome my issues with inheriting a constructor. I have looked at other questions, followed every suggestion that I have seen, and still no dice. Any and all help is appreciated.

Header File:

#ifndef PACKAGE_H
#define PACKAGE_H

struct info
{
    std::string sName;
    std::string sAddr;
    std::string sCity;
    std::string sState;
    std::string sZip;

    std::string rName;
    std::string rAddr;
    std::string rCity;
    std::string rState;
    std::string rZip;
};

class Package
{
 public:
    Package(int c, double w, double co, info *i); //Constructor. Will implement 
                                       //all data types

    ~Package();                            // Default Destructor

    virtual double calculateCost();        // Not pure, can be overloaded

 protected:

    int counter;
    double weight;
    double cost;

    info sendRecieve;                      // Struct for holding tedious info
};

class TwoDayPackage : public Package
{
 public:
    TwoDayPackage(int c, double w, double co, info *i, double a)
    :Package(c, w, co, *i, a);

    double calculateCost();

 private:
    double flatFee;
};

class OvernightPackage : public Package
{
 public:
    OvernightPackage(int c, double w, double co, info *i, double a)
    :Package(c, w, co, *i);

    double calculateCost();

 private:
    double addtlCost;
};

.cpp definition file:

#include "Package.h"

using namespace std;

Package::Package(int c, double w, double co, info *i)
{
    cost = c;
    weight = w;
    cost = co;

    sendRecieve.sName = i->sName;
    sendRecieve.sAddr = i->sAddr;
    sendRecieve.sCity = i->sCity;
    sendRecieve.sState = i->sState;
    sendRecieve.sZip = i->sZip;

    sendRecieve.rName = i->rName;
    sendRecieve.rAddr = i->rAddr;
    sendRecieve.rCity = i->rCity;
    sendRecieve.rState = i->rState;
    sendRecieve.rZip = i->rZip;
}

double Package::calculateCost()
{
    return (cost * weight);
}

// end base class definitions

TwoDayPackage::TwoDayPackage(int c, double w, double co, info *i, double a)
    :Package(c, w, co, *i)
{
    flatFee = a;
}

double TwoDayPackage::calculateCost()
{
    return ((cost * weight) + flatFee);
}

//end TwoDayPackage definitions

OvernightPackage::OvernightPackage(int c, double w, double co, info *i, double a)       :Package(c, w, co, *i)
{
    addtlCost = a;
}

double OvernightPackage::calculateCost()
{
    double multi = (addtlCost + cost);

    return (multi * weight);
} 

Here is an image of the errors that I keep getting. If anyone can help I will appreciate it. Also, any suggestions are welcome. Thank everyone in advance.

Screenshot of errors

Upvotes: 1

Views: 238

Answers (4)

Tristan Brindle
Tristan Brindle

Reputation: 16824

Package(int c, double w, double co, info *i);

TwoDayPackage(int c, double w, double co, info *i, double a)
:Package(c, w, co, *i, a);

Two problems here: firstly, TwoDayPackage is requesting a Package constructor which takes five arguments, but the one defined only takes four arguments.

But a rather bigger problem is that the initialiser list -- the bit after the : -- belongs to the definition, not the declaration, so it needs to go in your .cpp file, with the rest of the definition, not in the header.

Upvotes: 1

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70929

The last parameter of the constructor of Package is a Info* while int he initialization list of OvernightPackage you pass *i to it. i is of type Info* thus *i is of type Info& which is not as expected by the constructor of Package.

As for the call in the intialization list of TwoDayPackage, you are passing 5 arguments to the constructor of Package while it only takes 4.

Upvotes: 2

Bathsheba
Bathsheba

Reputation: 234695

Drop the base member initialisation from your constructor declarations.

That is, in your header, replace

TwoDayPackage(int c, double w, double co, info *i, double a)
    :Package(c, w, co, *i, a);

with, simply

TwoDayPackage(int c, double w, double co, info *i, double a);

Also, make sure that your base member initialisers have the correct number and type of parameters.

Upvotes: 0

Blacktempel
Blacktempel

Reputation: 3995

This can't work. Your constructor only takes 4 arguments, you called it with 5.

class TwoDayPackage : public Package
{
 public:
    TwoDayPackage(int c, double w, double co, info *i, double a)
    :Package(c, w, co, *i, a); //Here

//[...]
Package(int c, double w, double co, info *i);

Upvotes: 0

Related Questions