Hi ive made a code which does two things. First is read from a separate .dat file a product's model number, name a proce. The second bit allows the user to choose the product model. Depending on what the user chooses, only 1 of the products is displayed. So for example if the user inputs 1. Then the program shows information about product 1(model number name price).

here is my code. it works

#include <iostream>
#include <fstream>
using namespace std;

class product
{


      public:
      int p;
      int model;
      string item;
      int price;
      void show();

      };

class product2
{
      public:
      void display();
      friend class product; 
      };

void product::show()
{
    product arrayp[10];
    ifstream infile;
    infile.open("C:\\products.dat");
  for(int p=0; p<10; p++)
  {
       infile
       >>arrayp[p].model
       >>arrayp[p].item
       >>arrayp[p].price;
      }
    infile.close();
   for(int p=0; p<3; p++)
  {
  cout
  <<arrayp[p].model<<" "
  <<arrayp[p].item<<" "
  <<arrayp[p].price<<"\n";
  } 
cout<<"choose your product:"<<endl;
cin>>p;
cout<<"You have chosen: "<<endl;
cout
  <<arrayp[p].model<<" "
  <<arrayp[p].item<<" "
  <<arrayp[p].price<<"\n";
}

void product2::display()
{

}

int main()
{
  product prod;
  product2 pro;
  prod.show();
  pro.display();
  system("pause");
  return 0;
} 

however i would like to split this into 2 classes. one for reading the items(product) and one for user input and display (product2). i am using friend classes but when i move the "choose product..." over it does not work.
any help is appreciated.

here is the code for products.dat

0 Book 20
1 Pencil 5
2 Pen 10

in short. class product shows all the products. the second class product2 lets user choose one.

Recommended Answers

All 2 Replies

I'm not positive, but it sounds like you're unclear on classes, and "object-oriented-ness" in general. For a case as simple as yours, an Object can be thought of simply as an abstraction of a real world object. Specifically a "product". Objects contain their attributes ("members") and most basic actions ("methods"). I don't know why you'd want to have a separate class to display a product. It should encapsulate its member-attributes (product number, written description, and price) and its abilities (creating/destroying instance objects -- constructor/destructor, displaying an instance's attributes, possibly even entering values from a user or a static-class-method to create a list of objects from the data file.

The part of the program where the user chooses a product is not part of the product class, nor is it part of some parallel "product2" class. You will undoubtedly get into the Model/View/Controller paradigm for building interfaces at some point (and probably others as well), but that's well beyond this assignment. Instead, it can just be part of the "main" program.

So whether you include it in the product class or not, you need a function which can read your .dat file and create a list of Product instances, and you need a loop where the user can enter something, and then the proper product will be found in the list. You may want to wrap the list up in its own class called ProductList with methods like findProductWithNumber(int product_number) or you could even have a findUserSpecifiedProduct() method which includes prompting the user and getting the input, though that makes your class dependent on a terminal-oriented user interface.

I hope this helps you sort out what functionality you want where in your code!

i see. i guess ill add the file part to the main function and ill ignore product2 class. thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.