Question

In: Computer Science

Part 1 Create a class named Room which has two private data members which are doubles...

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.

Solutions

Expert Solution

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:



Related Solutions

Define the class HotelRoom. The class has the following private data members: the room number (an...
Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler...
Define the class HotelRoom. The class has the following private data members: the room number (an...
Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler...
The Account class Create a class named Account, which has the following private properties:
in java The Account class Create a class named Account, which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNumber (), getBalance (), setBalan newBalance). There is no setNumber () once an account is created, its account number cannot change. Now implement these methods: void deposit (double amount) and void withdraw (double amount). For both these methods, if the amount is less than...
The Account class Create a class named Account , which has the following private properties:
 The Account class Create a class named Account , which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNunber(), getBalance(), setBalance (double newBalance) . There is no setNunber() - once an account is created, its account number cannot change. Now implement these methods: void deposit (double anount) and void withdraw(double anount). For both these methods, if the amount is less than zero,...
The Account class Create a class named Account, which has the following private properties: number: long...
The Account class Create a class named Account, which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNumber(), getBalance(), setBalance(double newBalance). There is no setNumber() -- once an account is created, its account number cannot change. Now implement deposit(double amount) and withdraw(double amount) methods. If the amount is less than zero,...
The Account class Create a class named Account, which has the following private properties: number: long...
The Account class Create a class named Account, which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNumber(), getBalance(), setBalance(double newBalance). There is no setNumber() -- once an account is created, its account number cannot change. Now implement deposit(double amount) and withdraw(double amount) methods. If the amount is less than zero,...
The Account class Create a class named Account, which has the following private properties: number: long...
The Account class Create a class named Account, which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNumber(), getBalance(), setBalance(double newBalance). There is no setNumber() -- once an account is created, its account number cannot change. Now implement these methods: void deposit(double amount) and void withdraw(double amount). For both these methods,...
Write a class called Person that has two private data members - the person's name and...
Write a class called Person that has two private data members - the person's name and age. It should have an init method that takes two values and uses them to initialize the data members. It should have a get_age method. Write a separate function (not part of the Person class) called std_dev that takes as a parameter a list of Person objects and returns the standard deviation of all their ages (the population standard deviation that uses a denominator...
C++ Create a class for working with fractions. Only 2 private data members are needed: the...
C++ Create a class for working with fractions. Only 2 private data members are needed: the int numerator of the fraction, and the positive int denominator of the fraction. For example, the fraction 3/7 will have the two private data member values of 3 and 7. The following methods should be in your class: a. A default constructor that should use default arguments in case no initializers are included in the main. The fraction needs to be stored in reduced...
Write a program in which define a templated class mySort with private data members as a...
Write a program in which define a templated class mySort with private data members as a counter and an array (and anything else if required). Public member functions should include constructor(s), sortAlgorithm() and mySwap() functions (add more functions if you need). Main sorting logic resides in sortAlgorithm() and mySwap() function should be called inside it. Test your program inside main with integer, float and character datatypes.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT