HELP Calling function from another class

HEllo everyone so I am using a base class and other sub classes. My problem is that I don't know how to call a function from a base class so that I can use it for another class.

Here is a part of my base class implementation file. I want to reuse the function: getInsuranceFee();



This is the instructions:

The base class Package’s calculateCost function should determine the cost by multiplying the weight by the costPerOunce and adding the insurance fee if applicable. Insurance fees are based on the item’s declared value or insuranceType as shown in the table below:
Insurance Type Insurance Fee ($)
Upto100 5
Upto1000 15
Upto5000 40
PriorityPackage should redefine calculateCost function so it calculates the shipping cost based on the package type and dimensions and independent of its weight. A PriorityPackage’s cost is a flat fee that depends on the package type and dimensions as summarized below:

Width
Length
Height
Cost ($)
Box
< 12
< 12
< 5
25
Box
< 11
< 8
< 5
17
Box
< 8
< 5
< 2
10
Envelope



7
Letter



3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  double Package::getInsuranceFee()
{

  double insuranceFee=0.0;

  if (insuranceType=="none")
    {
      insuranceFee=0.0;
    }
  else if(insuranceType=="upto100")
    {
      insuranceFee=5.0;
    }

  else if(insuranceType=="upto1000")
    {
      insuranceFee=15.0;
    }

  else if(insuranceType=="upto5000")
    {
      insuranceFee=40.0;
    }

  return insuranceFee;
}


double Package::getWeight() const
{

    return weight;
}

double Package::getCost() const
{/*                                                                                                                                                                                  
   if(costPerOunce <0)                                                                                                                                                               
   {                                                                                                                                                                                 
   cout << "Invalid.";                                                                                                                                                               
   }                                                                                                                                                                                 
   else*/
  return costPerOunce;
}

double Package::calculateCost()
{
  double cost=0;

  double fee=getInsuranceFee();

  cost= (weight*costPerOunce) + fee;

  return cost;
}

void Package::printPackage()
{
  double  cost=calculateCost();

  cout << senderName << " " << senderAddress << " " << senderCity << " " << senderState << " " << senderZIPcode << " " << recipName << " " << recipAddress << " " << recipCity << " \
" << recipState << " " << recipZIPcode << " " << label << " "<< date <<  " " << weight<<  " " << costPerOunce << " " <<insuranceType  << " " << cost << endl;;

}


This is my other subclass "PriorityPackage"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
double PriorityPackage::calculateCost()
{
  Package::getInsuranceFee();

  double cost, total, fe,k;

  if(type== "box")
    {

      if(width <12 && length <12 && height <5)
        {
          cost=25.00;
        }

      else if(width <11 && length <8 && height <5)
        {
          cost=17.00;
        }
      else if( width <8 && length <5 && height <2)
        {
          cost= 10.00;
        }

    }

  else if(type== "envelope")
    {
      cost=7.00;

    }

  else if(type== "letter")
    {
      cost=3.00;
    }


  k=getInsuranceFee();

  total=cost+ k;

  return total;
}
void PriorityPackage::printPriority()
{
  double total=calculateCost();

  cout << senderName << " " << senderAddress << " " << senderCity << " " << senderState<< " " << senderZIPcode << " " << recipName << " " << recipAddress << " " << recipCity << " "\
 << recipState << " " << recipZIPcode << " " << label << " " << date <<" " << weight << " " << costPerOunce << " " <<insuranceType << " " << type << " " << length << " " << width <\
< " " <<height<< " " << total <<endl;

}


How can I use the getInsuranceFee() to my calculateCost() function in PriorityPackage.cpp?
Last edited on
I would think that Package is the base class of PriorityPackage. You did not show the declaration of the classes.
Topic archived. No new replies allowed.