Skip to content

Instantly share code, notes, and snippets.

@hasinur1997
Created March 13, 2019 15:44
Show Gist options
  • Select an option

  • Save hasinur1997/e368ba90b578cee94d71f74f28f2edd7 to your computer and use it in GitHub Desktop.

Select an option

Save hasinur1997/e368ba90b578cee94d71f74f28f2edd7 to your computer and use it in GitHub Desktop.

Implementation of Friend Class

#include<iostream>

using namespace std;

class Square;

class Rectangle {
	public:
		int height;
		int width;
		
		void area() {
			cout << height*width << endl;
		}
		
		void convert(Square object);
};

class Square {
	private:
		friend class Rectangle;
		float side;
	
	public:
		
		Square(int x) {
			side = x;
		}
};

void Rectangle::convert( Square object ) {
	height = object.side;
	width = object.side;
}

int main() {
	Rectangle r;
	Square s(5);
	
	r.convert(s);
	r.area();
}

@emranweb
Copy link

Nice Work 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment