|
-
Catfish
class/function
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?
-
Mako Shark
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
}
Last edited by dighn; 09-22-2002 at 04:09 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++
Last edited by Malone; 10-02-2002 at 08:58 PM.
AMD AthlonXP 2600+ Thoroughbred B @ 200x10.5
Shuttle AN35N nForce2 Ultra 400
2x512MB Kingston PC3200 (3-3-3)
ATI Radeon 9600 Pro
40GB WD ATA-100 8MB cache
Creative 12X DVD Drive
Memorex 52X CD-RW
Running Windows XP Pro
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|