In: Computer Science
Part 1 Create a class named Room which has two private data members which are doubles named length and width. The class has five functions: a constructor which sets the length and width, a default constructor which sets the length to 12 and the width to 14, an output function, a function to calculate the area of the room and a function to calculate the parameter. Also include a friend function which adds two objects of the room class. Part 2 Copy your class definition from the previous Part 1 then write the definitions of the 5 functions which are in your class. Part 3 Write a complete in Visual Studio to test your class and all of its functions. Upload the .cpp file as well as the results of running the program.
C++ code:
#include <bits/stdc++.h>
using namespace std;
// forward declaration
class Room1;
// class Room declaration
class Room {
// two private data members, length and width
private:
double length;
double width;
public:
// constructor which sets the length and width
Room(int l,int w){
length = l;
width = w;
}
// default constructor which sets the length to 12 and the width to 14
Room(){
length = 12;
width = 14;
}
// function to calculate the area of the room
double getArea(){
return length * width;
}
// function to calculate the parameter
double perimeter(float l, float w){
return((2*l) + (2*w));
}
// friend function declaration
friend int add( Room , Room1 );
};
class Room1{
private:
double length;
double width;
public:
// constructor which sets the length and width
Room1(int l,int w){
length = l;
width = w;
}
// default constructor which sets the length to 12 and the width to 14
Room1(){
length = 12;
width = 14;
}
// function to calculate the area of the room
double getArea(){
return length * width;
}
// function to calculate the parameter
double perimeter(float l, float w){
return((2*l) + (2*w));
}
// friend function declaration
friend int add( Room , Room1 );
};
// Function add() is the friend function of classes Room and Room1
// that accesses the member variables length and width
int add(Room objectA, Room1 objectB)
{
return ((objectA.length + objectA.width)+(objectB.length + objectB.width));
}
// The main function
int main()
{
Room R1( 4.5, 8.5 );
cout << "Area: " << R1.getArea() << endl;
cout<< "perimeter: " << R1.perimeter(4.5,8.5) <<endl;
// friend function which adds two objects of the room class
Room objectA;
Room1 objectB;
cout<<"Sum: "<< add(objectA, objectB);
return 0;
}
OUTPUT: