I am working on a problem which will work with two classes which I have created "product" and "order". The product class will have attributes of name,code and price. The order class will have attributes of order_no,product code, and quantity aswell as a method to calculate the total price which is where I am encountering difficulties. I need a way for the order class to check the price of the product once a product number is entered so that I can display it on the order invoice.
I have been told that one way to sort this out may be with the use of a derived class but I am unsure about how to do this. My code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <math.h>
class order
{
int order_no;
int product_code;
int quantity;
public:
float totalprice(float x, int y);
void invoice(void);
void getorder(void);
void line(void);
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class product
{
char product_name[20];
int product_code;
float unit_price;
public:
/*
product(char pn[30], int pc, float up)
{
pn = product_name;
pc = product_code;
up = unit_price;
}
*/
float getprice();
void getproducts();
void displayproducts();
};
Are you familiar with databases? And the N-N relationship?
You've broken this relationship by enforcing a N-1 relationship. In your application each order can only have 1 product, and each product can appear in many orders. You want each order to have many products. So the design for your order is off.
2nd. You need to put your products somewhere and provide an interface to search for a product and return a handle to it. This will allow your orders to hold handles to products that they can query for information (e.g price, name etc).
At this point in time, I don't think you need a derived class. Perhaps a management class that stores an array (vector) of products and exposes a mechanism for your orders to query for products.
Hi Zaita thank you for your reply. I understand what you are saying regards the N-N relationship but this is the task that I was given for next week. Each order can have only 1 product as I am required to display the invoice as in the displayed in the order::invoice method.
The task I was given was worded as follows:
Let us consider an example of an inventory control of products in a store. One way of recording the details of the products is to record their names, code number, total items in the stock and the price of each item.
Data
Product_name
Product_code
Unit_Price
Methods
GetProducts()
DisplayProducts()
(i) Define a class called ‘product’ with data items product_name, product_code, and Unit_price. Save the class as a header file, i.e. **.h.
(ii) Define a constructor with parameter for dynamic initialization and define the methods.
(iii) Write a main program to test the product system. Use the header file for the class defined in (i).
The above system has to be upgraded by using the sales information such as
Data
Order_no
Product_code
Quantity
Methods
Total_price() calculated as Unit_Price*Quantity
Invoice() shown as below
should be included in the upgraded system.
(iv) Define a derived class called ‘Order’ which will have the members.
(v) Define a method for displaying an invoice of an order as follows
------------------------------------------------------
| ABC Company Invoice Date: |
------------------------------------------------------
| Order no | |
| Product code | |
| Quantity @ Unit_price | *** |
------------------------------------------------------
| Total price | *** |
------------------------------------------------------
(vi) Write a main program to test the product and the order system. Use the header file for the class defined in (v). In this case, reuse the software developed last week.
class Product {
int price;
}
class Order {
public:
Product *pProduct;
};
class ProductManager {
public
vector<Product> vProductList;
product* getProduct(string name) { };
}
int main() {
Order myOrder = Order();
myOrder.pProduct = ProductManager.getProduct("coke");
cout << myOrder.pProduct.price;
}
Obviously that code is incomplete etc, but it gives a pseudo example of what I was explaining.
I can understand what you are saying in regards to the relationships but I am not sure what the above code means. Why do I need a vector to store the products, isnt the array of products already doing this job?
No, that's scientific notation...it's because you only need to make objects of type order now, and they will have the variables that product has. Although, you do need to go through and make all the methods of the class public.
I have to have them as two seperate classes, order being derived from the base class product. When I enter the product number into the order I have to find someway of referencing the unit price of the corresponding product number so that it can be display with the order::invoice method.
The current code that I have for the classes is now:
Basically, you would just create a product array like you are, and since they are derived from the base class order, they will have all the variables that order had, plus whatever you told them to have.
I have followed the syntax in the tutorial above but am still unable to return a value for the unit price, it is still returning the scientific notation mentioned last night
Are you sure you are setting the variable? It could also be a problem with the way cin is handling the string with a "." in it...try converting the string to a double (you might have to do some searching for this)
I am not sure if this problem has something to do with my compiler as I cannot see anything wrong with it syntax wise and it is seeming to compile without any problems the only one being the display of the scientific notification instead of a float value.
I would be very grateful if you would compile the two pieces of code below on your computer and see if you too are getting these same values as I can see no reason as to why I should not be allowed access to the unit price held with the product class if it is derived from the order class.
This is the "derived.h" header file called in the main program.
I am not sure that I am supposed to be putting the data of the product into the order class. I believe that I should be referencing the product class once the user has selected the product code on the getorder() method.