//Program: OO1_cylinder.cpp //Purpose: (1) To calculate Volume "V" and surface area "S" of a cylinder // from its diameter "D" and height "H". // (2) To determine whether this cylinder fits into a regular prism // of known width, depth, and height. // (3) To determine whether any cylinder edges // are tangential to prism walls // Program is composed of a class and function. Also includes predicate function // and set & get functions. //Author: ... //Prepared on: 20 June 2003 //At: ..., for OO C++, Assignment OO1 //----------------------------------------------------------------------------- #include #include #include class cylinder //class definitionn for cylinder class { //beging class definition public: //public declared member functions cylinder(); //class constructor float set_cylinder_diameter(float); //sets and gets cylinder diameter input float set_cylinder_height(float); //sets and gets cylinder height input float set_prism_width(float); //sets and gets prism width from main float set_prism_depth(float); //sets and gets prism depth from main float set_prism_height(float); //sets and gets prism height from main void set_other_data(); //sets cylinder volume and surface void calculate_cylinder_volume(); //calculates cylinder volume void calculate_cylinder_surface(); //calculates cylinder surface void display_cylinder_results(); //displays cylinder results bool prism(bool); //predicate function-determines if cylinder fits prism void fit_results_display(); //displays messages for cylinder fit ~cylinder(); //class destructor private: //private attributes float cylinder_diameter; //diameter of cylinder(user input) float cylinder_height; //height of cylinder (user input) float cylinder_volume; // volume of cylinder(output) float cylinder_surface; // surface of cylinder(output) float prism_width; //width of prism(user input) float prism_depth; //depth of prism(user input) float prism_height; //height of prism (user input) bool cylinder_fit; //does cylinder fit into prism [true\false](output) float pi ; //declares pi }; //------------------------------------------------------------------------------ //the constructor for cylinder class. cylinder::cylinder() { cout<<"\nA new cylinder instance (object) is created:" << endl;//announces activation } //member function- sets diameter of cylinder and gets user inputed value float cylinder::set_cylinder_diameter(float cd)//declare cd(cylinder diameter) { //allow input greater than 0 & less than 20. sets cylinder diameter at 1.0 cylinder_diameter =(cd > 0.0 && cd < 20.0) ? cd : 1.0; cylinder_diameter = cd; //diameter given value of user input from main return cd; //return value } //member function- sets height of cylinder and gets user inputed value float cylinder::set_cylinder_height(float ch) { //allow input greater than 0 & less than 20. sets cylinder height at 1.0 cylinder_height = (ch > 0.0 && ch < 20.0) ? ch : 1.0; cylinder_height = ch; //height given value of user input from main return ch; //return value } //member function- sets volume and surface of cylinder at 0.0 Pi at 3.14159 void cylinder::set_other_data() { cylinder_volume=cylinder_surface=0.0; pi = 3.14159; } //member functiom calculates cylinder volume void cylinder::calculate_cylinder_volume() { cylinder_volume = (pi * (cylinder_diameter * cylinder_diameter ) *(cylinder_height))/4; } //member functiom calculates cylinder surface void cylinder::calculate_cylinder_surface() //it calculates the surface { pi = 3.14159; cylinder_surface= ((pi * (cylinder_diameter * cylinder_diameter)/2.0)) + (pi * cylinder_diameter * cylinder_height); } //displays cylinder volume and surface to user void cylinder::display_cylinder_results() { cout<<"\n\nThe total volume of the cylinder is: "<< cylinder_volume << endl; cout<<"\n\nThe surface area of the cylinder is: "<< cylinder_surface << endl; } //member function- sets width of prism and gets user inputed value float cylinder::set_prism_width(float pw) { //allow input greater than 0 & less than 20. sets prism width at 1.0 prism_width =(pw > 0.0 && pw < 20.0) ? pw: 1.0; prism_width = pw;//width given value of user input from main return pw; //return value } //member function- sets depth of prism and gets user inputed value float cylinder::set_prism_depth(float pd) { //allow input greater than 0 & less than 20. sets prism width at 1.0 prism_depth =(pd > 0.0 && pd < 20.0) ? pd : 1.0; prism_depth = pd; //depth given value of user input from main return pd; //return value } //member function- sets height of prism and gets user inputed value float cylinder::set_prism_height(float ph) { //allow input greater than 0 & less than 20. sets prism width at 1.0 prism_height = (ph > 0.0 && ph < 20.0) ? ph : 1.0; prism_height = ph; //prism heightgiven value of user input from main return ph; //return value } //member function determines if cylinder fits into prism, calculated //on inputed values from user for cylinder and prism dimensions bool cylinder:: prism(bool cf) { cylinder_fit = cf; // attribute fit given boolean value of cf input in main // set at c++ keyword false in main. //if statements to determine if cylinder fits into prism if (cylinder_height <= prism_height) { cylinder_fit = true; if((cylinder_diameter <= prism_width)&&(cylinder_diameter <= prism_depth)) { cylinder_fit = true; } if ((cylinder_diameter > prism_width)||(cylinder_diameter > prism_depth)) { cylinder_fit = false; } } if (cylinder_height > prism_height) { cylinder_fit = false; } return cf; //return boolean value } //member function determines display to user void cylinder:: fit_results_display() { //if statements to determine messages displayed based on cylinder_fit(true/false) //and inputed values for dimension of cylinder and prism //displays if cylinder fits and tangential edges if applicable if(cylinder_fit == true) { cout<<"\n\nThe cylinder fits inside the prism"<> cd; //accept cylinder diameter user input cout << "\nEnter the height of the cylinder: "; cin >>ch; //accept cylinder height user input //calls member functions of cylinder class for cylinder calulations b.set_cylinder_diameter(cd); //sets and gets diameter of cylinder b.set_cylinder_height(ch); //sets and gets height of cylinder b.set_other_data(); //sets volume and surface at 0.0 b.calculate_cylinder_volume(); //calculates volume of cylinder b.calculate_cylinder_surface(); //calculates surface of cylinder b.display_cylinder_results(); //displays cylinder results to user //declares and intiates attributes to be used for prism input //abbreviated input attribute names to be used by member function //for full name dimension attributes (ie. pw-prism_width) float pw=1.0,pd=1.0,ph=1.0; //set at 1.0 //attibute used in member function prism(cylinder_fit = cf) bool cf = false; //set boolean value for 'cf' at false. //prompt user to enter prism dimensions cout << "\n\nEnter the width of a regular prism: "; cin >> pw; //accept prism width user input cout << "\nEnter the depth of the regular prism: "; cin >> pd; //accept prism depth user input cout << "\nEnter the height of the regular prism: "; cin >>ph; //accept prism height user input //calls member functions of cylinder class for cylinder fit calculations b.set_prism_width(pw); //sets and gets prism width b.set_prism_depth(pd); //sets and gets prism depth b.set_prism_height(ph); //sets and gets prism height b.prism(cf); //calculates if cylinder fits in prism b.fit_results_display(); /*determines and displays appropriate messages for cylinder fit calculations*/ b.~cylinder(); //destroys the cylinder object display_end(); //runs the "file scope" function to report end of program } //member function- defines the "file scope" where the program should end void display_end(void) { // exit message to user cout<<"\n\nThank you for using the program." << "\n" << endl; system("pause"); } //------------------------------------------------------------------------------