0

I saw an example of friend class on www.cplusplus.com, please find it below:

// friend class
#include <iostream>
using namespace std;

class CSquare;

class CRectangle {
    int width, height;
  public:
    int area ()
      {return (width * height);}
    void convert (CSquare a);
};

class CSquare {
  private:
    int side;
  public:
    void set_side (int a)
      {side=a;}
    friend class CRectangle;
};

void CRectangle::convert (CSquare a) {
  width = a.side;
  height = a.side;
}
  
int main () {
  CSquare sqr;
  CRectangle rect;
  sqr.set_side(4);
  rect.convert(sqr);
  cout << rect.area();
  return 0;
}

After line using namespace std;
we have class CSquare;

I did not understand the purpose of declaring Class here, please guide me on why and what is the purpose of this line in the code?

1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by keeda
0

nevermind I found it below on the link.. thanks all..

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.