C++ Cylinder Area Homework: Find & Program Solution

  • #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

Similar threads

Replies
3
Views
1K
Replies
8
Views
1K
Replies
2
Views
3K
Replies
2
Views
1K
Replies
2
Views
1K
Replies
2
Views
1K
Replies
15
Views
7K
Replies
1
Views
2K
Back
Top