dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Read class objects into array?

  1. #1
    Join Date
    Jul 2015
    Posts
    4

    Read class objects into array?

    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

    Code:
    OverNightPackages[i] = new OvernightPackage()
    but when I try the compiler goes crazy.

    This is the code I have:

    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
    and the error I'm currently getting from my compiler:

    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]'
    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.

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Read class objects into array?

    The issue is not how you allocate, but rather, you you are calling the functions. twoday is an array of 100 pointers. so "twoday.calculateShippingCost" does not make sense. You need to make it something like:
    twoday[0]->calculateShippingCost

    Also:
    Code:
    OverNightPackages[i] = new OvernightPackage()
    1. Please be consistent with your name. Is it "OverNight" or "Overnight". 1 or 2 words?
    2. If you are going to use lowerCamelCase for naming your variables, then stick to it.

    Also:
    Code:
    	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
    That's not how you use inheritance. Rather, you have only a Package pointer (or array or pointers, whichever). You then allocate either a TwoDayPackage or OvernightPackage into it. Once the object has been allocated, you stop caring about *what* it is, but rather, you just use it and don't care. Something like:
    Code:
        Package* package;
        cout<<"Please Enter Your Choice From The Menu Below:"<<endl<<endl; 
        cin>>i; 
        switch (i)
        {
            case 1:
                package = new Package(...);
                break;
            case 2:
                package = new TwoDayPackage(...);
                break;
            case 3:
                package = new OvernighPackage(...);
                break;
        }
        cout << "that's gonna cost you: " << package->calculateShippingCost() << endl;
    but most importantly:
    Code:
    double calculateCost(float weight, double costPerOunce) //function that calculate the cost z
    You need to declare this function virtual.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)