Derived Class ??

Pages: 12
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();
   
};


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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <time.h>
#include "product.h"
#include "order.h"

void product::getproducts(void)
{
   cout << "Enter Product Name : ";
  	cin.getline(product_name,20);
   cout << "Enter Product Code : ";
  	cin >> product_code;
	cout << "Enter Unit Price : ";
  	cin >> unit_price;
   cin.ignore();

}

float product::getprice(void)
{
	return unit_price;
}

void product::displayproducts(void)
{
  	cout << "Product Name : " << product_name;
  	cout << "\nProduct Code : " << product_code;
  	cout << "\nUnit Price : " << unit_price;
}

void order::getorder(void)
{
   cout << "Enter Order No : ";
   cin >> order_no;
   cout << "Enter Product Code : ";
   cin >> product_code;
   cout << "Enter Quantity : ";
   cin >> quantity;
}

float order::totalprice(float a, int b)
{
        return a*b;
}

void order::invoice(product p)
{
    char dateStr [9];
    cout << "\n";
    line();
    cout << "\n|" << " ABC Company Invoice       Date: " << _strdate(dateStr) << " |\n";
    line();
    cout << "\n|" << " Order No                  |      " << order_no << "       |" ;
    cout << "\n|" << " Product code              |   " << product_code << "       |" ;
    cout << "\n|" << " Quantity " << quantity << "  @ Unit Price  |         ***  |\n";
    line();
    cout << "\n|" << " Total price               |         ***  |\n";
    line();
}

void order::line(void)
{
 	for(int x=0;x<44;x++)
   	cout << "-";
}

main()
{
	product P[3];  //object of type product
   order O[3]; // object of type order

	for(int i=0;i<3;i++)
    {
    	cout << "\nEnter Product " << (i+1) << " Information";
      cout << "\n--------------------------\n";
    	P[i].getproducts();
    }

   cout<<"\n";
	for(int i=0;i<3;i++)
   {
    	cout << "\n\nProduct P" << (i+1);
      cout << "\n---------\n";
    	P[i].displayproducts();
   }

   cout<<"\n";
   for(int i=0;i<3;i++)
    {
    	cout << "\nEnter Order Information " << (i+1);
      cout << "\n------------------------\n";
    	O[i].getorder();
    }

   cout<<"\n";
   for(int i=0;i<3;i++)
   {
    	cout << "\n\nOrder No: " << (i+1);
      cout << "\n---------\n";
    	O[i].invoice();
   }
 	getche();
}


I would greatly appreciate help if someone could give me a hint as to how I could go about solving this problem.

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.
I agree that this does not seem very logical as it would be very odd for an order to contain only 1 product at a time.
Re-read my reply. Having a class that contains a list of products with an interface to query it still is applicable to your problem.

e.g
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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?
Last edited on
I have changed it to a derived class now but I am still not getting the correct value showing. New code is as follows:

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
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <math.h>


class product
{
   char product_name[20];
   int product_code;
   public:
   float unit_price;
   float getunit();
   float getprice();
   void getproducts();
   void displayproducts();

};

class order: public product
{
   int order_no;
   int product_code;
   int quantity;
   public:
   float totalprice(float x, int y);
   void invoice();
   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
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <time.h>
#include "derived.h"

void product::getproducts(void)
{
  cout << "Enter Product Name : ";
  cin.getline(product_name,20);
  cout << "Enter Product Code : ";
  cin >> product_code;
  cout << "Enter Unit Price : ";
  cin >> unit_price;
  cin.ignore();

}

float product::getprice(void)
{
	return unit_price;
}

void product::displayproducts(void)
{
  	cout << "Product Name : " << product_name;
  	cout << "\nProduct Code : " << product_code;
  	cout << "\nUnit Price : " << unit_price;
}

void order::getorder(void)
{
   cout << "Enter Order No : ";
   cin >> order_no;
   cout << "Enter Product Code : ";
   cin >> product_code;
   cout << "Enter Quantity : ";
   cin >> quantity;
}

float order::totalprice(float a, int b)
{
        return a*b;
}

void order::invoice()
{
    char dateStr [9];
    cout << "\n";
    line();
    cout << "\n|" << " ABC Company Invoice       Date: " << _strdate(dateStr) << " |\n";
    line();
    cout << "\n|" << " Order No                  |      " << order_no << "       |" ;
    cout << "\n|" << " Product code              |   " << product_code << "       |" ;
    cout << "\n|" << " Quantity " << quantity << "  @ Unit Price  |         ***  |\n" << getprice();
    line();
    cout << "\n|" << " Total price               |         ***  |\n";
    line();
}

void order::line(void)
{
 	for(int x=0;x<44;x++)
   	cout << "-";
}

main()
{
	product P[3];  //object of type product
   order O[3]; // object of type order

	for(int i=0;i<3;i++)
    {
    	cout << "\nEnter Product " << (i+1) << " Information";
      cout << "\n---------------------------\n";
    	P[i].getproducts();
    }

   cout<<"\n";
	for(int i=0;i<3;i++)
   {
    	cout << "\n\nProduct P" << (i+1);
      cout << "\n---------\n";
    	P[i].displayproducts();
   }

   cout<<"\n";
   for(int i=0;i<3;i++)
    {
    	cout << "\nEnter Order Information " << (i+1);
      cout << "\n------------------------\n";
    	O[i].getorder();
    }

   cout<<"\n";
   for(int i=0;i<3;i++)
   {
    	cout << "\n\nOrder No: " << (i+1);
      cout << "\n---------\n";
    	O[i].invoice();
   }
 	getche();
}


Where the unit price is supposed to be displayed it is returning what I can only assume is a memory location: 5.94446e-39,5.96386e-39, 5.87747e-39
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 changed the all the methods of both classes to public but am still being shown the scientific notation when trying to display the unit price
Any ideas on where I am going wrong?
You are probably not initializing the data, are you sure you changed your program so you are putting the data into an order instead of a product?
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:

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

#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <math.h>


class product
{


   public:
   /*
   product(char pn[30], int pc, float up)
   {
    	pn = product_name;
      pc = product_code;
      up = unit_price;
   }
   */
   char product_name[20];
   int product_code;
   float unit_price;
   float getunit();
   float getprice();
   void getproducts();
   void displayproducts();

};

class order: public product
{

   public:                        
   int order_no;
   int product_code;
   int quantity;
   float totalprice(float x, int y);
   void invoice();
   void getorder(void);
   void line(void);
};


You are saying that I do not need the product class now but what if I need to add another product?
Last edited on
The products are being initialised as they were from the start as I am unsure how I would go about doing them as base classes of orders.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

void product::getproducts(void)
{
   cout << "Enter Product Name : ";
   cin.getline(product_name,20);
   cout << "Enter Product Code : ";
   cin >> product_code;
   cout << "Enter Unit Price : ";
   cin >> unit_price;
   cin.ignore();

}

void product::displayproducts(void)
{
  	cout << "Product Name : " << product_name;
  	cout << "\nProduct Code : " << product_code;
  	cout << "\nUnit Price : " << unit_price;
}
Last edited on
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.

This has good info:

http://www.cplusplus.com/doc/tutorial/inheritance.html
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)
The unit price is held in product class as a float value and not a string so I am unsure on what it is I would need to convert
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.

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

#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <math.h>


class product
{

   public:
   /*
   product(char pn[30], int pc, float up)
   {
    	pn = product_name;
      pc = product_code;
      up = unit_price;
   }
   */
   char product_name[20];
   int product_code;
   float unit_price;
   void getproducts();
   void displayproducts();

};

class order: public product
{

   public:                        
   int order_no;
   int product_code;
   int quantity;
   float getUnit(int a);
   float totalprice(float x, int y);
   void invoice();
   void getorder(void);
   void line(void);
};


And the main program is:

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <time.h>
#include "derived.h"

void product::getproducts(void)
{
   cout << "Enter Product Name : ";
  	cin.getline(product_name,20);
   cout << "Enter Product Code : ";
  	cin >> product_code;
	cout << "Enter Unit Price : ";
  	cin >> unit_price;
   cin.ignore();

}

void product::displayproducts(void)
{
  	cout << "Product Name : " << product_name;
  	cout << "\nProduct Code : " << product_code;
  	cout << "\nUnit Price : " << unit_price;
}

void order::getorder(void)
{
   cout << "Enter Order No : ";
   cin >> order_no;
   cout << "Enter Product Code : ";
   cin >> product_code;
   cout << "Enter Quantity : ";
   cin >> quantity;
}

float order::totalprice(float a, int b)
{
        return a*b;
}

void order::invoice()
{
    char dateStr [9];
    cout << "\n";
    line();
    cout << "\n|" << " ABC Company Invoice       Date: " << _strdate(dateStr) << " |\n";
    line();
    cout << "\n|" << " Order No                  |      " << order_no << "       |" ;
    cout << "\n|" << " Product code              |   " << product_code << "       |" ;
    cout << "\n|" << " Quantity " << quantity << "  @ Unit Price  |         ***  |\n" << (float)unit_price;
    line();
    cout << "\n|" << " Total price               |         ***  |\n";
    line();
}

void order::line(void)
{
 	for(int x=0;x<44;x++)
   	cout << "-";
}

main()
{
	product P[3];  //object of type product
   order O[3]; // object of type order

	for(int i=0;i<3;i++)
    {
    	cout << "\nEnter Product " << (i+1) << " Information";
      cout << "\n---------------------------\n";
    	P[i].getproducts();
    }

   cout<<"\n";
	for(int i=0;i<3;i++)
   {
    	cout << "\n\nProduct P" << (i+1);
      cout << "\n---------\n";
    	P[i].displayproducts();
   }

   cout<<"\n";
   for(int i=0;i<3;i++)
    {
    	cout << "\nEnter Order Information " << (i+1);
      cout << "\n------------------------\n";
    	O[i].getorder();
    }

   cout<<"\n";
   for(int i=0;i<3;i++)
   {
    	cout << "\n\nOrder No: " << (i+1);
      cout << "\n---------\n";
    	O[i].invoice();
   }
 	getche();
}


The problem is in this piece of code as it is not able to display the unit price of the product entered.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

void order::invoice()
{
    char dateStr [9];
    cout << "\n";
    line();
    cout << "\n|" << " ABC Company Invoice       Date: " << _strdate(dateStr) << " |\n";
    line();
    cout << "\n|" << " Order No                  |      " << order_no << "       |" ;
    cout << "\n|" << " Product code              |   " << product_code << "       |" ;
    cout << "\n|" << " Quantity " << quantity << "  @ Unit Price  |         ***  |\n" << (float)unit_price;
    line();
    cout << "\n|" << " Total price               |         ***  |\n";
    line();
}
The problem is you are not putting the product data into order, I would suggest modifying the getorder() to get all the data you want.

(As of now it is still only getting the data in order, you need to get the data inside of product for it as well)
Last edited on
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.
Pages: 12