Need to Convert Classes Program to Also Use Vectors, Pointers, Polymorphism

My Intermediate C++ professor wants this program updated as follows: "The project should contain a vector of package pointers to objects of classes TwoDayPackage and OvernightPackage. Loop through the vector to process the packages polymorphically."

I understand "objects of classes" but I do not understand what he means by "a vector of package pointers", and how polymorphic processing will vary from traditional processing. Unfortunately, he refuses to clarify his homework instructions.

Package.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef PACKAGE_H
#define PACKAGE_H 
#include <string> 
using namespace std; 

class Package
{
	public: 
	Package(); 
        double getWeight() const; 
        double getCost() const;        
	double calculateCost(); 
	void printPackage1() const; 
	void printPackage2() const; 
	void printPackage3() const;  

	private: 
	string senderName, senderAddress, senderCity, senderState; senderZip;
        string recipientName, recipientAddress, recipientCity, recipientState, recipientZip;   
	double weight, cost;  

}; 

#endif  


Package.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "Package.h"
#include <iostream> 
#include <string> 
using namespace std; 

Package::Package()
{
	weight = 0.0, cost = 0.0;
	// strings do not absolutely require initialization, including this for compelteness 
	senderName = "", senderAddress = "", senderCity = "", senderState = ""; 
	recipientName = "", recipientAddress = "", recipientCity = "", recipientState = "";
    // changed Zip from int to string.  Int was dropping zeros in zip codes starting with zero.  
	senderZip = "", recipientZip = "";  
}  

double Package::getWeight() const
{
    double weight = 17;
    return weight; 
}

double Package::getCost() const
{
	double cost = .25; 
	return cost; 
}

double Package::calculateCost()
{ 
	return getWeight() * getCost(); 
}

void Package::printPackage1() const
{

	cout << "Package 1" << endl << endl; 
	
	cout << "Sender:" << endl; 	
	cout << "Lou Brown" << endl; 
	cout << "1 Main St" << endl; 
	cout << "Boston, MA 11111" << endl << endl;
	
	cout << "Recipient:" << endl; 
	cout << "Mary Smith" << endl; 
	cout << "7 Elm St " << endl; 
	cout << "New York, NY 22222" << endl << endl; 

}

void Package::printPackage2() const
{

	cout << "Package 2" << endl << endl; 
	
	cout << "Sender:" << endl; 	
	cout << "Lisa Klein" << endl; 
	cout << "5 Broadway" << endl; 
	cout << "Somerville, MA 33333" << endl << endl;
	
	cout << "Recipient:" << endl; 
	cout << "Bob George" << endl; 
	cout << "21 Pine Rd" << endl; 
	cout << "Cambridge, MA 44444" << endl << endl; 

}

void Package::printPackage3() const
{
	cout << "Package 3" << endl << endl; 
	
	cout << "Sender:" << endl; 	
	cout << "Ed Lewis " << endl; 
	cout << "2 Oak St" << endl; 
	cout << "Boston, MA 55555" << endl << endl;
	
	cout << "Recipient:" << endl; 
	cout << "Don Kelly" << endl; 
	cout << "9 Main St" << endl; 
	cout << "Denver, CO 66666" << endl << endl; 
}


TwoDayPackage.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef TWODAYPACKAGE_H
#define TWODAYPACKAGE_H 
#include "Package.h"    

class TwoDayPackage : public Package
{

	public: 
		TwoDayPackage(); 
		double calculateCost(); 


	private: 
		double flatFee; 	

}; 

#endif  


TwoDayPackage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "TwoDayPackage.h" 
#include <iostream> 
using namespace std; 

TwoDayPackage::TwoDayPackage()
{
	flatFee = 4.57; 
} 

double TwoDayPackage::calculateCost()
{
    return (flatFee + Package::calculateCost()); 
}   


OvernightPackage.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef OVERNIGHTPACKAGE_H
#define OVERNIGHTPACKAGE_H
#include "Package.h"   

class OvernightPackage : public Package
{ 
		
	public: 
		OvernightPackage();
		double calculateCost();


	private: 
		double flatFee; 


}; 

#endif 


OvernightPackage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "OvernightPackage.h" 
#include <iostream> 
using namespace std; 

OvernightPackage::OvernightPackage()
{
	flatFee = 7.39; 
} 

double OvernightPackage::calculateCost()
{
    return (flatFee + Package::calculateCost()); 
}   


TestPackage.cpp
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
#include "Package.h"
#include "TwoDayPackage.h"
#include "OvernightPackage.h" 
#include <iostream> 
#include <iomanip>
using namespace std; 

int main()
{

	// create objects 
	Package myPackage; 
	TwoDayPackage myTwoDayPackage; 
	OvernightPackage myOvernightPackage;  

        // print objects and use inherited calculateCost     
        myPackage.printPackage1(); 
	cout << "Cost: $" << myPackage.calculateCost() << endl << endl; 
	myPackage.printPackage2(); 
	cout << "Cost: $" << myTwoDayPackage.calculateCost() << endl << endl;
	myPackage.printPackage3(); 
	cout << "Cost: $" << myOvernightPackage.calculateCost() << endl << endl;
	
	
	//end program 
	system ("PAUSE"); 
	return 0; 
}
 


Assistance will be appreciated!

Thanks
Last edited on
I think he means your class Package inside the vector. Such as

1
2
#include <vector>
std::vector<Package*> packages
Last edited on
Topic archived. No new replies allowed.