Parameterized constructors

This is a discussion on Parameterized constructors within the C++ Programming forums, part of the General Programming Boards category; Ok guys, need a little help. I'm buidling 3 objects below and I need to let the user initialize them ...

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

    Parameterized constructors

    Ok guys, need a little help. I'm buidling 3 objects below and I need to let the user initialize them to certain values. How can I keep the quantity from erasing everytime without building separate cells? To give you an idea of what I need to do.

    Code:
    class Tools
    {
        Tools( int,  float );
    };
    
    Tools::Tools( int num_items, float cost)
    {
         quantity = num_items;
         price = cost;
    }
    
    /************************************************
    Tools saws;
    Tools hammers;
    Tools nails;
     
    cout << "Please enter the outstanding quantity of saws ";
    cin >> num_items; 
    cout << endl;
    
    cout << "Please enter the outstanding price of each saw";
    cin >> cost; 
    cout << endl;
    
    Tools Tools(num_items, cost); //Should I call the constuctor here?
    	
    cout << "Please enter the outstanding quantity of hammers";
    cin >> num_items; 
    cout << endl;
    
    cout << "Please enter the outstanding price of each hammer";
    cin >> cost; 
    cout << endl;
    
    cout << "Please enter the outstanding quantity of nails";
    cin >> num_items; 
    cout << endl;
    
    cout << "Please enter the outstanding price of box of nails";
    cin >> cost;
    cout << endl;
    
    ***********************************************/
    So I need each object to hold a different quantity (num_items) and price (cost). How can I accomplish this?

    Thanks in advance for any help.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    when ye' declare new objects of your class type.. you will have the opportunity to use the power o' the parameterized constuctor:

    Code:
    Tools saws(1, 3.643);       //New objects are initialized on creation
    Tools hammers(2, .096);     //by a parameterized constructor
    Tools nails(10, 3.141);
    Last edited by The Brain; 11-04-2005 at 05:41 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Move the declarations of your three variables until after you have the data available to initialize them, then use the parameterized constructor as you did.
    Code:
    Tools saws(num_items, cost);
    In C++ you should only declare variables when you are ready to initialize and use them anyway.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    20
    Thanks for all the help.

    Is there any way I could go about doing a total sum of the inventory of these items simply? For example, if I had a function to find the dollar value of each item and then a separate function to find the total dollar value of the combined items? Say

    Code:
    void Tools::Calculate_each()
    {
    	cout << quantity * price;
    }
    
    float Tools::Calculate_total()
    {
    	cout << "The total value of inventory for all items combined is ";  
                         ???
    }
    If the parameterized constuctor holds these values,

    SAWS
    quantity 100
    price $10.00 --->Total value : $1000.00

    HAMMERS
    quantity 200
    price $5.00 ---> Total value : $1000.00

    NAILS 300
    price $2.50 ---> Total value : $750.00

    How would I end up with Total inventory : $2750.00

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    Add a totalPrice() function to your Tools class. It will calculate the total inventory price for one Tools object. Calculating the grand total of all the Tools is then a matter of calling the totalPrice() function on each of your Tools objects and adding them up.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    20
    7stud, thanks for the help. How would I go about adding them up though? I can't use saws.totalPrice() + hammers.totalPrice() + nails.totalPrice() right? Please enlighten a novice.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Your idea should work, I think.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    How would I go about adding them up though? I can't use saws.totalPrice() + hammers.totalPrice() + nails.totalPrice() right?
    Why not? What is saws.totalPrice()? When you call a function the funtion call is replaced by the return value of the function. So, if you have this statement:
    Code:
    double grand_total = saws.totalPrice() + hammers.totalPrice() + nails.totalPrice();
    then saws.totalPrice() is replaced by the number returned from the function, hammers.totalPrice() is replaced by a number, and nails.totalPrice() is replaced by a number, producing the statement:

    double grand_total = 1000.00 + 1000.00 + 750.00;
    Last edited by 7stud; 11-05-2005 at 02:33 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy constructors; Best practices (and private)
    By Mario F. in forum C++ Programming
    Replies: 15
    Last Post: 06-23-2006, 04:42 PM
  2. Arrays and Constructors
    By Verdagon in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2005, 07:20 PM
  3. constructors, arrays, and new
    By Thantos in forum C++ Programming
    Replies: 6
    Last Post: 05-30-2004, 06:21 PM
  4. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 09:16 AM
  5. Default Constructors
    By MethodMan in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2002, 06:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21