KEMBAR78
Class Friend Class 1d.cpp | PDF
0% found this document useful (0 votes)
4 views1 page

Class Friend Class 1d.cpp

Uploaded by

Emmanuel Mrigo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Class Friend Class 1d.cpp

Uploaded by

Emmanuel Mrigo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

1: //Friend Classes

2: #include <iostream> //For Output and Input Stream


3: using namespace std;
4:
5: class Square;//Forward declaration of Square
6:
7: class Rectangle //Class Definition
8: {
9: int width, height;//Data Members of Rectangle
10:
11: public:
12: Rectangle(int w = 1, int h = 1):width(w),height(h){}//initialization
13: void display()
14: {
15: cout <<"Area of Rectangle: " << width * height << endl;
16: }
17: void morphosis(Square &); //Function for changing a Rectangle to a Square
18: }; //End of class Square definition
19:
20: class Square // Definition of class Square
21: {
22: int side; //Square Data Member
23:
24: public:
25: Square(int s = 1):side(s){} //side initialized to 1
26: void display()
27: {
28: cout <<"Area of Square: " << side * side << endl;
29: }
30: friend class Rectangle;// all member functions of Rectangle are friends to Square
31: };//End of class Square definition
32:
33: void Rectangle::morphosis(Square &s) //Argument is object of class Square, ref. Line No 30
34: {
35: width = s.side;//Square Data Member access through local Object s ref. Line No 30
36: height = s.side;//Square Data Member access through local Object s ref. Line No 30
37: }
38:
39: int main ()
40: {
41: Rectangle rec (5,10);//Rectangle Object created ,assigned values
42: Square sq (5); //Square Object created & value assigned
43: cout << "Before:" << endl;
44: rec.display();
45: sq.display();
46: rec.morphosis(sq);//Adopting the size of Square, ref. Line No: 30
47: cout << "\nAfter:" << endl;
48: rec.display();
49: sq.display();
50: return 0; //Object Destruction & Returning control to OS
51: }
52:

You might also like