Click to See Complete Forum and Search --> : class/function


f15e
09-21-2002, 07:33 PM
I am using C++.
Here's the setup. I have two classes(Customer and Book) for this bookstore program.
How, if at all possible, can I access the private members of each class within a regular function that is not part of either class.

My problem is that I am not allowed to do any input or output within the class functions. If I could do this I wouldn't be asking this question. So therefore I must use a function outside of the classes to input and output the data stored in the private members. For example, each block in my array Book bookInfo[] holds several bits of information on a particular customer. Now I can use a class fuction to test to see, for instance, if a customer is a member of the bookclub or not which is given by a Y or N. I search to see if there a member with a for loop. If the customer is a member, how can I send all of that customers information to a funcion outside of the classes?

dighn
09-22-2002, 04:09 PM
yeah sure, just list the function as a "friend" in your class definition eg.

class Foo
{
... stuff...

friend void someFunc(int i,char j);
};

void someFunc(int i, char j)
{
//now this function has access to all private parts of Foo
}

Malone
09-27-2002, 06:38 PM
You could create functions within the classes that simply return the values of the data members. Like...

class Book {
private:
float price;
float numOfPages;
string title;

public:
int getPrice() {return price;}
int getPages() {return numOfPages;}
string getTitle() {return title;}
};

These functions are not literally output, cause all they do is return a value. Then your function in another class could use those and actually output them.

p.s. forgive me for any syntax errors, i'm primarily a java guy, not c++