Question

P3 - An Array of objects (120 points) Write a main program and declare an array of 5 RectangularCube objects [20pts). a. Use
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;

class RectangularCube{//creaitng class
private:
int length,width,height;
public:
RectangularCube(){}//constructor
RectangularCube(int length, int width, int height){//parameterized constructor
this->length = length;
this->width = width;
this->height = height;
}
//getters to get object length and width and height and volume and surface area
int getLength(){
return this->length;
}
int getWidth(){
return this->width;
}
int getHeight(){
return this->height;
}
int getSurfacearea(){
return 2*((this->width*this->length) + (this->length*this->height) + (this->height*this->width));
}
int getVolume(){
return (this->length*this->width*this->height);
}
};

//printcube accepts rectangularcube object and print desired parameter
void printCube(RectangularCube rc){

cout << "\nL = " << rc.getLength()<< ",W = " << rc.getWidth()<< ",H = " << rc.getHeight()<< ",V = "<<rc.getVolume()<< ",A = " << rc.getSurfacearea()<<endl;
}

int main() {
int length, width, height;
  
RectangularCube rc[5];//create 5 objects of rectangularcube
// this is to initialize the rand() with seed 0
srand(time(0));
  
for(int i=0; i<5; i++){
// random numbers are generated between 1 and 10
length = rand()%10 + 1;
width = rand()%10 + 1;
height = rand()%10 + 1;
  
rc[i] = RectangularCube(length, width, height);
}
  
// looping through created 5 objects
for(int i=0; i<5; i++){
printCube(rc[i]);
cout << "\n-------------------------------------------\n";
}

}

/cygdrive/d/practice gkongana@GKONGANA-IN /cygdrive/d/practice $ g++ practice.cpp gkongana@GKONGANA-IN /cygdrive/d/practice $File Edit Selection Find View Goto Tools Project Preferences Help practice.cpp #include <iostream> using namespace std; 2 claUpractice practice.cpp - Sublime Text (UNREGISTERED) File Edit Selection Find View Goto Tools Project Preferences Help practi

Add a comment
Know the answer?
Add Answer to:
P3 - An Array of objects (120 points) Write a main program and declare an array...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that performs the following operations on a one dimensional array with 50 unsigned...

    Write a program that performs the following operations on a one dimensional array with 50 unsigned integers. The main program will initialize the array, print the array values to the screen and then call a function that will determine and print the maximum and minimum values. •Declare the array and any other variables. •Use a loop to initialize the array with 50 random integer values between 0 and 99 using the rand() function. (number = rand() % 100;) •Using cout...

  • C Language Write the code that dynamically allocates an array of struct objects based on a...

    C Language Write the code that dynamically allocates an array of struct objects based on a size entered through the command line arguments, You will use the following struct and enum. typedef enum Color { RED, GREEN, BLUE } Color; typedef struct MyStruct { int value; Color color; } MyStruct; Write the code to do the following: a. Check for one additional command line argument. Only proceed to the rest of the program if it exists and that the value...

  • Part I: Create a program class named TestArrays This class should have a main() method that...

    Part I: Create a program class named TestArrays This class should have a main() method that behaves as follows: Create an integer array of size 10. Using a loop, fill the elements with increments of 5, starting with 5 in the first element. Using the array elements, sum the second, fifth and seventh elements. Display the sum with a label. Change the fourth element to 86. Subtract 9 from the ninth element. Using a loop, display the elements of the...

  • a) Declare and instantiate an array named scores of twenty-five elements of type int.   (b)Write a...

    a) Declare and instantiate an array named scores of twenty-five elements of type int.   (b)Write a statement that declares an array namedstreetAddress that contains exactly eighty elements of typechar. 2. In a single statement: declare, create and initialize an arraynamed a of ten elements of type int with the values of the elements (starting with the first) set to 10, 20,..., 100 respectively. 3. Declare an array reference variable, week, and initialize it to an array containing the strings "mon",...

  • I need to write a Java program. Declare and initialize an ArrayList of type Employee. In...

    I need to write a Java program. Declare and initialize an ArrayList of type Employee. In a while loop, keep initializing objects of type Employee, storing them in the array, and allow the user to enter ID, salary, and leaveDays of the each employee one by one and in one line. Once the user entered a -1, stop initializing new employees, and print "You entered [actual length of array] employees. Thank you!" Create a FOR loop that goes through every...

  • JAVA I. Using the Event Class created previously, create an array of objects;        II. Demonstrate...

    JAVA I. Using the Event Class created previously, create an array of objects;        II. Demonstrate passing array reference to a method;        III. Demonstrate creating array of objects that prints out only x 0.0 for all objects. PROJECT 5C - ARRAYS Create a new file called " EventArray5C". There are some "challenging" directions in this project. . 1.Prepare a document box (tell me that task(s) the application is to accomplish, how it will accomplish the tasks, the author of...

  • Write a main program that will randomly fill a 2 dimensional array with integer values in...

    Write a main program that will randomly fill a 2 dimensional array with integer values in the range of 0 to 50. Use the Rand_Int function and constants to do this. These values represent the height of a terrain. Write a function that will determine what part of the terrain will flood. A. Create a global constant called MAX and assign it the value of 10. B. Prompt the user in main, at what height do they consider it to...

  • Write a C program convert.c that converts each number in an array by the sum of...

    Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...

  • For this program you need to take the following data and convert it into an array...

    For this program you need to take the following data and convert it into an array literal of Javascript object literals, where each object literal has three properties: a destination, a number of miles, and a number of gallons as shown below. You need to assign the array of objects to a variable, like trips, Your array of objects cannot contain the mpg value. The value for miles per gallon must be calculated by the function called when the button...

  • Part I Short Answer (50 points) 1. Write a pseudocode declaration for a String array initialized...

    Part I Short Answer (50 points) 1. Write a pseudocode declaration for a String array initialized with the following strings: “Einstein”, “Newton”, “Copernicus”, and “Kepler”. 2. Assume names in an Integer array with 20 elements. Write a pseudocode algorithm a For Loop that displays each element of the array. 3. Assume the arrays numberArray1 and numberArray2 each have 100 elements. Write a pseudocode algorithm that copies the values in numberArray1 to numberArray2 . 4. Write a pseudocode algorithm for a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT