Thread: C++ Classes & Function Assignment please help!

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    1

    Question C++ Classes & Function Assignment please help!

    Hi! I am EXTREMELY new to C++ and I wish to ask for some help on my latest assignment. I do not really know where to begin. Any help is greatly appreciated!!
    This is the assignment:
    Using the variables created for the application, create a class object called Inventory. Our variables should be private within that class. In addition, set up public member functions for setting and getting the values for those private variables. There should be two constructors: a default constructor to set all values to zero or empty (for string variables), and a parameter constructor for setting the values when declared.
    The class will also include functions for calculating the total value of the inventory and for generating the random number for the SKU.
    We will still be using a loop to ask the user for the category, the item, the price, and the number in stock, so all that is required is the modifications in variables. Validation for the input should be within the class.
    The output will remain as we have previously done it, so, again, modifications are needed for the variables only.

    This is the code I have so far, I do not know where to begin:
    Code:
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    #include <time.h>
    #include <iomanip>
    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 newItemNumber, int newQuantity, double newCost)
    {
      itemNumber = newItemNumber;
      quantity = newQuantity;
      cost = newCost;
      setTotalCost(quantity, cost); // this can be called as quantity and cost have now been initialized.
    }
    
    
            void setItemNumber(int)
            {
                itemNumber = itemNumber;
            }
            void setQuantity(int)
            {
                quantity = quantity;
            }
            void setCost(double)
            {
                cost = cost;
            }
            void setTotalCost(int, double)
            {
                totalCost = quantity * cost;
            }
    
    
            int getItemNumber()
            {
                return itemNumber;
            }
            int getQuantity()
            {
                return quantity;
            }
            double getCost()
            {
                return cost;
            }
            double getTotalCost()
            {
                return totalCost;
            }
    };
    
    
    
    
        int main()
    {
        int itemNumber;
        int quantity;
        double cost;
        double totalCost;
    
    
        cout << "Enter the Item Number: ";
        cin >> itemNumber;
        while (itemNumber < 0)
        {
            cout << "Please enter a positive value for the Item Number: ";
            cin >> itemNumber;
        }
        cout << "Enter the Quantity of the item: ";
        cin >> quantity;
        while (quantity < 0)
        {
            cout << "Please enter a positive value for the Quantity of the item: ";
            cin >> quantity;
        }
        cout << "Enter the Cost of the item: ";
        cin >> cost;
        while (cost < 0)
        {
            cout << "Please enter a positive value for the Cost of the item: ";
            cin >> cost;
        }
    
    
        Inventory information(itemNumber, quantity, cost);
    
    
        totalCost = information.getTotalCost();
        itemNumber = information.getItemNumber();
        cost = information.getCost();
        quantity = information.getQuantity();
        cout << "The Item Number is: " << itemNumber << endl;
        cout << "The Quantity is: " << quantity << endl;
        cout << "The Cost is: " << cost << endl;
        cout << "The Total Cost is: " << totalCost << endl;
    
    
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Apart from
    > generating the random number for the SKU.
    and
    > Validation for the input should be within the class.
    you seem to be pretty much there.

    That's hardly "I don't know where to begin".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Your get and set functions totally expose your "private" variables. They may as well just be public. At any rate, you bizarrely haven't given names to the parameters!

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I would remove this line
    Code:
    double totalCost;
    And, would add a method the returns the calculated totalCost.

    NOTE: This change implies removing setTotalCost.

    And, you need to name the parameters passed to the other set methods.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copy assignment of polymorphic classes
    By skewray in forum C++ Programming
    Replies: 11
    Last Post: 03-19-2016, 11:32 AM
  2. Four function calculator assignment.
    By Romyo2 in forum C Programming
    Replies: 10
    Last Post: 05-09-2015, 10:24 AM
  3. Beginner. Help with a class assignment. (Function)
    By SkywardTaco in forum C++ Programming
    Replies: 3
    Last Post: 04-29-2015, 03:03 PM
  4. Function assignment problems
    By Allen Sarkisov in forum C++ Programming
    Replies: 4
    Last Post: 03-02-2014, 04:26 PM
  5. multi function assignment
    By CASHOUT in forum C Programming
    Replies: 12
    Last Post: 02-03-2013, 02:15 PM

Tags for this Thread