I need someone to explain this to me because I am slightly confused.
When using public member functions I thought that all the member functions would in a way share information with each other. Take this header file I made as an example for what I am talking about.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <string>
using namespace std;
#ifndef INVENTORY_H
#define INVENTORY_H
class Inventory
{
private:
int itemNumber, quantity;
double cost, totalCost;
public:
Inventory ()
{
itemNumber = 0, quantity = 0, cost = 0, totalCost = 0;
}
Inventory (int ItemNum, int Quantity, double COST)
{
totalCost = 0;
itemNumber = ItemNum;
quantity = Quantity;
cost = COST;
}
void setItemNumber (int ItemNum)
{ itemNumber = ItemNum; }
void setQuantity (int Quantity)
{ quantity = Quantity; }
void setCost (double COST)
{ cost = COST; }
void setTotalCost ()
{ totalCost = cost * quantity; }
int getItemNumber ()
{ return itemNumber; }
int getQuantity ()
{ return quantity; }
double getCost ()
{ return cost; }
double getTotalCost ()
{ return totalCost; }
};
#endif
|
When I use getTotalCost in my main function I get a result of 0.
I am supposed to use setTotalCost to calculate the totalCost which is...
totalCost = cost * quantity;
Then I am supposed to return that value back intto main when accessing my instance of a class using the getTotalCost function.
I thought the member functions communicate with each using the private variables, so that I would be able to call getTotalCost and it would return the value that was calculated in setTotalCost.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#include <iostream>
#include <string>
#include <iomanip>
#include "Inventory.h"
using namespace std;
int main ()
{
int NewProducts;
Inventory *stock = nullptr;
int ItemNum, quantity;
double cost;
cout << "This program was written by: MisterTams ©" << endl;
cout << "____________________________________________" << endl;
cout << "At the end of the daily warehouse operations" << endl;
cout << "enter any new items that need to be added to" << endl;
cout << "our in-stock inventory if they were out-of- " << endl;
cout << "stock as of earlier today or before then....\n" << endl;
cout << "How many new different product types were added" << endl;
cout << "during receiving operations?" << endl;
cout << "\nEnter: ";
cin >> NewProducts;
stock = new Inventory [NewProducts]; //dynamically allocating an array using a pointer
for (int x = 0; x < NewProducts; x++)
{
cout << "\nNewly Stocked Item #" << x + 1 << ":" << endl;
cout << "Enter Item Stock Number: ";
cin >> ItemNum;
cout << "Enter Item Quantity: ";
cin >> quantity;
cout << "Enter Sale Price: $";
cin >> cost;
stock[x] = Inventory (ItemNum, quantity, cost);
}
cout << "\nNewly Stock Inventory in Warehouse" << endl;
cout << "___________________________________________________________________" << endl;
cout << "ITEM STOCK #" << setw(16) << "QUANTITY" << setw(19) << "COST/EA" << setw(20) << "TOTAL COST" << endl;
for (int y = 0; y < NewProducts; y++)
{
cout << setw(15) << left << stock[y].getItemNumber() << ' ';
cout << setw(12) << right << stock[y].getQuantity() << ' ';
cout << fixed << setprecision(2);
cout << setw(10) << "$";
cout << setw(8) << right << stock[y].getCost() << ' ';
cout << setw(6) << "$";
cout << setw(13) << right << stock[y].getTotalCost() << endl;
}
delete [] stock;
cout << "\nMemory Address of instance of stock object before nullptr operator: " << stock << endl;
stock = nullptr;
cout << "Memory Address of instance of stock object after nullptr operator: " << stock << endl; //experimenting with memory address to see what nullptr does
system("pause");
return 0;
}
|
if I were to change the getTotalCost member function in my header file for the class to this...
1 2
|
double getTotalCost ()
{ return totalCost = cost * quantity; }
|
Then all is fine... But my program challenge requirements, specifically state to make a member function that stores the totalCost in a function called
setTotalCost. Then to return that resulting value I would have to call getTotalCost in main. This simply doesn't work for me. What gives? Any help would be appreciated.
Thanks.