C++ Cylinder Area Homework: Find & Program Solution

Click For Summary

Discussion Overview

The discussion revolves around a homework problem requiring participants to find the area of a cylinder by creating a C++ program. The focus is on programming concepts, specifically the use of classes and inheritance in C++.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares a C++ program attempting to calculate the area of a cylinder but encounters compilation errors related to access control for class members.
  • The participant notes that the cylinder class cannot access the radius variable from the circle class due to its private access modifier.
  • Another participant suggests that the issue may stem from the difference between "private" and "protected" access in class declarations, implying that changing the access level might resolve the issue.
  • There are multiple references to specific compilation errors, indicating that the program fails to compile due to inaccessible members from base classes.

Areas of Agreement / Disagreement

Participants express uncertainty about the best approach to resolve the compilation errors, with differing opinions on the implications of access modifiers in class inheritance. No consensus is reached on a definitive solution.

Contextual Notes

The discussion highlights limitations in understanding class access control and inheritance in C++, which may affect the ability to compile and run the program successfully.

Const@ntine
Messages
285
Reaction score
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)
  {
   count << "(" << 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)
  {
   count << "Center: (" << x << "," << y << ") ";
   count << "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)
  {
   count << "Center: (" << x << "," << y << ") ";
   count << "Radius: " << radius << " Height: " << height << endl;
  }
};

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

 x.print();
 count << "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
count << "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
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)
  {
   count << "(" << 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)
  {
   count << "Center: (" << x << "," << y << ") ";
   count << "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)
  {
   count << "Center: (" << x << "," << y << ") ";
   count << "Radius: " << radius << " Height: " << height << endl;
  }
};

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

 x.print();
 count << "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
count << "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.
 
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)
  {
   count << "(" << 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)
  {
   count << "Center: (" << x << "," << y << ") ";
   count << "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)
  {
   count << "Center: (" << x << "," << y << ") ";
   count << "Radius: " << radius << " Height: " << height << endl;
  }
};

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

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

 x.setX(25);
 x.setY(35);
 x.setRadius(1.5);
 x.setHeight(2);
 x.print();
 count << "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?
 
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   Reactions: Const@ntine
[Edit: Post deleted. Nevermind. You already did what I was about to suggest correctly.]
 
  • Like
Likes   Reactions: Const@ntine
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   Reactions: collinsmark

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 15 ·
Replies
15
Views
8K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K