C++ Cylinder Area Homework: Find & Program Solution

In summary, the programmer is trying to solve a problem in C++ involving a circle and a cylinder, but they are not able to compile the program.
  • #1
Const@ntine
285
18

Homework Statement



Find the are of the cylinder, by creating a program in C++.

2. The attempt at a solution

So far I have:

C:
#include <iostream>
using namespace std;

const double PI = 3.14159;

class point
{
 protected:
  int x;
  int y;
 
 public:
  point()
  {
  }
  point (int xvalue, int yvalue)
  {
   x = xvalue;
   y = yvalue;
  }
  void setX(int value)
  {
   x = value;
  }
  void setY(int value)
  {
   y = value;
  }
  int getX(void)
  {
   return x;
  }
  int getY(void)
  {
   return y;
  }
  void print(void)
  {
   cout << "(" << x << "," << y << ")" << endl;
  }
};

class circle : public point
{
 private:
  double radius;
 
 public:
  circle()
  {
  }
  circle(int xvalue, int yvalue, double radiusvalue)
  {
   x = xvalue;
   y = yvalue;
   radius = radiusvalue;
  }
  void setRadius(double value)
  {
   radius = value;
  }
  double getRadius (void)
  {
   return radius;
  }
  double getArea(void)
  {
   return PI*radius*radius;
  }
  void print(void)
  {
   cout << "Center: (" << x << "," << y << ") ";
   cout << "Radius : " << radius << endl;
  }
};

class cylinder : circle
{
 private:
  double height;
 
 public:
  cylinder()
  {
  }
  cylinder(int xvalue, int yvalue, int rvalue, double hvalue)
  {
   x = xvalue;
   y = yvalue;
   radius = rvalue;
   height = hvalue;
  }
  void setHeight(double value)
  {
   height = value;
  }
  double getHeight(void)
  {
   return height;
  }
  double getArea(void)
  {
   return 2*circle::getArea() + 2*PI*radius*height;
  }
  void print(void)
  {
   cout << "Center: (" << x << "," << y << ") ";
   cout << "Radius: " << radius << " Height: " << height << endl;
  }
};

int main()
{
 cylinder x(37, 43, 2.5, 3);

 x.print();
 cout << "Cylinder area: " << x.getArea() << endl;

 x.setX(25);
 x.setY(35);
 x.setRadius(1.5);
 x.setHeight(2);
 x.print();

 return 0;
}

But I can't compile it yet. I get:

---------- Capture Output ----------
> "C:\Emerald\gcpp.bat" AC.cpp AC
AC.cpp: In constructor 'cylinder::cylinder(int, int, int, double)':
AC.cpp:46:10: error: 'double circle::radius' is private
double radius;
^
AC.cpp:90:4: error: within this context
radius = rvalue;
^
AC.cpp: In member function 'double cylinder::getArea()':
AC.cpp:46:10: error: 'double circle::radius' is private
double radius;
^
AC.cpp:103:38: error: within this context
return 2*circle::getArea() + 2*PI*radius*height;
^
AC.cpp: In member function 'void cylinder::print()':
AC.cpp:46:10: error: 'double circle::radius' is private
double radius;
^
AC.cpp:108:26: error: within this context
cout << "Radius: " << radius << " Height: " << height << endl;
^
AC.cpp: In function 'int main()':
AC.cpp:21:8: error: 'void point::setX(int)' is inaccessible
void setX(int value)
^
AC.cpp:119:11: error: within this context
x.setX(25);
^
AC.cpp:119:11: error: 'point' is not an accessible base of 'cylinder'
AC.cpp:25:8: error: 'void point::setY(int)' is inaccessible
void setY(int value)
^
AC.cpp:120:11: error: within this context
x.setY(35);
^
AC.cpp:120:11: error: 'point' is not an accessible base of 'cylinder'
AC.cpp:58:8: error: 'void circle::setRadius(double)' is inaccessible
void setRadius(double value)
^
AC.cpp:121:17: error: within this context
x.setRadius(1.5);
^
AC.cpp:121:17: error: 'circle' is not an accessible base of 'cylinder'

> Terminated with exit code 1.

I'm guessing the problem is that the cylinder part of the program can't access the circle class, and use the radius. Problem is, I know little next to nothing about classes and whatnot, so I'm at a loss here. I set up that program with what was given by a previous exercise, plus some other programs I studied, but I'm stuck now.

Any help is appreciated!
 
Physics news on Phys.org
  • #2
Darthkostis said:

Homework Statement



Find the are of the cylinder, by creating a program in C++.

2. The attempt at a solution

So far I have:

C:
#include <iostream>
using namespace std;

const double PI = 3.14159;

class point
{
 protected:
  int x;
  int y;
 
 public:
  point()
  {
  }
  point (int xvalue, int yvalue)
  {
   x = xvalue;
   y = yvalue;
  }
  void setX(int value)
  {
   x = value;
  }
  void setY(int value)
  {
   y = value;
  }
  int getX(void)
  {
   return x;
  }
  int getY(void)
  {
   return y;
  }
  void print(void)
  {
   cout << "(" << x << "," << y << ")" << endl;
  }
};

class circle : public point
{
 private:
  double radius;
 
 public:
  circle()
  {
  }
  circle(int xvalue, int yvalue, double radiusvalue)
  {
   x = xvalue;
   y = yvalue;
   radius = radiusvalue;
  }
  void setRadius(double value)
  {
   radius = value;
  }
  double getRadius (void)
  {
   return radius;
  }
  double getArea(void)
  {
   return PI*radius*radius;
  }
  void print(void)
  {
   cout << "Center: (" << x << "," << y << ") ";
   cout << "Radius : " << radius << endl;
  }
};

class cylinder : circle
{
 private:
  double height;
 
 public:
  cylinder()
  {
  }
  cylinder(int xvalue, int yvalue, int rvalue, double hvalue)
  {
   x = xvalue;
   y = yvalue;
   radius = rvalue;
   height = hvalue;
  }
  void setHeight(double value)
  {
   height = value;
  }
  double getHeight(void)
  {
   return height;
  }
  double getArea(void)
  {
   return 2*circle::getArea() + 2*PI*radius*height;
  }
  void print(void)
  {
   cout << "Center: (" << x << "," << y << ") ";
   cout << "Radius: " << radius << " Height: " << height << endl;
  }
};

int main()
{
 cylinder x(37, 43, 2.5, 3);

 x.print();
 cout << "Cylinder area: " << x.getArea() << endl;

 x.setX(25);
 x.setY(35);
 x.setRadius(1.5);
 x.setHeight(2);
 x.print();

 return 0;
}

But I can't compile it yet. I get:

---------- Capture Output ----------
> "C:\Emerald\gcpp.bat" AC.cpp AC
AC.cpp: In constructor 'cylinder::cylinder(int, int, int, double)':
AC.cpp:46:10: error: 'double circle::radius' is private
double radius;
^
AC.cpp:90:4: error: within this context
radius = rvalue;
^
AC.cpp: In member function 'double cylinder::getArea()':
AC.cpp:46:10: error: 'double circle::radius' is private
double radius;
^
AC.cpp:103:38: error: within this context
return 2*circle::getArea() + 2*PI*radius*height;
^
AC.cpp: In member function 'void cylinder::print()':
AC.cpp:46:10: error: 'double circle::radius' is private
double radius;
^
AC.cpp:108:26: error: within this context
cout << "Radius: " << radius << " Height: " << height << endl;
^
AC.cpp: In function 'int main()':
AC.cpp:21:8: error: 'void point::setX(int)' is inaccessible
void setX(int value)
^
AC.cpp:119:11: error: within this context
x.setX(25);
^
AC.cpp:119:11: error: 'point' is not an accessible base of 'cylinder'
AC.cpp:25:8: error: 'void point::setY(int)' is inaccessible
void setY(int value)
^
AC.cpp:120:11: error: within this context
x.setY(35);
^
AC.cpp:120:11: error: 'point' is not an accessible base of 'cylinder'
AC.cpp:58:8: error: 'void circle::setRadius(double)' is inaccessible
void setRadius(double value)
^
AC.cpp:121:17: error: within this context
x.setRadius(1.5);
^
AC.cpp:121:17: error: 'circle' is not an accessible base of 'cylinder'

> Terminated with exit code 1.

I'm guessing the problem is that the cylinder part of the program can't access the circle class, and use the radius. Problem is, I know little next to nothing about classes and whatnot, so I'm at a loss here. I set up that program with what was given by a previous exercise, plus some other programs I studied, but I'm stuck now.

Any help is appreciated!

I haven't tried to compile the code, but I think the issue has to do with the difference between "private" and "protected" in your variable (property) declarations.

"public" means that the variable (property) can be accessed anywhere.

"protected" means that the variable (property) can be accessed by member functions (i.e. methods) within the class, as well as other classes that inherit from that class. (Friends are also able to access protected variables/properties, but that's not important in this case.)

"private" means that the variable (property) can only be accessed by member functions (i.e. methods) within the class. Classes that inherit from the parent class do not have access to the parent class' private variables/properties.

Make sure that a child class is not trying to access a parent's class' private properties/variables.
 
  • #3
collinsmark said:
I haven't tried to compile the code, but I think the issue has to do with the difference between "private" and "protected" in your variable (property) declarations.

"public" means that the variable (property) can be accessed anywhere.

"protected" means that the variable (property) can be accessed by member functions (i.e. methods) within the class, as well as other classes that inherit from that class. (Friends are also able to access protected variables/properties, but that's not important in this case.)

"private" means that the variable (property) can only be accessed by member functions (i.e. methods) within the class. Classes that inherit from the parent class do not have access to the parent class' private variables/properties.

Make sure that a child class is not trying to access a parent's class' private properties/variables.

Thanks for the reply! I looked around a bit based on what you said, and came up with this:

C:
#include <iostream>
using namespace std;

const double PI = 3.14159;

class point
{
 protected:
  int x;
  int y;
 
 public:
  point()
  {
  }
  point (int xvalue, int yvalue)
  {
   x = xvalue;
   y = yvalue;
  }
  void setX(int value)
  {
   x = value;
  }
  void setY(int value)
  {
   y = value;
  }
  int getX(void)
  {
   return x;
  }
  int getY(void)
  {
   return y;
  }
  void print(void)
  {
   cout << "(" << x << "," << y << ")" << endl;
  }
};

class circle : public point
{
 protected:
  double radius;
 
 public:
  circle()
  {
  }
  circle(int xvalue, int yvalue, double radiusvalue)
  {
   x = xvalue;
   y = yvalue;
   radius = radiusvalue;
  }
  void setRadius(double value)
  {
   radius = value;
  }
  double getRadius (void)
  {
   return radius;
  }
  double getArea(void)
  {
   return PI*radius*radius;
  }
  void print(void)
  {
   cout << "Center: (" << x << "," << y << ") ";
   cout << "Radius : " << radius << endl;
  }
};

class cylinder : public circle
{
 private:
  double height;
 
 public:
  cylinder()
  {
  }
  cylinder(int xvalue, int yvalue, int rvalue, double hvalue)
  {
   x = xvalue;
   y = yvalue;
   radius = rvalue;
   height = hvalue;
  }
  void setHeight(double value)
  {
   height = value;
  }
  double getHeight(void)
  {
   return height;
  }
  double getArea(void)
  {
   return 2*circle::getArea() + 2*PI*radius*height;
  }
  void print(void)
  {
   cout << "Center: (" << x << "," << y << ") ";
   cout << "Radius: " << radius << " Height: " << height << endl;
  }
};

int main()
{
 cylinder x(37, 43, 2.5, 3);

 x.print();
 cout << "Cylinder area: " << x.getArea() << endl;

 x.setX(25);
 x.setY(35);
 x.setRadius(1.5);
 x.setHeight(2);
 x.print();
 cout << "Cylinder area: " << x.getArea() << endl;

 return 0;
}

When I execute it, I get:

Center: (37,43) Radius: 2 Height: 3
Cylinder area: 62.8318
Center: (25,35) Radius: 1.5 Height: 2
Cylinder area: 32.9867
Press any key to continue . . .


Which is correct if you do the computations by hand. So I guess that's it? Are there any logical errors or something along those lines?
 
  • #4
Darthkostis said:
So I guess that's it? Are there any logical errors or something along those lines?
Logically speaking, it looks good to me. :smile: (as far as I can tell by glancing through it.)
 
  • Like
Likes Const@ntine
  • #5
[Edit: Post deleted. Nevermind. You already did what I was about to suggest correctly.]
 
  • Like
Likes Const@ntine
  • #6
collinsmark said:
Logically speaking, it looks good to me. :smile: (as far as I can tell by glancing through it.)
Nice! Thanks a lot for the help!
 
  • Like
Likes collinsmark

What is the formula for finding the area of a cylinder in C++?

The formula for finding the area of a cylinder is 2 * pi * radius * (radius + height), where pi is the mathematical constant and radius and height are the dimensions of the cylinder.

Can you provide an example code for finding the area of a cylinder in C++?

Yes, here is an example code for finding the area of a cylinder using the formula mentioned above:

double pi = 3.14159; //declaring pi as a double data typedouble radius = 5.0; //declaring radius as a double data typedouble height = 10.0; //declaring height as a double data typedouble area = 2 * pi * radius * (radius + height); //calculating the area using the formulacout << "The area of the cylinder is: " << area << endl; //printing the result

What are the data types used in the formula for finding the area of a cylinder in C++?

The data types used in the formula for finding the area of a cylinder are double and int. Double is used to represent the decimal values and int is used to represent whole numbers.

What happens if the user enters negative values for the dimensions of the cylinder?

If the user enters negative values for the dimensions of the cylinder, the program will still calculate the area using the given formula. However, the result will be negative, which does not make sense in the context of finding the area of a cylinder. It is important for the user to enter positive values for accurate results.

Is there a library function in C++ for finding the area of a cylinder?

No, there is no specific library function for finding the area of a cylinder in C++. However, there are library functions for calculating the area and circumference of a circle, which can be used to find the area of the circular bases of a cylinder and then the total area can be calculated using the formula.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
758
  • Engineering and Comp Sci Homework Help
Replies
8
Views
844
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
987
  • Engineering and Comp Sci Homework Help
Replies
15
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
Back
Top