In: Computer Science
C++ code
Write a program to illustrate how to use the temporary class. Your program must contain statements that would ask the user to enter data of an object and use the setters to initialize the object.
Use three header files named main.cpp, temporary.h, and temporaryImp.cpp
An example of the program is shown below:
Enter object name (rectangle, circle, sphere, or cylinder: circle Enter object's dimensions: rectangle (length and width) circle (radius and 0) sphere (radius and 0) rectangle (base raidus and height) 10 0 After setting myObject: circle: radius = 10.00, area =
// Temporary.h
#include<iostream>
using namespace std;
class
Rectangle
// Rectangle Class
{
float length, width;
public:
void setValues(float len, float wid);
void printValues();
};
class
Circle
// Circle Class
{
float radius,area;
public:
void setValues(float radius, float area);
void printValues();
};
class
Sphere
// Sphere Class
{
float radius, area;
public:
void setValues(float radius, float area);
void printValues();
};
class
Cylinder
// Cylinder Class
{
float radius, height;
public:
void setValues(float radius, float height);
void printValues();
};
******************************************************************************************************************
// TemporaryImp.cpp
// Implementation of All methods.
#include "Temporary.h"
void Rectangle::setValues(float len, float
wid) // Setter
Method
{
this->length = len;
this->width = wid;
}
void
Rectangle::printValues()
// To display data
{
cout << "Rectangle: length = " <<
this->length << ", width" << this->width;
}
void Circle::setValues(float radius, float area)
{
this->radius = radius;
this->area = area;
}
void Circle::printValues()
{
cout << "circle: radius = " <<
this->radius << ", area = " << this->area;
}
void Sphere::setValues(float radius, float area)
{
this->radius = radius;
this->area = area;
}
void Sphere::printValues()
{
cout << "sphere: radius = " <<
this->radius << ", area = " << this->area;
}
void Cylinder::setValues(float radius, float height)
{
this->radius = radius;
this->height = height;
}
void Cylinder::printValues()
{
cout << "cylinder: radius = " <<
this->radius << ", height = " <<
this->height;
}
**********************************************************************************************************************
// main.cpp
#include "Temporary.h"
int main()
{
char str[10] = { 0 };
float val1 = 0, val2 = 0;
cout << "Enter object name (Rectangle, Circle,
Sphere or Cylinder):";
cin >> str;
cout << "Enter object's dimensions:";
cin >> val1 >> val2;
if (0 == _strcmpi(str, "Rectangle"))
{
Rectangle
rec;
// Object creation as per user input.
rec.setValues(val1,val2);
// Setter method call to set values.
rec.printValues();
}
else if (0 == _strcmpi(str, "Circle"))
{
Circle
cir;
// Object creation as per user input.
cir.setValues(val1,
val2);
// Setter method call to set values.
cir.printValues();
}
else if (0 == _strcmpi(str, "Sphere"))
{
Sphere
sp;
// Object creation as per user input.
sp.setValues(val1,
val2);
// Setter method call to set values.
sp.printValues();
}
else if (0 == _strcmpi(str, "Cylinder"))
{
Cylinder
cyl;
// Object creation as per user input.
cyl.setValues(val1,
val2);
// Setter method call to set values.
cyl.printValues();
}
return 0;
}
******************************************************************************************************************************
OUTPUT :
Enter object name (Rectangle, Circle, Sphere or
Cylinder):circle
Enter object's dimensions:
10
0
circle: radius = 10, area = 0
******************************************************************************************************************************
Explanation :
I have done object creation as per user input and initialize values to member of that class, display those values. i didnt do any calculation operation. if any help need, plz comment.
******************************************************************************************************************************