Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Consider the following statements: class temporary { public: void set(string,

ID: 3881663 • Letter: 2

Question

2. 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.

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.

Explanation / Answer

//temporary.h

//header file defintion of temporary

#ifndef TEMP_H

#define TEMP_H

#include<iostream>

#include<string>

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;

};

#endif TEMP_H

------------------------------------------------------------------------------------------------------------

//temporary.cpp

//implementation file defition

#include<iostream>

#include "temporary.h"

using namespace std;

//constructor

temporary::temporary(string desc, double f, double s)

{

set(desc,f,s);

}

//set method

void temporary::set(string desc, double f, double s)

{

setDescription(desc);

setFirst(f);

setSecond(s);

}

//print method

void temporary::print()

{

string shape="";

if(description=="rectangle")

{

cout<<"rectangle"<<endl;

cout<<"length : "<<first

<<"width : "<<second<<",area = "<<manipulate()<<endl;

}

else if(description=="circle")

{

cout<<"circle"<<endl;

cout<<"radius : "<<first

<<",area = "<<manipulate()<<endl;

}

else if(description=="cylinder")

{

cout<<"cylinder"<<endl;

cout<<"radius : "<<first

<<",area = "<<manipulate()<<endl;

}

else if(description=="sphere")

{

cout<<"sphere"<<endl;

cout<<"radius : "<<first

<<",height : "<<second<<",area = "<<manipulate()<<endl;

}

}

//manipulate method

double temporary::manipulate()

{

double result=0;

if(description=="rectangle")

{

result=first*second;

}

else if(description=="circle")

{

result=3.1415*first*first;

}

else if(description=="sphere")

{

result=(4/3.0)*(3.1415)*(first*first*first);

}

else if(description=="cylinder")

{

result=3.1415*first*first*second;

}

return result;

}

//get method

void temporary::get(string& desc, double&f, double&s)

{

description=desc;

first=f;

second=s;

}

//set metods

void temporary::setDescription(string desc)

{

description=desc;

}

void temporary::setFirst(double f)

{

first=f;

}

void temporary::setSecond(double s)

{

second=s;

}

string temporary::getDescription() const

{

return description;

}

double temporary::getFirst()const

{

return first;

}

double temporary::getSecond()const

{

return second;

}

------------------------------------------------------------------------------------------------------------

//main.cpp

//include header files

#include<iostream>

//include temporary.h

#include "temporary.h"

using namespace std;

//start of main method

int main()

{

//create an object of rectangle type

temporary rectangle;

rectangle.set("rectangle",5.0,6.0);

rectangle.print();

//create an object of circle type

temporary circle("circle",5.0);

circle.print();

//create an object of cylinder type

temporary cyclinder;

cyclinder.set("cylinder",5.0,6.0);

cyclinder.print();

//create an object of sphere type

temporary sphere("sphere",5.0,6.0);

sphere.print();

system("pause");

return 0;

}

------------------------------------------------------------------------------------------------------------

sample output:

rectangle
length : 5width : 6,area = 30
circle
radius : 5,area = 78.5375
cylinder
radius : 5,area = 471.225
sphere
radius : 5,height : 6,area = 523.583