Thread: member Functions

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    36

    member Functions

    I wrote this program :

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <cstdlib>
    #include <fstream>
    using namespace std; 
    
    
    class Inventory{ 
          private:
                  int itemNumber;
                  int quantity;
                  double cost;
                  double totalCost;
          public:
                 Inventory ()
                 {
                            itemNumber = 0;
                            quantity = 0;
                            cost = 0;
                            totalCost = 0;
                 }
                 Inventory ( int n, int c, int q )
                 {
                            itemNumber = n;
                            quantity = q;
                            cost = c;
                            totalCost = setTotalCost();
                 }
                 void setItemNumber ( int number )
                 {
                            itemNumber = number;
                 }
                 void setQuantity ( int quant )
                 {
                            quantity = quant;
                 }
                 void setCost ( double cst )
                 {
                            cost= cst;
                 }
                 double setTotalCost ()
                 {
                            totalCost = quantity * cost;
                            return totalCost;
                 }
                 int getItemNumber ()
                 {
                            return itemNumber;
                 }
                 int getQuantity () 
                 {
                            return quantity;
                 }
                 double getCost ()
                 {
                            return cost;
                 }
                 double getTotalCost ()
                 {
                            return totalCost;
                 }
    };
    
    void Clear_Screen(void);
    
    int main()
    {
                 int it_number, it_quant;
                 float it_cost;
                 Inventory hammer, wrench, saw;
                 
                 cout<<"\nEnter Item Number for hammers: ";
                 cin>>it_number;
                 hammer.setItemNumber(it_number);
                 cout<<"\nEnter quantity of hammers: ";
                 cin>>it_quant;
                 hammer.setQuantity(it_quant);
                 cout<<"\nEnter the cost of one hammer: ";
                 cin>>it_cost;
                 hammer.setCost(it_cost);
                 hammer.setTotalCost();
                 Clear_Screen();
                 
                 cout<<"\nEnter Item Number for wrenches: ";
                 cin>>it_number;
                 wrench.setItemNumber(it_number);
                 cout<<"\nEnter quantity of wrenches: ";
                 cin>>it_quant;
                 wrench.setQuantity(it_quant);
                 cout<<"\nEnter the cost of one wrench: ";
                 cin>>it_cost;
                 wrench.setCost(it_cost);
                 wrench.setTotalCost();
                 Clear_Screen();
                 
                 cout<<"\nEnter Item Number for saws: ";
                 cin>>it_number;
                 saw.setItemNumber(it_number);
                 cout<<"\nEnter quantity of saws: ";
                 cin>>it_quant;
                 saw.setQuantity(it_quant);
                 cout<<"\nEnter the cost of one saw: ";
                 cin>>it_cost;
                 saw.setCost(it_cost);
                 saw.setTotalCost();
                 Clear_Screen();
                 
                 
                 cout<<"________Item Number_______Quantity_______Cost___________Total Cost___________" << endl << endl;
                 cout<< setw(14) <<hammer.getItemNumber() << setw(16) << hammer.getQuantity() << setw(15)
                 << hammer.getCost() << setw(20) << hammer.getTotalCost()<< endl;
                 cout << endl;
                 cout<< setw(14) <<wrench.getItemNumber() << setw(16) << wrench.getQuantity() << setw(15)
                 << wrench.getCost() << setw(20) << wrench.getTotalCost()<< endl;
                 cout << endl;
                 cout<< setw(14) <<saw.getItemNumber() << setw(16) << saw.getQuantity() << setw(15)
                 << saw.getCost() << setw(20) << saw.getTotalCost()<< endl;
                 cin.get(); cin.get();
                 
                 cout << "\n\n   Thank you for using the program!" << endl;
                 cout << "\n\n   Press any key to exit" << endl;
                 getchar();
                 return 0;
    }
    
    void Clear_Screen(void)
    {
       #ifdef _WIN32
          system("cls");
       #else
          system("clear");
       #endif
    }
    And now I need to modify it so the input and output are member functions. I thought I had, but now when I get to the display, the output is all zeros. I can't see where i went wrong. Here is the new code:


    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <cstdlib>
    #include <fstream>
    using namespace std; 
    
    
    class Inventory{ 
          private:
                  int itemNumber;
                  int quantity;
                  double cost;
                  double totalCost;
          public:
                 Inventory ()
                 {
                           
                            itemNumber = 0;
                            quantity = 0;
                            cost = 0;
                            totalCost = 0;
                 }
                 Inventory ( int n, int c, int q )
                 {
                            itemNumber = n;
                            quantity = q;
                            cost = c;
                            totalCost = setTotalCost();
                 }
                 void setItemNumber ( int number )
                 {
                            itemNumber = number;
                 }
                 void setQuantity ( int quant )
                 {
                            quantity = quant;
                 }
                 void setCost ( double cst )
                 {
                            cost= cst;
                 }
                 double setTotalCost ()
                 {
                            totalCost = quantity * cost;
                            return totalCost;
                 }
                 int getItemNumber ()
                 {
                            return itemNumber;
                 }
                 int getQuantity () 
                 {
                            return quantity;
                 }
                 double getCost ()
                 {
                            return cost;
                 }
                 double getTotalCost ()
                 {
                            return totalCost;
                 }
                 void hammers()
                 {
                        Inventory hammer;
                        int it_number, it_quant;
                        float it_cost;
                        cout<<"\nEnter Item Number for hammers: ";
                        cin>>it_number;
                        hammer.setItemNumber(it_number);
                        cout<<"\nEnter quantity of hammers: ";
                        cin>>it_quant;
                        hammer.setQuantity(it_quant);
                        cout<<"\nEnter the cost of one hammer: ";
                        cin>>it_cost;
                        hammer.setCost(it_cost);
                        hammer.setTotalCost();
                        cin.get();
                 }
                 void wrenches()
                 {
                        Inventory wrench;
                        int it_number, it_quant;
                        float it_cost;
                        cout<<"\nEnter Item Number for wrenches: ";
                        cin>>it_number;
                        wrench.setItemNumber(it_number);
                        cout<<"\nEnter quantity of wrenches: ";
                        cin>>it_quant;
                        wrench.setQuantity(it_quant);
                        cout<<"\nEnter the cost of one wrench: ";
                        cin>>it_cost;
                        wrench.setCost(it_cost);
                        wrench.setTotalCost();
                        cin.get();
                 }
                 void saws()
                 {
                      Inventory saw;
                      int it_number, it_quant;
                      float it_cost;
                      cout<<"\nEnter Item Number for saws: ";
                      cin>>it_number;
                      saw.setItemNumber(it_number);
                      cout<<"\nEnter quantity of saws: ";
                      cin>>it_quant;
                      saw.setQuantity(it_quant);
                      cout<<"\nEnter the cost of one saw: ";
                      cin>>it_cost;
                      saw.setCost(it_cost);
                      saw.setTotalCost();
                      cin.get();
                 }   
          
    };
    
    void Clear_Screen(void);
    int main()
    {
                 Inventory hammer, wrench, saw;
                 hammer.hammers();
                 Clear_Screen();
                 wrench.wrenches();
                 Clear_Screen();
                 saw.saws();
                 Clear_Screen();
                 cout<<"________Item Number_______Quantity_______Cost____________Total Cost___________" << endl << endl;
                 cout<< setw(14) <<hammer.getItemNumber() << setw(16) << hammer.getQuantity() << setw(15)
                 << hammer.getCost() << setw(20) << hammer.getTotalCost()<< endl;
                 cout << endl;
                 cout<< setw(14) <<wrench.getItemNumber() << setw(16) << wrench.getQuantity() << setw(15)
                 << wrench.getCost() << setw(20) << wrench.getTotalCost()<< endl;
                 cout << endl;
                 cout<< setw(14) <<saw.getItemNumber() << setw(16) << saw.getQuantity() << setw(15)
                 << saw.getCost() << setw(20) << saw.getTotalCost()<< endl;
                 cout << "\n\n   Thank you for using the program!" << endl;
                 cout << "\n\n   Press any key to exit" << endl;
                 getchar();
                 return 0;
    }
    
    void Clear_Screen(void)
    {
       #ifdef _WIN32
          system("cls");
       #else
          system("clear");
       #endif
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
                 void hammers()
                 {
                        Inventory hammer;
    You are working with the local var hammer instead of this, after you exit the function your hammer var is destroyd
    and your this object is leaved intact.

    Also - you SetCost and setQuantity do not update totalCost - leaving place for bugs - user can not call your setTotalCost function leaving the object in the inconsistent state -
    better remove the setTotalCost and totalCost members - calculate the totalCost value on the fly in the GetTotalCost function using cost and quantity members
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    This is the C Board, you want the C++ Board. Can a mod move this?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Right, moved to the C++ programming forum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    36
    Sorry, I realized this was the wrong board the second I posted. Apparently, I can't delete or move?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you have a generic inventory class, it seems pretty limiting that each type of item should have it's own member function. Imagine that you are responsible for the inventory software for a large supermarket, that has 15 different kinds of Milk (variying amount of fat, large and small sizes of container, organic or not-organic, and so on), never mind all the other products that a supermarket would stock. And someone says "We need to add a new kind of milk" - you wouldn't want to have to write a new function for that, would you?

    Instead, I suggest that you make your class use a parameter that states what type of item it is. This can either be part of the constructor, or part of the (now generic) input function that reads in the rest of the data to the inventory item.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    36
    Thanks for your help, but I still dont know how to make the generic input/output member functions and retain all the info for later display. And do I call the member functions just like a normal function? Do I need to make an object to call them?
    Last edited by veronicak5678; 03-04-2008 at 05:17 PM.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You already have an Inventory object per item, so all you need is some way to "name" the item. You have two choices: Either you set the name as part of the construction (a string parameter to the constructor), or you pass it along as you ask for the data to be input. In either case, you should store the name as part of the object, and then use a getter function to retrieve it for printing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    36
    I'm not sure I understand what you mean. I see how it would work if I named them, but how can I do that? Here's what i have been doing:


    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <cstdlib>
    #include <fstream>
    using namespace std; 
    
    class Inventory{ 
          private:
                 int itemNumber;
                 int quantity;
                 double cost;
                 double totalCost;
          public:
                 Inventory ()
                 {
                         itemNumber = 0;
                         quantity = 0;
                         cost = 0;
                         totalCost = 0;
                 }
                 Inventory ( int n, int c, int q )
                 {
                         itemNumber = n;
                         quantity = q;
                         cost = c;
                         totalCost = getTotalCost();
                 }
                 void Inventory::setItemNumber ( string name )
                 {
                         cout<<"\nEnter Item Number for: " << name << endl;
                         cin>>itemNumber;
                  
                 }
                 void Inventory::setQuantity ( string name )
                 {
                         cout<<"\nEnter quantity of: " << name << endl;
                         cin>>quantity;
                 }
                 void Inventory::setCost ( string name )
                 {
                         cout<<"\nEnter the cost of one: " << name << endl;
                         cin>> cost;
                 }
                 int getItemNumber () const
                 {
                            return itemNumber;
                 }
                 int getQuantity () const
                 {
                            return quantity;
                 }
                 double getCost () const
                 {
                            return cost;
                 }
                 double getTotalCost () 
                 {
                            totalCost = quantity * cost;
                            return totalCost;
                 }  
    };
    
    void Clear_Screen(void);
    
    int main()
    {
                 Inventory hammer, wrench, saw;
                 string name[]={"hammer", "wrench", "saw"};
                 hammer.Inventory::setItemNumber(name[0]);
                 hammer.Inventory::setQuantity(name[0]);
                 hammer.Inventory::setCost(name[0]);
                 hammer.Inventory::getTotalCost();
                 Clear_Screen();
                 wrench.Inventory::setItemNumber(name[1]);
                 wrench.Inventory::setQuantity(name[1]);
                 wrench.Inventory::setCost(name[1]);
                 wrench.Inventory::getTotalCost();
                 Clear_Screen();
                 saw.Inventory::setItemNumber(name[2]);
                 saw.Inventory::setQuantity(name[2]);
                 saw.Inventory::setCost(name[2]);
                 saw.Inventory::getTotalCost();
                 Clear_Screen();
                 cout<<"________Item Number_______Quantity_______Cost___________Total Cost___________" << endl << endl;
                 cout<< setw(14) <<hammer.getItemNumber() << setw(16) << hammer.getQuantity() << setw(15)
                 << hammer.getCost() << setw(20) << hammer.getTotalCost()<< endl;
                 cout << endl;
                 cout<< setw(14) <<wrench.getItemNumber() << setw(16) << wrench.getQuantity() << setw(15)
                 << wrench.getCost() << setw(20) << wrench.getTotalCost()<< endl;
                 cout << endl;
                 cout<< setw(14) <<saw.getItemNumber() << setw(16) << saw.getQuantity() << setw(15)
                 << saw.getCost() << setw(20) << saw.getTotalCost()<< endl;
                 cin.get(); cin.get();
                 cout << "\n\n   Thank you for using the program!" << endl;
                 cout << "\n\n   Press any key to exit" << endl;
                 getchar();
                 return 0;
    }
    
    void Clear_Screen(void)
    {
       #ifdef _WIN32
          system("cls");
       #else
          system("clear");
       #endif
    }
    I don't know if this is what he means by asking me to use member functions for input.
    Last edited by veronicak5678; 03-04-2008 at 10:26 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. member functions can't see private data members
    By ichijoji in forum C++ Programming
    Replies: 2
    Last Post: 11-22-2006, 02:36 PM
  2. issue with member functions
    By Chaplin27 in forum C++ Programming
    Replies: 4
    Last Post: 09-27-2006, 09:18 PM
  3. Classes and member functions
    By uraliss in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2006, 07:38 AM
  4. Class member variables only readable by member functions?
    By _Elixia_ in forum C++ Programming
    Replies: 4
    Last Post: 10-10-2003, 03:52 PM
  5. trouble defining member functions
    By dP munky in forum C++ Programming
    Replies: 7
    Last Post: 04-13-2003, 08:52 PM