Creating related objects

Hello everyone, how u guys doing?
Im doing nice, but have some, maybe noob, good question about C++!
How im able to create a object from class product related to an object from class table?
The program is suppose to create an table to an client and to create products to that table.
Thanks!

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
#include <stdlib.h>
#include <iostream>
#include <string>

std::string NewTable;
int value;
using namespace std;

class table{
public:
	int number;
	float finalValue;
};

class product{
public:
	string product;
	float price;
	int table;
	float time;
	int date;
};

void NewTable(){
	
	cout << "Client Name\n";
	getline(std::cin,NewTable);
	table NewTable;
	cout << "Table Number\n";
	std::cin>>value;
	NewTable.number=value;

	cout << NewTable.number << endl;
}

int main(){
	NewTable();
	return 0;
}
Last edited on
It's probably pretty easy, but can you provide more information on what you're trying to do? If this is an assignment, can you post the full assignment?
the newtable function -- newtable variable (this is confusing to use the same name as the function, I advise always using unique names) destroys the newtable table variable when it exits. Just so you know.

it sounds like table needs a container of products (eg, vector<products>) inside it, from what little you gave us.
@dhayden
I trying to create a program that is able to create tables and create products as objects. Like a customer comes to my restaurant, so my program is going to create a table for him with his name and a number. My customer is going to order something to eat and something to drink. So my program is going to create two objects for that table, hamburguer and coca-cola. Each object has a price and in the end all the prices going to be sum in finalValue.
@joannin
No, i want to create an object to every item that going to be ordered and in the end sum all the values in finalValue from table.

I thought that if in every product i put the number of the table and create a method that compares the int table from product with table int number will be a way to do that. But im thinking right like that?
This appears to be an exercise in UML-like planning or some form of disciplined approach.

You can add all the bells and whistles with dates, times, reserved, paid, credit card details as you build the solution up. However here is a working start as a framework.

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
#include <iostream>
#include <string>

class Table
{
private:
    int number;
public:
    Table(){};
    Table(int no, int ns) { number = no; }
    int getNo(){ return number;}
};


class Product
{
private:
    std::string name;
    double price;
public:
    Product(){};
    Product(std::string nm, double pc){ name = nm; price = pc; }
};


class Meal
{
private:
    Table tab;
    Product* menu = nullptr; // vector much better
public:
    Meal();
    Meal(Table tbl){tab = tbl; menu = new Product[10];}
    ~Meal(){delete[] menu;}
    
    void addProduct(Product pr){ std::cout << " addProduct stub\n"; }
    
    void printBill()
    {
        std::cout
        << "Table no: " << tab.getNo() << " printBill stub\n"; }
};


int main()
{
    Table tabl_z(33, 5); // more need to be in a master list
    Product prod_x("Beans", 2.65); // more need to be in a master list
   
    Meal my_meal(tabl_z);
    
    my_meal.addProduct(prod_x);
    my_meal.printBill();
        
    return 0;
}




BTW Your use of the word create is a bit off. Tables don't create products.

Think about it carefully - Products create products - that's why you need a list of Products in order for them to be allocated to a Meal. Similarly Tables don't create Customers, in fact the customers name isn't really all that important, if at all.

Customers sit down -> order -> eat -> pay, which is arguably not even remotely an act of creation. The various acts of creation have occurred elsewhere ;)
yes its a design problem.
be careful here. you don't want to tie unrelated things together too tightly, but you have to glue it together somewhere, somehow. If you do it the wrong way, you start having to do weird stuff to make it work right, and it quickly becomes unmanageable.

eg, what does it look like if table has a container of customers, and customers have a container of products? Then if 2 guys come in, sit at table 1.... table 1 now has guy 1 and guy 2 in the customer container. Guy 1 might have combo #3 and a shake, guy 2 might have a burger, fries, and donuts. Whatever. You can add up guy 1's total bill, and guy 2, and have them pay their own. You can add that and get the total for the table. Any way you try to assemble it, this looks like it would work.

if you put the container of products to the table, that will let you get a total, for the table, but if you later decide you need to allow charging each customer at the table instead of a grand total, its messy. If you need to know what one of the customers ate, its messy. So it may solve the problem today but can't be improved for better functionality tomorrow.

That was off the cuff and its a crude, simple example but do you see how simply moving one container around made a big difference?
its an exercise for learning how to deal with OOP. I know that i need to do stuff properly right but the teacher asked to do like that xD
So, the final bill would be shared by people in the table or paid by just one people!(I think)

Doing like that will work? I mean, creating a method in product that changes finalValue in table if int table==int number? But how to create a method that will check all products numbers to see if match a selected table? I will have to create a function to do that? And how to destroy an object when its used?
IMO Jonnin's suggestion of a table has a container of customers, and customers have a container of products is the way forward.

You could even take it one level further and introduce restaurant. restaurant has a container of tables, tables has a container of customers and customers has a container of products.

I don't know what input/output is expected, but for a restaurant there are a fixed number of tables, and assuming all tables are the same - then there is a maximum number of customers per table. For each customer, are you going to allocate a new table if one is available, or allocate to an already occupied table if not full?

IMO data structures should be designed before any coding is done, assumptions should be stated, data flow indicated etc etc.

Re billing. Using the container of etc model, it's easy to produce a bill for a customer (simply sum their products) and for a table (simply sum the bill for its customers). The 'tricky' situation is if you have say two groups of 2 at a table for four and you want just a bill for each group. With table/customer model, this can be a bit tricky but if this is a requirement, then introduce a group object eg restaurant->tables->group->customer->product. Always remember Butler Lampson's famous quote 'All problems in computer science can be solved by another level of indirection'.
Tables and customers exist independently of each other, so a table doesn't contain customers, it has a collection of customer IDs.

A customer has a collection of ordered items. You can probably also use the "ordered items" model to create the menu.

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 <iostream>
#include <set>
#include <string>
#include <vector>

using std::set;
using std::string;
using std::vector;

// An item at the restaurant
class Item {
    string name;
    double price;
};


class Customer {
public:
    string name;
    unsigned id;

    // A customer has a collection of items. This isn't a collection of item
    // IDs because it represents the actual item ordered. So it could include
    // a discounted price.
    vector<Item> order;

    // less-than operator lets you create a set of customers
    bool operator < (const Customer &rhs) { return id < rhs.id; }

};

class Table {
    unsigned id;		// the unique table number
public:
    vector<unsigned> customers;	// vector of customer ids
    Table(unsigned id_) : id(id_) {}

    // less-than operator lets you create a set of tables
    bool operator < (const Table &rhs) { return id < rhs.id; }
};

set<Table> tables;
set<Customer> customers;

I thought doing thing this way but receiving these errors:

main.cpp:23:24: error: no member named 'finalValue' in
'std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >'
cout << customerName.finalValue << endl;
~~~~~~~~~~~~ ^
main.cpp:39:18: error: no member named 'table' in
'std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >'
if(productName.table==x){
~~~~~~~~~~~ ^
main.cpp:40:29: error: no member named 'price' in
'std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >'
setFinalBill(productName.price);


My Code

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
#include <stdlib.h>
#include <iostream>
#include <string>

std::string NewTable;
int value;
using namespace std;


class table{
private:
	int number;
	double finalValue;
public:
	void setTableNumber(int x){
		number=x;
	}
	
	void setFinalBill(int x){
		finalValue+=x;
	}
	void outputTable(){
		cout << table.finalValue << endl;
	}
};

class product{
private:
	double price=2.99;
	int table;
	double time;
	int date;
public:
	void setProductTable(int x){
		table=x;
	}
	
	void sendBillValue(string productName,int x){
		if(productName.table==x){
			setFinalBill(productName.price);
		}
	}
};

void NewTableFunc(){
	
	cout << "Customer Name\n";
	getline(std::cin,NewTable);
	table NewTable;
	
	cout << "Table Number\n";
	std::cin>>value;
	NewTable.setTableNumber(value);


	product Hamburguer;
	cout << "Customer Table\n";
	std::cin >> value;
	Hamburguer.setProductTable(value);

	Hamburguer.sendBillValue("Hamburguer",1);

}

int main(){
	NewTableFunc();
	return 0;
}
Last edited on
 
cout << table.finalValue << endl;


don't you just mean:

 
cout << finalValue << endl;


also note:

 
table NewTable;


NewTable has already been defined as a global variable of type string and holds the customer name. It's not a good idea to have global variables - as issues like this occur!
@seeplus
Its just an prototype im going to get things more detailed when this prototype works!

now im receiving this:
main.cpp:40:4: error: use of undeclared identifier 'setFinalBill'
setFinalBill(price);


im not able to call a method from table in product? How im able to send price from object product to finalValue in table?
Last edited on
i did it! Thanks guys for your time!
Topic archived. No new replies allowed.