4 Replies - 500 Views - Last Post: 02 May 2012 - 12:05 PM Rate Topic: -----

#1 teamsilvamma  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 153
  • Joined: 17-January 12

inheritance

Posted 02 May 2012 - 07:49 AM

I'm working on an inheritance program. The main class contains the sender and recipient's information, as well as the weight of the package and the cost per ounce to ship on a standard shipment. The derived class inherits the information from the main package class and adds the overnite fee on top of the regular rate. My question is, how do I establish that inheritance? I followed the textbook but something is not working. I'm not sure of what to pass to the derived class and will it be automatically used from the main class. It is my understanding that as long as I tell the derived class to inherit the main class, everything in the main class is usable and readily accessible. Here is the files so you can check them out. Th eerro I'm getting says: overnightpackage.h|20|error: expected class-name before '{' token|


/***************************
* package.h
* Luis Silva
*
* header file for package.cpp
****************************/
#ifndef PACKAGE_H
#define PACKAGE_H

#include<string>

using namespace std;

class package
{
    public:
        package( const string &, const string &, const string &,
                const string &, const string &, double = 0.0, double = 0.0 );

        //customer info function
        void setCustomer ( const string &, const string &, const string &, const string &, const string & );
        string getCustomer()const;

        void setStandWeight( double );
        double getStandWeight () const;

        void setCostPerOunze( double );
        double getCostPerOunze () const;

        double calculateCost() const;
        void displayMessage() const;
        void print() const;

    private:

        string customerName, customerAddress, customerCity, customerState, customerZip;
        double standWeight, costPerOunze;
};

#endif // PACKAGE_H




/********************************
* package.cpp
* base class
* Luis Silva
*
*********************INSTRUCTIONS******************************
* Create an inheritance hierarchy to represent various packages
* Build a base class and two subclasses that derive from base class
* Base class includes data members:
    * name, address, city, state, and zip code for sender and recipient.
    * weigh in oz (positive values only)
    * cost per oz (positive values only)
* Package constructor initializes all data members
* Package includes public member function calculateCost with algorithm:
    weight * cost
**************************OUTPUT*****************************


*************************************************************/
#include "package.h"

#include<iostream>
using namespace std;


package::package( const string &name, const string &address,
                const string &city, const string &state,
                const string &zip, double weight, double cost )
{
    customerName = name;
    customerAddress = address;
    customerCity = city;
    customerState = state;
    customerZip = zip;
    setStandWeight ( weight );
    setCostPerOunze ( cost );
}

void package::setCustomer( const string &name, const string &address, const string &city, const string &state, const string &zip )
{
    customerName = name;
    customerAddress = address;
    customerCity = city;
    customerState = state;
    customerZip = zip;
}

string package::getCustomer() const
{
    return customerName;
    return customerAddress;
    return customerCity;
    return customerState;
    return customerZip;
}

void package::setStandWeight( double weight )
{
    standWeight = ( weight < 0.0 ) ? 0.0 : weight;
}

double package::getStandWeight() const
{
    return standWeight;
}
void package::setCostPerOunze( double cost )
{
    costPerOunze = ( cost < 0.0 ) ? 0.0 : cost;
}

double package::getCostPerOunze() const
{
    return costPerOunze;
}


double package::calculateCost() const
{
    return standWeight * costPerOunze;
}

void package::displayMessage() const
{
    cout << "Name:   " << customerName << "\nAdress: " << customerAddress
    << "\nCity:   " << customerCity << "\nState:  " << customerState
    << "\nZip:    " << customerZip << endl;
}

void package::print() const
{

    cout << "Package weight is: " << standWeight << endl;
    cout << "Cost per ounze is: $" << costPerOunze << endl;
    cout << "Standard Delivery Rate is: $" << calculateCost() << endl;
}




/***************************
* overnightpackage.h
* subclass

* Luis Silva
*
* header file for overnightpackage.cpp
****************************/

#ifndef OVERNIGHTPACKAGE_H
#define OVERNIGHTPACKAGE_H

#include <string>
//#include "package.h"

using namespace std;

class OvernightPackage : public package

{
    public:
        OvernightPackage( const string &, const string &, const string &,
                const string &, const string &, double = 0.0, double = 0.0, double = 0.0 );

        void setOverniteFee( double );
        double getOverniteFee() const;
        double calculateCost() const;

    private:
        double overniteFee;
};

#endif // OVERNIGHTPACKAGE_H




/********************************
* overnightpackage.cpp
* subclass
* Luis Silva

*********************INSTRUCTIONS******************************
* Inherit functionality from package class
* Create a data member for overnite-delivery service rate
* Initilize data member in the constructor
* Redefine function calculateCost with algorithm
    overnitefee + cost = overniterate
    weight * overniterate
*********************INSTRUCTIONS******************************/

#include <iostream>
#include "overnightpackage.h"

using namespace std;

OvernightPackage::OvernightPackage( const string &name, const string &address, const string &city,
                                   const string &state, const string &zip, double overnite ) : package()
{
    setOverniteFee = ( overnite );
}

void OvernightPackage::setOverniteFee( double overnite)
{
    overniteFee = ( overnite < 0.0 ) ? 0.0 : overnite;
}

double OvernightPackage::getOverniteFee() const
{
    return overniteFee;
}





/********************************
* main.cpp
* Luis Silva
*
* write test program
* create objects for each type of package
* test function calculateCost
*************************************************************/
#include <iostream>
#include <iomanip>
#include "package.h"

using namespace std;

int main()
{
    cout << fixed << setprecision( 2 );

    package sender( "Mr. John Doe", "100 Elm St.", "Philadelphia", "PA", "17100" );
    package recipient( "Mrs.Sally Sue", "321 Main St.", "New York", "NY", "12345" );
    //package myTwoDayPackage;
    //package myOvernightPackage;

    cout << "*********SENDER********\n" << endl;
        sender.displayMessage();
    cout << "\n*********RECIPIENT********\n" << endl;
        recipient.displayMessage();
    cout << endl;

    sender.setStandWeight(23.78);
    sender.setCostPerOunze(.58);
    sender.print();
    //myPackage.calculateCost();
    //myTwoDayPackage.calculateCost();
    //myOvernightPackage.calculateCost();

}




Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: inheritance

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,533
  • Joined: 05-May 05

Re: inheritance

Posted 02 May 2012 - 08:39 AM

Quote

Here is the files so you can check them out. Th eerro I'm getting says: overnightpackage.h|20|error: expected class-name before '{' token|


GCC can't find the definition for the class Package because you've commented out the include directive.

For your inheritance problem, you said an OvernightPackage adds a charge to the cost. So if needs to then override that cost function and add the fee right?

class OvernightPackage: public Package {
     void calculateCost() {
          return Package::calculateCost()+overnightFee;
     }
};


Even if you create an instance of the subclass and reference it with a super type, the overridden implementation will run. A.k.a polymorphism.

Package* p = new OvernightPackge(args);
p->calculateCost(); //calls OvernightPackage::calculateCost()


This post has been edited by blackcompe: 02 May 2012 - 08:39 AM

Was This Post Helpful? 2
  • +
  • -

#3 teamsilvamma  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 153
  • Joined: 17-January 12

Re: inheritance

Posted 02 May 2012 - 08:54 AM

That was great help..I can't believe I missed that double slash ha ha

I'm having an error in this line

        OvernightPackage( const string &, const string &, const string &,
                       const string &, const string &, double = 0.0, double = 0.0, double = 0.0 );



error: prototype for 'OvernightPackage::OvernightPackage(const std::string&, const std::string&, const std::string&, const std::string&, const std::string&, double)' does not match any in class 'OvernightPackage'|

I thought this is inherited
Was This Post Helpful? 0
  • +
  • -

#4 teamsilvamma  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 153
  • Joined: 17-January 12

Re: inheritance

Posted 02 May 2012 - 09:31 AM

View Postblackcompe, on 02 May 2012 - 10:39 AM, said:

Quote

Here is the files so you can check them out. Th eerro I'm getting says: overnightpackage.h|20|error: expected class-name before '{' token|


GCC can't find the definition for the class Package because you've commented out the include directive.

For your inheritance problem, you said an OvernightPackage adds a charge to the cost. So if needs to then override that cost function and add the fee right?

class OvernightPackage: public Package {
     void calculateCost() {
          return Package::calculateCost()+overnightFee;
     }
};


Even if you create an instance of the subclass and reference it with a super type, the overridden implementation will run. A.k.a polymorphism.

Package* p = new OvernightPackge(args);
p->calculateCost(); //calls OvernightPackage::calculateCost()



can you explain the polymorphism part? I have not covered that in class and we are down to the final week already so I'm pretty sure we wont

btw..the program works I just have to make sure that I can call the calculateCost function from the overnightPacakage to print out.
Was This Post Helpful? 0
  • +
  • -

#5 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,533
  • Joined: 05-May 05

Re: inheritance

Posted 02 May 2012 - 12:05 PM

A thorough explanation would take too long and would probably confuse you even more. Refer to this polymorphism tutorial.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1