Question

In: Computer Science

Please write this program in C++ Write a program that           - a user-defined class Cuboid...

Please write this program in C++

Write a program that

          - a user-defined class Cuboid which has the following elements:

                   - default constructor (has no parameters)

                   - constructor that has 3 parameters (height, length, width)

                   - data members

                             - height, length, width

                   - member functions

                             - to set the value of each of the data members - 3 functions

                             - to get the value of each of the data members - 3 functions

                             - one that returns the volume

                             - one that returns the surface area

                             - one that increases each dimension by a specified factor

                   (Function definitions outside of the class!!!)

          - in main()

                   - instantiate a Cuboid object and initialize at the time of definition

                             - make sure the constructor with parameters is called

                      - define an array of 2 Cuboid objects

                      - using a loop get information to populate the 2 objects in the array

                               -prompt for the height, length, and width

                                -prompt for a size by which to increase each dimension(one input)

      -call a global function passing a Cuboid object and the increase factor as separate arguments; function will call the class member function to increase the dimensions; parameters of the function should be references

                  -display the dimensions, the volume, and the surface area for each of the three objects.

                         

                       

Solutions

Expert Solution

#include <iostream>

using namespace std;

//declaring class

class Cuboid {

   public:

       Cuboid() {}

       Cuboid(float height, float length, float width) {
           this -> height = height;
           this -> length = length;
           this -> width = width;
       }

       float getHeight();
       void setHeight(float height);
       float getLength();
       void setLength(float length);
       float getWidth();
       void setWidth(float width);
       float getVolume();
       float getSurfaceArea();
       void increaseDimension(float factor);


   private:
       float height, length, width;

};

//defining member functions

float Cuboid::getHeight() {
   return this -> height;
}

void Cuboid::setHeight(float height) {
   this -> height = height;
}

float Cuboid::getLength() {
   return this -> length;
}

void Cuboid::setLength(float length) {
   this -> length = length;
}

float Cuboid::getWidth() {
   return this -> width;
}

void Cuboid::setWidth(float width) {
   this -> width = width;
}

float Cuboid::getVolume() {
   return (height*length*width);
}

float Cuboid::getSurfaceArea() {
   return 2*((length*height) + (height*width) + (length*width));
}

void Cuboid::increaseDimension(float factor) {
   this -> height = this -> height + factor;
   this -> length = this -> length + factor;
   this -> width = this -> width + factor;
}

//Global function to increase dimension by a common factor
void increaseDimensionByFactor(Cuboid &c, float &factor) {
   c.increaseDimension(factor);   //calls class member function
}


int main() {

   //initiating the first cuboid object with parameterized constructor
   Cuboid c0 = Cuboid(5.1, 6.2, 7.3);

   //printing details for c0
   cout << "Height for c0 : " << c0.getHeight() << "\n";
   cout << "Length for c0 : " << c0.getLength() << "\n";
   cout << "Width for c0 : " << c0.getWidth() << "\n";
   cout << "Volume for c0 : " << c0.getVolume() << "\n";
   cout << "Surface Area for c0 : " << c0.getSurfaceArea() << "\n";

   //defining an array of two Cuboid objects
   Cuboid cuboids[2];
   float height, length, width, increaseFactor;

   //taking input and initializing
   for(int i=0; i < sizeof(cuboids)/sizeof(*cuboids); i++) {
       cout << "Enter height of Cuboid " << i + 1 << " : ";
       cin >> height;

       cout << "Enter length of Cuboid " << i + 1 << " : ";
       cin >> length;

       cout << "Enter width of Cuboid " << i + 1 << " : ";
       cin >> width;

       cout << "Enter size by which to increase each dimension of Cuboid " << i + 1 << " : ";
       cin >> increaseFactor;

       cuboids[i] = Cuboid(height, length, width);

       //increasing dimension by factor
       increaseDimensionByFactor(cuboids[i], increaseFactor);

       //printing details
       cout << "Height for c" << i + 1 << " : " << cuboids[i].getHeight() << "\n";
       cout << "Length for c" << i + 1 << " : " << cuboids[i].getLength() << "\n";
       cout << "Width for c" << i + 1 << " : " << cuboids[i].getWidth() << "\n";
       cout << "Volume for c" << i + 1 << " : " << cuboids[i].getVolume() << "\n";
       cout << "Surface Area for c" << i + 1 << " : " << cuboids[i].getSurfaceArea() << "\n";
   }
}



Related Solutions

Please Write C++ PROGRAM : That will write a program that initially prompts the user for...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for a file name. If the file is not found, an error message is output, and the program terminates. Otherwise, the program prints each token in the file, and the number of times it appeared, in a well formatted manner. To accomplish all this, do the following: - Open the file - the user must be prompted and a file name input. DO NOT hardcode...
Write and test a user-defined class (requiring conditions). Write an application (client) program that uses an...
Write and test a user-defined class (requiring conditions). Write an application (client) program that uses an instance(s) of a user-defined class. The federal income tax that a person pays is a function of the person's taxable income. The following table contains formulas for computing a single person's tax. Bracket Taxable Income Tax Paid 1 $22,100 or less 15% 2 More than $22,100 but $53,500 or less $3,315 plus 28% of the taxable income over $22,100 3 More than $53,500 but...
Please code in c# (C-Sharp) Write a program that will ask the user for their name....
Please code in c# (C-Sharp) Write a program that will ask the user for their name. If the user does not input anything, display a warning before continuing. The program will then ask the user whether they want to have an addition, subtraction, multiplication, or division problem. Once the user indicates their choice, the program will display 2 randomly generated numbers from 1 to 9 in a math problem matching the user’s choice. Example: user selects addition, the equation presented...
*Please write code in C++* Write a program to verify the validity of the user entered...
*Please write code in C++* Write a program to verify the validity of the user entered email address.   if email is valid : output the stating that given email is valid. ex: "The email [email protected] is valid" else : output the statement that the email is invalid and list all the violations ex:  "The email sarahwinchester.com is invalid" * @ symbol * Missing Domain name The program should keep validating emails until user enter 'q' Upload your source code. ex: main.cpp
Please solve the following problem for MATLAB Write a user-defined function (name it F to C)...
Please solve the following problem for MATLAB Write a user-defined function (name it F to C) that converts temperature in degrees F to temperature in degrees C. Use the function to solve the following problem. The change in the length of an object, ∆L, due to a change in the temperature, ∆T, is given by: ∆L = αL∆T, where a is the coefficient of thermal expansion. Determine the change in the area of a rectangular(4.5 m by 2.25m) aluminum (α=23·10-61/˚C)...
C# PLEASE Lab7B: For this lab, you’re going to write a program that prompts the user...
C# PLEASE Lab7B: For this lab, you’re going to write a program that prompts the user for the number of GPAs to enter. The program should then prompt the user to enter the specified number of GPAs. Finally, the program should print out the graduation standing of the students based on their GPAs. Your program should behave like the sample output below. Sample #1: Enter the number of GPAs: 5 GPA #0: 3.97 GPA #1: 3.5 GPA #2: 3.499 GPA...
C++ Please For this assignment, you will write a program that lets the user play against...
C++ Please For this assignment, you will write a program that lets the user play against the computer in a variation of the popular blackjack car game. In this variation of the game, two-six sided dice are used instead of cards. The dice are rolled, and the player tries to beat the computer's hidden total without going over 21. Here are some suggestions for the game's design: Each round of the game is performed as an iteration of a loop...
Please code C# 8. Write a program that prompts the user to enter an integer. The...
Please code C# 8. Write a program that prompts the user to enter an integer. The program then determines and displays the following: Whether the integer is divisible by 5 and 6 Whether the integer is divisible by 5 or 6
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT