Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Create a class Cylinder. It should have the following members:
- //Two private members: length, radius.
- //A default constructor without parameters.
- //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).
- //A public double volume() function which calculates the volume of the cylinder. (volume = pi * radius^2 * length)
- //A public double surface() function which calculates the surface area of the cylinder. (surface = 2 * pi * radius * length)
- //In the main() function, create an array of Cylinder objects. Input the number of cylinders from the keyboard and enter their //parameters.
- //Output the volumes and surface areas of all cylinders.
- //Output the cylinders with the smallest volume and surface areas and their location in memory.
- #include<iostream>
- using namespace std;
- class Cylinder {
- private:
- double length, radius;
- public:
- Cylinder() {
- this->length = 0.0;
- this->radius = 0.0;
- }
- Cylinder(double length, double radius) {
- this->length = length;
- this->radius = radius;
- }
- Cylinder(const Cylinder ©) {
- this->length = copy.length;
- this->radius = copy.radius;
- }
- Cylinder &operator=(const Cylinder ©) {
- if(this!=©) {
- this->length = copy.length;
- this->radius = copy.radius;
- }
- return *this;
- }
- double volume() {
- double volumee = 3.1415 * this->radius*2 * this->length;
- return volumee;
- }
- double surface() {
- double surfacee = 2 * 3.1415 * this->radius * this->length;
- return surfacee;
- }
- };
- int main() {
- int n;
- cout<<"Please enter the number of cylinders"<<endl;
- cin>>n;
- Cylinder * cil[100];
- double a, b;
- int indeks1 = 0;
- int indeks2 = 0;
- double cmpvolume = cil[0]->volume();
- double cmpsurface = cil[0]->surface();
- for(int i=0; i<n; i++) {
- cout<<"Please enter the length and radius for cylinder "<<i+1<<" respectively."<<endl;
- cin>>a>>b;
- cil[i] = new Cylinder(a, b);
- if(cil[i]->volume() < cmpvolume) {
- cmpvolume = cil[i]->volume();
- indeks1 = i;
- }
- if(cil[i]->surface() < cmpsurface) {
- cmpsurface = cil[i]->surface();
- indeks2 = i;
- }
- cout<<"Volume is "<<cil[i]->volume();
- cout<<"Surface is "<<cil[i]->surface();
- }
- cout<<"Smallest volume is "<<cil[indeks1]->volume()<<". Location is "<<&cil[indeks1];
- cout<<"Smallest surface is "<<cil[indeks2]->surface()<<". Location is "<<&cil[indeks2];
- }
Add Comment
Please, Sign In to add comment