CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Threaded View

  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:isplay(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:isplay' specified as friend

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

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured