I've been working on the below question for a little bit. I answered all of the questions, but I'm not really sure if they're correct. If any of you could take a look at my answers for the question below, I'd really appreciate it. The question is listed above the code. The answers are listed below the code. Thanks!
a. Write the definition of the member function set so that the instance variables are set according to the parameters.
b. Write the definition of the member function manipulate that returns a decimal number as follows: If the value of description is "rectangle", it returns first * second; if the value of description is "circle", it returns the area of the circle with radius first; if the value of description is "sphere", it returns the volume of the sphere with radius first; if the value of description is "cylinder", it returns the volume of the cylinder with radius first and height second; otherwise, it returns the value -1.
c. Write the definition of the function print to print the values of the instance variables and the values returned by the function manipulate. For example, if description = "rectangle", first = 8.5, and second = 5, it should print: rectangle: length = 8.50, width = 5.00, area = 42.50
d. Write the definition of the constructor so that it initializes the instance variables using the function set.
e. Write the definition of the remaining functions to set or retrieve the values of the instance variables. Note that the function get returns the values of all instance variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
class temporary
{
public:
void set(string, double, double);
void print();
double manipulate();
void get(string&, double&, double&);
void setDescription(string);
void setFirst(double);
void setSecond(double);
string getDescription() const;
double getFirst()const;
double getSecond()const;
temporary(string = "", double = 0.0, double = 0.0);
private:
string description;
double first;
double second;
};
#include <iostream>
#include <iomanip>
#include "junk.h"
using namespace std;
int main() {
temporary object1;
temporary object2("rectangle", 8.5, 5);
temporary object3("circle", 6, 0);
temporary object4("cylinder", 6, 3.5);
cout << fixed << showpoint << setprecision(2) << endl;
object1.print();
object2.print();
object3.print();
object4.print();
object1.set("sphere", 4.5, 0);
object1.print();
}
//ANSWERS
a. void set (string d, double f, double s)
{
description = d;
first = f;
second = s;
}
b. double manipulate (string)
{
if (description = “rectangle”){
return first * second;
}
else if (description = “circle”){
return 3.14 * (first * first);
}
else if (description = “sphere”){
return (4/3) * 3.14 * (first * first * first);
}
else if (description = “cylinder”){
return 3.14 * (first * first) * second;
}
Else if (description = “”){
return -1;
}
}
c. double print(){
return manipulate();
}
d. temporary() {
description = “”;
first = 0;
second=0;
}
e. void setDescription(string s){
description = s;
}
string getDescription (){
return description;
}
void setFirst(double f){
first = f;
}
double getFirst (){
return first;
}
void setSecond(double r){
second = r;
}
double getSecond (){
return second;
}
|