member function of one class can be friend function of another class
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: member function of one class can be friend function of another class

  1. #1
    Join Date
    Feb 2012
    Location
    INDIA
    Posts
    25

    Question member function of one class can be friend function of another class

    How to make a member function of one class , the friend function of another class:


    class car
    {
    private:
    int speed;

    char color[20];

    public:

    void input()
    {
    cin >> color >> speed;
    }

    void display(car c)// Member function of Class car, which i want to make friend function for Class Scooter
    {
    cout << c.color << endl << c.speed;
    }
    };


    class scooter
    {

    public:
    int input_i()
    {
    cin >> color >> speed;
    }
    friend void car::display(scooter); // I am trying to make the member function display() of Class car , friend function for Class scooter.
    };

    void display(scooter s)
    {
    cout << s.color << s.speed;// compiler error color and speed undeclared.

    }

    // I want to make color and speed visible in Scooter class my making display function , friend function of class scooter.

    void main()
    {
    car c1;
    scooter s1;

    c1.input();

    s1.input_i();

    c1.display(c1);

    s1.display(s1);

    }

    [In the above program i want to make the member function display() of class Car , the friend function for class Scooter, so that class scooter can access private data members(speed and color) of class Car.]


    But i think there's a mistake in above coding , so please help me how i could do this, as compiler error: nonexistent function 'car::display' specified as friend

    THANKS.....
    Last edited by Tanushreeagr; June 1st, 2012 at 01:34 AM.
    TANUSHREE-AGRAWAL...

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: member function of one class can be friend function of another class

    Quote Originally Posted by Tanushreeagr View Post
    M...
    [ In the above program : friend void car::display(scooter); Is this the correct way to make member function of one class , friend function of another class.?]
    1. please, use Code tags while posting code snippets.
    2. Did you try to compile this code? What does compiler say you?
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2012
    Location
    INDIA
    Posts
    25

    Re: member function of one class can be friend function of another class

    Quote Originally Posted by VictorN View Post
    1. please, use Code tags while posting code snippets.
    2. Did you try to compile this code? What does compiler say you?
    I have added necessary code.
    TANUSHREE-AGRAWAL...

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    Re: member function of one class can be friend function of another class

    No you haven't. The post still lacks code tags [code]Properly indented code here[/code]
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  5. #5
    Join Date
    Jan 2009
    Posts
    596

    Re: member function of one class can be friend function of another class

    I suspect that the correct way to solve your real problem (not the one stated here - that is just due to the way you are approaching this) is to have a base class (e.g. vehicle) which contains the speed and color fields, and the method display. Then derive car and scooter classes from this.

  6. #6
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: member function of one class can be friend function of another class

    Please stop using the 'friend' keyword. Wipe it from your memory. As soon ans you need the 'friend' keyword, your design is wrong.

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,977

    Re: member function of one class can be friend function of another class

    As soon ans you need the 'friend' keyword, your design is wrong.
    Untrue as any excessively categorical statement. An advice to use 'friend' as rare as possible would be more correct.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center