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
|
#include <iostream>
#include <iomanip>
using namespace std;
class Shapes
{
public:
Shapes(int = 1, int = 1, int = 1, int = 1);
void set_box(int, int, int);
void set_cylinder(int, int);
void set_rectangle(int, int);
void set_circle(int);
// get functions
void get_dimensions() const; // default dimensions set by default constructor
int get_rectangle() const;
int get_box() const;
double get_cylinder() const;
private:
int length {1};
int width {1};
int height {1};
int radius {1};
};
const int minimum_length = 2; // set min. length a dimension can be
Shapes::Shapes(int L, int W, int H, int R) : length(L), width(W), height(H), radius(R) {}
void Shapes::get_dimensions() const
{
cout << "Dimensions: " << endl;
cout << " length = " << length << endl; // print length
cout << " width = " << width << endl; // print width
cout << " height = " << height << endl; // print height
cout << " radius = " << radius << endl; // print height
cout << endl; //skip a space
}
void Shapes::set_rectangle(int L, int W)
{
if (L >= minimum_length) {
cout << "default length = " << length << endl;
length = L;
cout << "new length = " << length << endl;
} else {
length = 0;
//cout << "length = " << length << endl;
cout << " ***invalid Rectangle dimensions! length must be > " << minimum_length << endl;
}
width = (W >= 3) ? W : 0;
}
// using parameters: length, width, height
void Shapes::set_box(int L, int W, int H)
{
length = (L >= minimum_length) ? L : 0;
width = (W >= minimum_length) ? W : 0;
height = (H >= minimum_length) ? H : 0;
}
// using parameters: height, radius
void Shapes::set_cylinder(int H, int R)
{
height = (H >= minimum_length) ? H : 0;
radius = (R >= minimum_length) ? R : 0;
}
/***** Get Functions *****/
int Shapes::get_rectangle() const
{
const int a = length * width;
cout << "rectangle area = " << a << endl;
return a;
}
int Shapes::get_box() const
{
const int a = length * width * height;
cout << "box volume = " << a << endl;
return a;
}
double Shapes::get_cylinder() const
{
const double a = 2.0 * radius * 3.141592654 * height;
cout << "cylinder volume = " << fixed << setprecision(3) << a << endl;
return a;
}
int main()
{
Shapes t0; // Shapes, default
Shapes t1; // rectangle area
Shapes t2; // box volume
Shapes t3; // cylinder volume
t0.get_dimensions();
t1.set_rectangle(12, 4);
t2.set_box(7, 2, 4);
t3.set_cylinder(2, 10);
t1.get_rectangle();
t2.get_box();
t3.get_cylinder();
cout << endl;
t0.get_dimensions();
t1.get_dimensions();
t2.get_dimensions();
t3.get_dimensions();
}
| |