Skip to content

Instantly share code, notes, and snippets.

@khaosans
Created November 8, 2018 06:52
Show Gist options
  • Save khaosans/982641a3f2c5fea366a6ba2029bb38da to your computer and use it in GitHub Desktop.
Save khaosans/982641a3f2c5fea366a6ba2029bb38da to your computer and use it in GitHub Desktop.
Friend c++
#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:
explicit 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment