1. (Package Inheritance Hierarchy) Use the package inheritance hierarchy (below)
ID: 3903895 • Letter: 1
Question
1. (Package Inheritance Hierarchy) Use the package inheritance hierarchy (below) to create a program that displays the address information and calculates the shipping costs for several packages. The program should contain a vector of package pointers to objects of classes: TwoDayPackage and OvernightPackage. Loop through the vector to process the packages polymorphically. For each package, invoke get functions to obtain the address information of the sender and the recipient, then print the two addresses as they would appear on mailing labels. Also, call each package's calculateCost member function and print the result. Keep track of the total shipping cost for all packages in the vector, and display this total when the loop terminates.
#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<<" Name of sender : "<<nameSender;
cout<<" Address : "<<addressSender;
cout<<" City : "<<citySender;
cout<<" State : "<<stateSender;
cout<<" ZIP : "<<ZIPSender;
cout<<" Nameof Recipient : "<<nameRecipient;
cout<<" Address : "<<addressRecipient;
cout<<" City : "<<cityRecipient;
cout<<" State : "<<stateRecipient;
cout<<" ZIP : "<<ZIPRecipient;
cout<<" Weight of Package : "<<weight;
cout<<" Cost 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<<" Two Day Package ";
display();
cout<<" Flat 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<<" Overnight Package ";
display();
cout<<" Additional 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<<" Total 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<<" Total Cost : "<<p2.calculateCost();
return 0;
}
Output:
Two Day Package
Name of sender : John
Address : 234,New Street
City : ALBANY
State : NY
ZIP : 12261-0001
Nameof Recipient : Smith
Address : 307
City : Trenton
State : NJ
ZIP : 08625-0307
Weight of Package : 4.5
Cost per ounce : 12.5
Flat Fee : 15.5
Total Cost : 71.75
Overnight Package
Name of sender : Candy
Address : 234,New Street
City : ALBANY
State : NY
ZIP : 12261-0001
Nameof Recipient : Nancy
Address : 307
City : Trenton
State : NJ
ZIP : 08625-0307
Weight of Package : 4.8
Cost per ounce : 17.5
Additional Fee : 23.5
Total Cost : 107.5
2. Write a simple function template for predicate function isEqualTo that compares its two arguments of the same type with the equality operator (==) and returns true if they are equal and false otherwise.
Use this function template in a program that calls isEqualTo only with a variety of fundamental types. Now write a separate version of the program that calls isEqualTo with a user -defined class type, but does not overload the equality operator.
What happens when you attempt to run this program? Now overload the equality operator (with the operator function) operator ==. Now what happens when you attempt to run this program?
Explanation / Answer
1. main.cpp
#include <iostream>
#include <vector>
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<<" Name of sender : "<<nameSender;
cout<<" Address : "<<addressSender;
cout<<" City : "<<citySender;
cout<<" State : "<<stateSender;
cout<<" ZIP : "<<ZIPSender;
cout<<" Nameof Recipient : "<<nameRecipient;
cout<<" Address : "<<addressRecipient;
cout<<" City : "<<cityRecipient;
cout<<" State : "<<stateRecipient;
cout<<" ZIP : "<<ZIPRecipient;
cout<<" Weight of Package : "<<weight;
cout<<" Cost per ounce : "<<cost;
}
virtual void displayInfo() = 0;
};
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<<" Two Day Package ";
display();
cout<<" Flat 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<<" Overnight Package ";
display();
cout<<" Additional Fee : "<<additionalFee;
}
};
int main() {
//vector of package pointers pointing to objects of derived classes TwoDayPackage and OverNightPackage
vector<Package*> packages;
TwoDayPackage *p1 = new TwoDayPackage("John","234,New Street","ALBANY", "NY" ,"12261-0001","Smith","307"," Trenton", "NJ"," 08625-0307",4.5,12.5,15.5);
packages.push_back(p1);
OverNightPackage *p2 = new OverNightPackage("Candy","234,New Street","ALBANY", "NY" ,"12261-0001","Nancy","307"," Trenton", "NJ"," 08625-0307",4.8,17.5,23.5);
packages.push_back(p2);
//Variable to keep track of total shipping cost of all the packages
double totalCost=0.0;
//Looping through the vector to process all the packages(either TwoDayPackage or OverNightPackage) polymorphically
for (Package* package : packages)
{
cout<<" Address Information of package:";
package->displayInfo(); //Function invoked to get the address information of sender and recipient of package
cout<<" Cost of the package: "<<package->calculateCost()<<" "; //function invoked to calculate the shipping cost of package
//each package's shipping cost is added to the totalCost of all the packages
totalCost = totalCost+package->calculateCost();
}
cout<<" Total cost of all packages: "<<totalCost<<" ";
return 0;
}
Output:
------------
Address Information of package:
Two Day Package
Name of sender : John
Address : 234,New Street
City : ALBANY
State : NY
ZIP : 12261-0001
Nameof Recipient : Smith
Address : 307
City : Trenton
State : NJ
ZIP : 08625-0307
Weight of Package : 4.5
Cost per ounce : 12.5
Flat Fee : 15.5
Cost of the package:
56.25
Address Information of package:
Overnight Package
Name of sender : Candy
Address : 234,New Street
City : ALBANY
State : NY
ZIP : 12261-0001
Nameof Recipient : Nancy
Address : 307
City : Trenton
State : NJ
ZIP : 08625-0307
Weight of Package : 4.8
Cost per ounce : 17.5
Additional Fee : 23.5
Cost of the package:
84
Total cost of all packages: 140.25
2. Template for predicate function isEqualTo
main.cpp
#include <iostream>
using namespace std;
template<class T>
bool isEqualTo(T v1, T v2)
{
if(v1 == v2)
return true;
else
return false;
}
int main() {
int x=10,y=1;
double a=1.0, b=1.0;
string s="test", t="test";
cout<<"On Int compare result on "<<x <<" ,"<<y<<" is: "<<isEqualTo<int>(x,y)<<" ";
cout<<"On double compare result on "<<a<<" ,"<<b<<" is: "<<isEqualTo<double>(a,b)<<" ";
cout<<"On string compare result on "<<s<<" ,"<<t<<" is: "<<isEqualTo<string>(s,t)<<" ";
return 0;
}
output:
On Int compare result on 10 ,1 is: 0
On double compare result on 1 ,1 is: 1
On string compare result on test ,test is: 1
If same above function is used to compare two class objects as below:
main.cpp
#include <iostream>
using namespace std;
class MyClass {
int age;
string name;
public:
MyClass(int ageGiven, string nameGiven)
{
age=ageGiven;
name= nameGiven;
}
};
template<class T>
bool isEqualTo(T v1, T v2)
{
if(v1 == v2)
return true;
else
return false;
}
int main() {
int x=10,y=1;
double a=1.0, b=1.0;
string s="test", t="test";
MyClass c1 = MyClass(10,"s1");
MyClass c2 = MyClass(10,"s2");
cout<<"On Int compare result on "<<x <<" ,"<<y<<" is: "<<isEqualTo<int>(x,y)<<" ";
cout<<"On double compare result on "<<a<<" ,"<<b<<" is: "<<isEqualTo<double>(a,b)<<" ";
cout<<"On string compare result on "<<s<<" ,"<<t<<" is: "<<isEqualTo<string>(s,t)<<" ";
isEqualTo<MyClass>(c1,c2);
return 0;
}
will get a compilation error saying:
If the "==" operator is overloaded as below, it works as expected.
main.cpp
#include <iostream>
using namespace std;
class MyClass {
int age;
string name;
public:
MyClass(int ageGiven, string nameGiven)
{
age=ageGiven;
name= nameGiven;
}
//overloaded "==" operator function for class MyClass
bool operator==(const MyClass & rhs)
{
if (this->age == rhs.age)
if(this->name == rhs.name)
return true;
else
return false;
else
return false;
}
};
template<class T>
bool isEqualTo(T v1, T v2)
{
if(v1 == v2)
return true;
else
return false;
}
int main() {
int x=10,y=1;
double a=1.0, b=1.0;
string s="test", t="test";
MyClass c1 = MyClass(10,"s1");
MyClass c2 = MyClass(10,"s1");
cout<<"On Int compare result on "<<x <<" ,"<<y<<" is: "<<isEqualTo<int>(x,y)<<" ";
cout<<"On double compare result on "<<a<<" ,"<<b<<" is: "<<isEqualTo<double>(a,b)<<" ";
cout<<"On string compare result on "<<s<<" ,"<<t<<" is: "<<isEqualTo<string>(s,t)<<" ";
cout<<"On MyClass objects compare result: "<<isEqualTo<MyClass>(c1,c2);
return 0;
}
Output:
On Int compare result on 10 ,1 is: 0
On double compare result on 1 ,1 is: 1
On string compare result on test ,test is: 1
On MyClass objects compare result: 1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.