196040

F.S. untested

Mar 18th, 2021 (edited)
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. //Create a class Cylinder. It should have the following members:
  2. //Two private members: length, radius.
  3. //A default constructor without parameters.
  4. //A parametrized constructor, with two parameters, to set the corresponding value of the private members (make use of the this pointer //upon initialization of the members).
  5. //A public double volume() function which calculates the volume of the cylinder. (volume = pi * radius^2 * length)
  6. //A public double surface() function which calculates the surface area of the cylinder. (surface = 2 * pi * radius * length)
  7. //In the main() function, create an array of Cylinder objects. Input the number of cylinders from the keyboard and enter their //parameters.
  8. //Output the volumes and surface areas of all cylinders.
  9. //Output the cylinders with the smallest volume and surface areas and their location in memory.
  10. #include<iostream>
  11. using namespace std;
  12. class Cylinder {
  13. private:
  14.     double length, radius;
  15. public:
  16.     Cylinder() {
  17.         this->length = 0.0;
  18.         this->radius = 0.0;
  19.     }
  20.     Cylinder(double length, double radius) {
  21.         this->length = length;
  22.         this->radius = radius;
  23.     }
  24.     Cylinder(const Cylinder &copy) {
  25.         this->length = copy.length;
  26.         this->radius = copy.radius;
  27.     }
  28.     Cylinder &operator=(const Cylinder &copy) {
  29.         if(this!=&copy) {
  30.             this->length = copy.length;
  31.             this->radius = copy.radius;
  32.     }
  33.     return *this;
  34.     }
  35.     double volume() {
  36.         double volumee = 3.1415 * this->radius*2 * this->length;
  37.         return volumee;
  38.     }
  39.     double surface() {
  40.         double surfacee = 2 * 3.1415 * this->radius * this->length;
  41.         return surfacee;
  42.     }
  43. };
  44. int main() {
  45.     int n;
  46.     cout<<"Please enter the number of cylinders"<<endl;
  47.     cin>>n;
  48.     Cylinder * cil[100];
  49.     double a, b;
  50.     int indeks1 = 0;
  51.     int indeks2 = 0;
  52.     double cmpvolume = cil[0]->volume();
  53.     double cmpsurface = cil[0]->surface();
  54.     for(int i=0; i<n; i++)    {
  55.         cout<<"Please enter the length and radius for cylinder "<<i+1<<" respectively."<<endl;
  56.         cin>>a>>b;
  57.         cil[i] = new Cylinder(a, b);
  58.         if(cil[i]->volume() < cmpvolume) {
  59.             cmpvolume = cil[i]->volume();
  60.             indeks1 = i;
  61.         }
  62.         if(cil[i]->surface() < cmpsurface) {
  63.             cmpsurface = cil[i]->surface();
  64.             indeks2 = i;
  65.         }
  66.         cout<<"Volume is "<<cil[i]->volume();
  67.         cout<<"Surface is "<<cil[i]->surface();
  68.     }
  69.     cout<<"Smallest volume is "<<cil[indeks1]->volume()<<". Location is "<<&cil[indeks1];
  70.     cout<<"Smallest surface is "<<cil[indeks2]->surface()<<". Location is "<<&cil[indeks2];
  71. }
Add Comment
Please, Sign In to add comment