Friday, 2 April 2021

cpp6-1-2019

 

/*Write a C++ program to create two Classes Square and Rectangle.  Compare  area of  both  the shapes using friend  function. Accept appropriate data members  for  both  the classes .*/

 

#include<iostream.h>

#include<conio.h>

class Square

{

friend class Rectangle;      // declaring Rectangle as friend class

int side;

public:

Square ( int s )

{

side = s;

}

};

 

class Rectangle

{

int length;

int breadth;

public:

int getArea()

{

return length * breadth;

}

void shape( Square a )

{

length = a.side;

breadth = a.side;

}

};

 

int main()

{

Square square(5);

Rectangle rectangle;

clrscr();

rectangle.shape(square);

cout << rectangle.getArea();

return 0;

getch();

}