C++ question: I came across this in my programming class I am a little confused
ID: 3741958 • Letter: C
Question
C++ question: I came across this in my programming class I am a little confused by it. I think I got A. and B. right but I am not sure about the rest (probably due to me being new to C++).
Consider the following statements:
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;
};
Now answer the following questions:
A. Write the definition of the member function set so that the instance variables are set according to the parameters.
void set(string st, double fst, double snd)
{
description= st;
first=fst;
second=snd;
}
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.
double manipulate()
{
if((strcmp(description, “rectangle”)==0) // to check two strings are equal or not
{
return (first*second);
}
else if((strcmp(description, “circle”)==0)
{
return((22/7)*first*first);//returning the area of the circle with radius first
}
else if((strcmp(description, “sphere”)==0)
{
return((4/3)*(22/7)*first*first*first);//returning the volume of the sphere with radius first
}
else if((strcmp(description, “cylinder”)==0)
{
return((22/7)*first*first*second);//returning the volume of the cylinder with radius first
}
else
return (-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.
Thank you for any help you can give!!!
Explanation / Answer
NOTE: In case of any query or doubt please leave comment, else please rate this answer with thumbs up
Following are the solutions for all the questions , I think solution to A you have given is also correct but the is not appropriate in context with other parts of this question so I have given the correct one:
A.
void set(string st, double fst, double snd)
{
setDescription(st);
setFirst(fst);
setSecond(snd);
}
B.
double manipulate()
{
if((strcmp(description, “rectangle”)==0) // to check two strings are equal or not
{
return (first*second);
}
else if((strcmp(description, “circle”)==0)
{
return((22/7)*first*first);//returning the area of the circle with radius first
}
else if((strcmp(description, “sphere”)==0)
{
return((4/3)*(22/7)*first*first*first);//returning the volume of the sphere with radius first
}
else if((strcmp(description, “cylinder”)==0)
{
return((22/7)*first*first*second);//returning the volume of the cylinder with radius first
}
else
return (-1);
}
C.
void print()
{
if((strcmp(description, “rectangle”)==0) // to check two strings are equal or not
{
cin>>"rectangle: length = ">>first>>", width = ">second>>", area = ">>(first*second);
}
else if((strcmp(description, “circle”)==0)
{
cin>>"circle: radius = ">>first>>", area = ">>((22/7)*first*first);//returning the area of the circle with radius first
}
else if((strcmp(description, “sphere”)==0)
{
cin>>"sphere: radius = ">>first>>", volume = ">>((4/3)*(22/7)*first*first*first);//returning the volume of the sphere with radius first
}
else if((strcmp(description, “cylinder”)==0)
{
cin>>"cylinder: radius = ">>first>>", height = ">second>>", volume = ">>((22/7)*first*first*second);//returning the volume of the cylinder with radius first
}
}
D.
temporary(string = "", double = 0.0, double = 0.0)
{
set(description, first, second);
}
E.
void set(string st, double fst, double snd)
{
setDescription(st);
setFirst(fst);
setSecond(snd);
}
void setDescription(string st)
{
description = st;
}
void setFirst(double f)
{
first = f;
}
void setSecond(double s)
{
s = second;
}
void get(string& st, double& f, double& s)
{
st = getDescription();
f = getFirst();
s = getSecond();
}
string getDescription()
{
return (description);
}
double getFirst()
{
return (first);
}
double getSecond()
{
return (second);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.