Now i need to modify the program to uses classes instead of structures. So my question is , the void functions that i have in the code, would they be placed in the class or outside of it? I am so lost in how to modify this. The new program also has to be menu driven but I have no problems with that.

Re: Working with classes

Posted 26 January 2009 - 03:32 AM

In C++, classes and structs are basicly the same thing.
Structs have their members declared public by default while classes have them declared private.
Private members can only be accessed by the class itself so no other part of the program is allowed to access or modify it directly.
Public members can be accessed and modified directly by any part of the program.

Just a few suggestions:
You shouldn't really have both stdio.h and iostream.
stdio.h should be used for C programs while iostream is more suited for C++.

Now to the modifying of your program, the simplest way is to change this:

I don't know if your professor will approve of this, but it works.
Your professor might want you to move the functions inside the class and use them from there.
Then you can do like this instead:

#include <iostream>
using namespace std;
class ProductInfo
{
// Members that can only be accessed by the class itself
private:
char pro_name[51];
char pro_desc[51];
char upc[13];
int inventory;
// Members that can be called from outside of the class
public:
// Constructor
ProductInfo();
// Otehr member functions
// You don't need to use pointers or variables here
// That is because they are only going to change
// variables that exist within the same class as themselves
void addProduct();
void showProduct();
};
int main()
{
// You might have to make slight changes here
// You have to add return 0 to the return in the if statement
// also you should check if size is less than 0.
// Alternativley make size and unsigned int.
// You can't for example have more than
// one(1) product right now, you should make it an array.
// Maybe not to begin with, get it to work first, then try with an array.
}
// In the constructor we set some default values for the class variables
ProductInfo::ProductInfo()
{
pro_name[0] = '\0';
pro_desc[0] = '\0';
upc[0] = '\0';
inventory = 0;
}
// Here we set user specified values to the class variables
void ProductInfo::addProduct()
{
cout<< "Enter product name: \n";
// It is good to use cin.ignore(),
// but you should only ignore newlines.
// Like this: cin.ignore(256,'\n');
// or this is even better: cin.ignore(numeric_limits<streamsize>::max(),'\n');
// If you just cin.ignore() you might eat away
// the first character that you want to use
cin.ignore ();
cin.getline(pro_name, 51);
cout<< "Enter a description for the product: \n";
cin.ignore ();
cin.getline(pro_desc, 51);
cout << "Enter product UPC: \n";
cin.ignore ();
cin.getline(upc, 13);
cout << "Enter the amount of products in stock: \n";
cin >> inventory;
return;
}
// Here we print to the screen what is stored in the class
void ProductInfo::showProduct()
{
// cout<< fixed << showpoint << setprecision(2);
cout << "Product Name :" << pro_name << endl;
cout << "Product Description :" << pro_desc << endl;
cout << "Product UPC :" << upc << endl;
cout << "Product in stock :" << inventory << endl;
}

Re: Working with classes

Posted 27 January 2009 - 11:25 AM

You can do all those things with a struct, though its less common to see structs used in an OO context, due to the 'struct' connotations which many people learned from C. Most people are likely to intuitively use struct only when they need very simple aggregate types, but the following definition is still legal.