class/function

Sharky Forums


Results 1 to 3 of 3

Thread: class/function

  1. #1
    Catfish f15e's Avatar
    Join Date
    Mar 2002
    Location
    I live in the south.
    Posts
    114

    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?

  2. #2
    Mako Shark dighn's Avatar
    Join Date
    Nov 2000
    Location
    Vancouver, BC Canada
    Posts
    3,171
    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.
    .

  3. #3
    Tiger Shark
    Join Date
    Oct 2000
    Location
    Erie, PA, USA
    Posts
    693
    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
  •