c++中friend class

来源:互联网 发布:淘宝如何最快提交订单 编辑:程序博客网 时间:2024/06/05 02:06
// friend class#include <iostream>using namespace std;class Square;class Rectangle {    int width, height;  public:    int area ()      {return (width * height);}    void convert (Square a);};class Square {  friend class Rectangle;  private:    int side;  public:    Square (int a) : side(a) {}};void Rectangle::convert (Square a) {  width = a.side;  height = a.side;}int main () {  Rectangle rect;  Square sqr (4);  rect.convert(sqr);  cout << rect.area();  return 0;}
原创粉丝点击