// Program file: MetricClass3.cpp // Demos both public and private access modifiers // and includes functions in a class and a constructor #include #include using namespace std; class Metric { private: // inaccessible data members static const float CM = 2.54; static const float KG = 0.454; int height; int weight; float centimeters; float kilograms; public: // accessible data and member functions // specific value constructor Metric(int h, int w) { height = h; weight = w; calc_height(); calc_weight(); } void set_height(int i) { height = i; } void set_weight(int p) { weight = p; } int get_height() { return height; } int get_weight() { return weight; } float get_centimeters() { return centimeters; } float get_kilograms() { return kilograms; } void calc_height() { centimeters = height * CM; } void calc_weight() { kilograms = weight * KG; } }; // end of Metric class statement // program functions prototypes, not part of Metric class void get_data(int &h, int &w); void print_results(Metric m); int main() { int h, w; system("cls"); get_data(h, w); Metric mObj(h, w); print_results(mObj); system("pause"); return 0; } void get_data(int &h, int &w) { cout << "Enter your height in inches: "; cin >> h; cout << "Enter your weight in pounds: "; cin >> w; } void print_results(Metric m) { cout << setprecision(2) << setiosflags(cout.fixed) << setiosflags(cout.showpoint); cout << "YOUR HEIGHT AND WEIGHT IN CENTIMETERS AND KILOGRAMS"; cout << endl << endl; cout << " Height Weight" << endl; cout << "--------------------------------------" << endl; cout << "English " << setw(6) << m.get_height() << "in. " << setw(6) << m.get_weight() << "lbs." << endl; cout << "Metric " << setw(6) << m.get_centimeters() << "cm. " << setw(6) << m.get_kilograms() << "kg." << endl; }