-
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
}
-
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
-
This is the C Board, you want the C++ Board. Can a mod move this?
-
Right, moved to the C++ programming forum.
-
Sorry, I realized this was the wrong board the second I posted. Apparently, I can't delete or move?
-
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
-
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?
-
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
-
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.