I'm doing an exercise where I create a sample package simulator system using inheritance and then read the class objects into an array. I started off without an array first just to get everything working but now that I have I'm not sure how to do it with an array. I've been told I could use the 'new' feature like
but when I try the compiler goes crazy.Code:OverNightPackages[i] = new OvernightPackage()
This is the code I have:
and the error I'm currently getting from my compiler:Code:#include <iostream> #include <string> using namespace std; class Package // Base Class { private: string name, city, state, sender, recipient; int zip; string address; float weight; // in ounce double cost; // per ounce public: void setName(string name); void setCity(string city); void setState(string state); void setZip(int zip); void setAddress(string address); void setSender(string sender); void setRecipient(string recipient); string getName() { return name; } string getCity() { return city; } string getState(){ return state;} int getZip() { return zip; } string getAddress() { return address; } string getSender() { return sender; } string getRecipient() { return recipient; } double calculateCost(float weight, double costPerOunce) //function that calculate the cost z { double z; //total cost = weight*cost per ounce z = weight * costPerOunce; //the cost z cout<< "The Base Cost = " <<z <<endl; return z; } }; // end class Package void Package::setName(string n) { name = n; } void Package::setCity(string c) { city = c; } void Package::setState(string s) { state = s; } void Package::setZip (int zp) { zip = zp; } void Package::setAddress(string adr) { address = adr; } void Package::setSender(string sen) { sender = sen; } void Package::setRecipient(string rec) { recipient = rec; } class TwoDayPackage: public Package // derived class { public: double calcShippingCost(float weight, double costPerOunce, double flatFee) /* function that calculate shipping cost by adding the flat fee to the weight-based cost calculated by base class Package's calculateCost function*/ { double z; //shippingcost of Two day Package class z= calculateCost(weight,costPerOunce) + flatFee; cout<< "The Two Day Package Cost = " << z << endl; return z; } private: double flatFee; }; // end TwoDayPackage class OvernightPackage: public Package //derived class that adds the additional fee per ounce { public: double calcCostOvernight(float weight, double costPerOunce, double additionalCost ) { double z; //shippingcost of overnight class z = calculateCost(weight, costPerOunce)+(additionalCost * weight); cout<< "The OvernightPackage Cost = " <<z << endl; return z; } private: double overnightCost; //per ounce }; // end class OvernightPackage int main() { int i; //i represent the user`s choice number string customerName, customerAddress, city, state, senderAddress, recipientAddress; float weight; string customerCity; double costPerOunce; double flatFee; double additionalCost; string customerState; int customerZipcode; Package base; //the object base of the package class TwoDayPackage* twoday[100]; //the object twoday of the first inhereted calss OvernightPackage* overnight[100]; //the object overnight of the second inhereted calss /* for (int c = 0; c<= 100;c++){ cin >> twoday [ c ]; } for (int d = 0; d<=100;d++){ cin >> overnight[ d ]; } */ /* do{ */ cout<<" This is the FedEx Package Sending System"<<endl<<endl; cout<< "Please fill in your package request form: " <<endl<<endl; cout<<"Enter Customer Name "<<endl<<endl; getline(cin, customerName); cout<<endl; base.setName(customerName); cout<<"Enter Customer Address"<<endl<<endl; getline(cin, customerAddress); cout<<endl; base.setAddress(customerAddress); cout<<"Enter Customer City"<<endl<<endl; getline(cin, customerCity); cout<<endl; base.setCity(customerCity); cout<<"Enter Customer State"<<endl<<endl; getline(cin, customerState); cout<<endl; base.setState(customerState); cout<<"Enter Customer ZIP code"<<endl<<endl; cin >> customerZipcode; cout<<endl; base.setZip(customerZipcode); cout<<"Enter Weight"<<endl; cin >> weight; cout<<endl; cout<<"Enter Cost Per Ounce"<<endl; cin>>costPerOunce; cout<<endl; cout<<"Please Enter Your Choice From The Menu Below:"<<endl<<endl; cout<<" 1- Calculate Base Cost "<<endl<<endl; cout<<" 2- Calculate Two Day Cost "<<endl<<endl; cout<<" 3- Calculate Over Night Cost"<<endl<<endl; cin>>i; cout<<endl; //i represent customer choice switch (i) { case 1 : base.calculateCost(weight, costPerOunce); break; case 2 : cout<<"Enter Flat Cost"<<endl<<endl; //additonal(to weight and cost) needed information cin>>flatFee; twoday.calcShippingCost(weight,costPerOunce,flatFee); break; case 3 : cout<<"Enter additional cost"<<endl<<endl; cin>>additionalCost; overnight.calcCostOvernight(weight,costPerOunce,additionalCost); break; default: cout<<"Invalid input. Please enter choice number. "<< endl; } // end switch cout << "Now entering sender and recipient info." << endl; cout<<"Enter the sender address " << endl; cin >> senderAddress; cout<<endl; base.setSender(senderAddress); cout<<"Enter recipient address" << endl; cin >> recipientAddress; cout<<endl; base.setRecipient(recipientAddress); cout <<"Address from:"<< senderAddress <<endl; cout<<"To:"<< recipientAddress << endl; /* }while */ return 0; } // end main
I've tried all the ways I know how to read info into arrays but I only know how to do it for non-class objects.Code:In function 'int main()': 210:10: error: request for member 'calcShippingCost' in 'twoday', which is of non-class type 'TwoDayPackage [100]' 216:19: error: request for member 'calcCostOvernight' in 'overnight', which is of non-class type 'OvernightPackage [100]'




Reply With Quote
