7 Replies - 724 Views - Last Post: 27 January 2009 - 11:25 AM Rate Topic: -----

#1 sramirez3585  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 29-September 08

Working with classes

Posted 25 January 2009 - 05:30 PM

Hello
ok lets see where to even start. I had a an assignment to write an inventory program using structures and functions. It is the code below:
#include <stdio.h>
#include <iostream>

using namespace std;

struct ProductInfo
	{

		char pro_name[51];
		char pro_desc[51];
		char upc[13];
		int  inventory;

	};

void addProduct(ProductInfo &);
void showProduct(ProductInfo);

int main()
{

  ProductInfo pro;
  int size=0;

  cout<< "How many products would you like to enter? \n";
  cin>>size;

	if (size == 0)return;
	  for (int i=0; i<size; i++)
	  {
		cout<< "Enter the data for product " << (i+1) << endl;
		addProduct(pro);
	  }
			 
			   for (int i=0; i<size;i++)
	showProduct(pro);
return 0;
}

void addProduct (ProductInfo &pro)
  {
	cout<< "Enter product name: \n";
	cin.ignore ();
	cin.getline(pro.pro_name, 51);

	cout<< "Enter a description for the product: \n";
	cin.ignore ();
	cin.getline(pro.pro_desc, 51);

	cout << "Enter product UPC: \n";
	cin.ignore ();
	cin.getline(pro.upc, 13);

	cout << "Enter the amount of products in stock: \n";
	cin >> pro.inventory;
	return;
  }

void showProduct (ProductInfo pro)
  {
  //  cout<< fixed << showpoint << setprecision(2);
	cout << "Product Name :" << pro.pro_name << endl;
	cout << "Product Description :" << pro.pro_desc << endl;
	cout << "Product UPC :" << pro.upc << endl;
	cout << "Product in stock :" << pro.inventory << endl;

  }



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.

Is This A Good Question/Topic? 0
  • +

Replies To: Working with classes

#2 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Working with classes

Posted 25 January 2009 - 06:35 PM

Classes and Structures are identical, in almost every aspect.
Classes have "more" options than a Structure though, if you want a simple dumbed down idea of the differences.

www.google.com is your friend! :)
Was This Post Helpful? 0
  • +
  • -

#3 Linkowiezi  Icon User is offline

  • D.I.C Regular

Reputation: 58
  • View blog
  • Posts: 316
  • Joined: 07-October 08

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:
struct ProductInfo
    {

        char pro_name[51];
        char pro_desc[51];
        char upc[13];
        int  inventory;

    };

So it looks like this:
//  Changed 'struct' to 'class'
class ProductInfo
    {
        public:  //  Added public declaration here
        char pro_name[51];
        char pro_desc[51];
        char upc[13];
        int  inventory;

    };

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;

}


Hope this will get you on the right track :)
Was This Post Helpful? 1
  • +
  • -

#4 sramirez3585  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 29-September 08

Re: Working with classes

Posted 26 January 2009 - 11:41 AM

thank you both very much!
Was This Post Helpful? 0
  • +
  • -

#5 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Working with classes

Posted 26 January 2009 - 12:31 PM

Welcome.
Was This Post Helpful? 0
  • +
  • -

#6 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Working with classes

Posted 26 January 2009 - 02:14 PM

View PostHyper, on 26 Jan, 2009 - 01:35 AM, said:

Classes have "more" options than a Structure though
More options such as what? There's just one difference between struct and class. the default access specifier for a struct is public. Other than that, they are completely identical
Was This Post Helpful? 0
  • +
  • -

#7 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Working with classes

Posted 26 January 2009 - 03:55 PM

virtual
protected
friend
etc... If I'm not mistaken.
Was This Post Helpful? 0
  • +
  • -

#8 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

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.
struct mystruct
{
private:
    int n;
protected:
    virtual void foo() {}
public:
    mystruct() : n(0) {}
    friend int bar();
}; 

This post has been edited by Bench: 27 January 2009 - 11:26 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1