Thread: need help with classes

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    44

    need help with classes

    im trying to make a variable in a class which changes and doesnt start over from the initial value ... for example if i have int quantity = 500 and subtract 100 the new value will be 400 then i subtract 100 and new value will be 300 ... etc how owuld i go about doing this.. i tried using static int but it gave me errors

    heres my class

    Code:
    class sales
    {
    public:
    	//default constructor
    	sales();
    	//mutator member functions
    	void setCustomer(string custom);
    	void setProduct(string prod);
    	void setQuantity(int qua);
    	void setMonth(int m);
    	void setDay(int d);
    	void setYear(int y);
    	void setValid(bool a);
    	void setFTime(bool a);
    	void setTotal(int tot);
    	void setFirst(int a);
    	//accessor member functions
    	bool getValid();
    	bool getFTime();
    	int getMonth();
    	int getDay();
    	int getYear();
    	string getCustomer();
    	string getProduct();
    	int getQuantity();
    	int getTotal();
    	int getFirst();
    
    
    	int checkDay(int); //utility function to test proper day for month and year
    protected:
    	//data members
    	string customerNum;
    	string productName;
    	int quantity;
    	bool valid;
    	bool ftime;
    	int month;	//1-12
    	int day;//1-31 based on month
    	int year;//any year
    	int total;
    	int first;
    };
    class ProductManager: public sales
    {
    public:
           string productName1;
           float price;
           static int quantity;
    };
    
    ProductManager store[40];
    the code that im having trouble with

    Code:
        int h = num_lines("products.txt");
        int j = num_lines(file);
         fstream in_file("products.txt",ios::in);
         if(!in_file)
         {
             cout << "error opening file";
             exit(0);
         }	
         for(i = 0; i < h; i++)
         {
                 in_file >> store[i].productName1;
                 in_file >> store[i].quantity;
                 in_file >> store[i].price;
         }
         for(i=0; i < j; i++)
         {
            cout << "Product Name:" << list.at(i).getProduct() << endl; 
            for(int k=0; k<h; k++)
            {
                if(list.at(i).getProduct() == store[k].productName1)
                  {
                      cout << "Quantity Purchased:" << list.at(i).getQuantity() << endl; 
                      cout << "Price:" << store[k].price << endl; 
                      cout << "Amount of Sale:" << store[k].price * list.at(i).getQuantity() << endl; 
                      cout << "Quantity remaining in stock:" << store[k].quantity - list.at(i).getQuantity() << endl << endl;
    
                  }
                      
            }     
    
    
         }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    #include <iostream>
    using namespace std;
    
    class Test
    {
    private:
    	static int num;
    
    public:
    	void setNum(int n)
    	{
    		num = n;
    	}
    
    	int getNum()
    	{
    		return num;
    	}
    	
    };
    
    //initialize static member(this would go in a
    //.cpp file in a multifile project):
    int Test::num = 500;
    
    	
    int main()
    {
    	Test a;
    	cout<<a.getNum()<<endl;
    	a.setNum(a.getNum() - 100);
    	cout<<a.getNum()<<endl<<endl;
    
    	Test b;
    	cout<<b.getNum()<<endl;
    	b.setNum(b.getNum() - 100);
    	cout<<b.getNum()<<endl<<endl;
    
    	cout<<a.getNum()<<endl;
    	
    	return 0;
    }
    Don't name a member variable in a derived class with the same name as a variable in a base class:
    Code:
    protected:
    	//data members
    	string customerNum;
    	string productName;
    	int quantity;
    	bool valid;
    	bool ftime;
    	int month;	//1-12
    	int day;//1-31 based on month
    	int year;//any year
    	int total;
    	int first;
    };
    class ProductManager: public sales
    {
    public:
           string productName1;
           float price;
           static int quantity;
    };
    Last edited by 7stud; 12-03-2005 at 08:48 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There is no need to make quantity static, just make it a regular variable like you do in sales. You can update it simply by assigning it a new value or by adding a setQuantity function to ProductManager.

    Technically, since you derive ProductManager from sales, you could just use the quantity stored in the sales base class, but it doesn't really make sense to derive ProductManager from sales so it would be better to get rid of that and just add a similar quantity variable to ProductManager.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    Quote Originally Posted by Daved
    There is no need to make quantity static, just make it a regular variable like you do in sales. You can update it simply by assigning it a new value or by adding a setQuantity function to ProductManager.

    Technically, since you derive ProductManager from sales, you could just use the quantity stored in the sales base class, but it doesn't really make sense to derive ProductManager from sales so it would be better to get rid of that and just add a similar quantity variable to ProductManager.

    hmm its not working it keeps subtracting from the initial value heres my class..

    Code:
     
    class ProductManager: public sales
    {
    public:
           string productName1;
           float price;
           int getQuantity1();
           void setQuantity1(int);
    
    private:
           int quantity1;
    
    };
    
    ProductManager store[40];
    
    void ProductManager::setQuantity1(int n)
    {
         quantity1 = n;
    }
    
    int ProductManager::getQuantity1()
    {
         return quantity1;
    }
    the section


    Code:
       int tmpquantity;
        int h = num_lines("products.txt");
        int j = num_lines(file);
         fstream in_file("products.txt",ios::in);
         if(!in_file)
         {
             cout << "error opening file";
             exit(0);
         }	
         
         for(i = 0; i < h; i++)
         {
                 in_file >> store[i].productName1;
                 in_file >> tmpquantity;
                 in_file >> store[i].price;
                 store[i].setQuantity1(tmpquantity);
         }	
         
         for(i=0; i < j; i++)
         {
            cout << "Product Name:" << list.at(i).getProduct() << endl; 
            for(int k=0; k < h; k++)
            {
                if(list.at(i).getProduct() == store[k].productName1)
                  {
                      cout << "Quantity Purchased:" << list.at(i).getQuantity() << endl; 
                      cout << "Price:" << store[k].price << endl; 
                      cout << "Amount of Sale:" << store[k].price * list.at(i).getQuantity() << endl; 
                      cout << "Quantity remaining in stock:" << (store[k].getQuantity1() - list.at(i).getQuantity()) << endl << endl;
    
                  }
                      
            }     
    
    
         }

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    When you subtract the sold quantity, you have to re-store the new quantity with setQuantity1. I would do the subtraction and setting first, then just output the result in a separate line of code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM