Using 3 classes in C++

Hi there. I'm having trouble with my classes. I'm creating a program that gives you all the information that you need when you place an order, and I've completed all but the last part of it.

I'm supposed to have the Order class contain both the Customer and the Product classes inside of them, and the Customer class also contains the Address class inside of it.

I need to find a way to access the zip code from the Address class in the Order class. Here is what I thought would work, but results in address being inaccessible:

1
2
3
4
string Order::getShippingZip()
{
   return customer.address.zip;
}


The address class is included in the customer.h file, and it works when I run code like this:

1
2
3
4
5
6
7
8
9
10
11
void Customer::prompt()
{
   cout << "Name: ";
   address.prompt();
}

void Customer::display()
{
   cout << name << endl;
   address.display();
}


Neither of the above examples say that it isn't accessible, and yet, when I try to access it from one above that, it says that it is inaccessible.

How do I access the private member variable zip in the Address class from the Order class without making it a public member variable?

Thank you. I can post more code if you would like to see it, but the entire project is 6 total files, and a lot of it is just getters and setters. That all compile and work correctly.
The whole point of making a variable private is so that you can't access it directly from outside the class. If you want to provide read-only access to the variable you could add a getter function to the class.
Last edited on
Ah. I see now what the problem was. I need to use a public method instead of trying to access the private variable. Thanks.
Last edited on
Topic archived. No new replies allowed.