O
Old Wolf
Hi Dan. You seem to be suffering some serious misconceptions about
object-oriented design. I really recommend you buy a GOOD book
(not a book written before 1999, and not a silly book). There are
lists of recommended books at http://www.accu.org/ .
Also, you are having trouble enough with one class, let alone
a hierarchy of classes. I suggest you try writing "class Circle"
without deriving it from Shape, and get everything working.
Then you can write "class Triangle" (without deriving it from Shape)
and get everything working. Then you can look at the two classes
and move their common features into the class Shape.
You seem to think that 'class Circle' is an umbrella for all things
related to circles. This is wrong. A Circle object is ONE CIRCLE.
This one circle has some properties: a centre and a radius. There
are things you can do with this one circle: eg. change its radius,
draw it, colour it in, move it, etc.
Your function "ShowCircle" seems to be a function that draws
_different_ circles. Showing other circles is not something that
you do with one circle. You should change this to be a function
that shows the one circle, eg.
void ShowCircle(void)
{ cout << "this circle has radius: "<< itsRadius << endl; }
and then if you want a function to display many circles, create
a free function:
void ShowCircles(Circle *ptr, int n_circles)
{ for (int i = 0; i < n_circles; ++i) ptr.ShowCircle(); }
This is GOOD design. I don't know why you think it violates
encapsulation.
Now let me return to your Shape class:
What is this function supposed to do ?? A shape object is a shape
(that already exists). How can you create an object that already exists?
If this function is not meant to apply to one shape, then it should
not be a member of this class.
This sticks out like a sore thumb. All of the code that relates
to the Shape class should make no mention of Circle, Cylinder, etc.
If it does, you need to rethink your design.
You appear to be creating a Shape and then trying to convert that
Shape into a Circle or whatever, based on what the user wants.
That is WRONG. You cannot do that in C++. What you must do is find
out what the user wants, and then create a Circle, Cylinder, etc.
(Note - in order to do this, you need to use language features which
you haven't used in your program so far (so I'm guessing you wouldn't
have any idea how to do this). Objects are either one type, or another.
A Circle is NOT a shape object. If a function has to return a Shape by
value, or take a Shape by value, you cannot give it a Circle.
You have to use a pointer or a reference to a Shape (which can, in fact,
point or refer to a Circle).
"volume" should not really be a member variable. It's bad design
(although not as awful as the things I pointed out above).
The reason it is bad is that in all of your derived classes (eg.
Circle), then whenever you change "radius" you also have to remember
to change "volume". It would be better if there were a function
to calculate the volume based on the radius.
Finally, the topic of this thread was "sharing information in a class".
Classes do not have information. Objects have information. An object's
member functions have access to all the information of that ONE OBJECT.
A class is a set of instructions that say how to make an object, and what
that object can do once it has been created.
What don't you understand about that?
Here is some better code. Study the differences between this code and yours.
#include <iostream>
using namespace std;
class Shape
{
public:
virtual double CalculateVolume() { return 0; }
virtual void Show()
{ cout << "Shape volume: " << CalculateVolume() << endl; }
};
// The word 'virtual' means that if we have a pointer/reference to
// a Shape, and then call CalculateVolume(), then if the Shape is
// actually a Circle, then the circle version of CalculateDouble()
// will be called.
class Circle: public Shape
{
private:
double radius;
public:
Circle(double r): radius(r) {}
double CalculateVolume() { return 3.14159 * radius * radius; }
};
class Square: public Shape
{
private:
double side_length;
public:
Square(double len): side_length(len), colour(0) {}
double CalculateVolume() { return side_length * side_length; }
int colour;
virtual void Show()
{ Shape::Show(); cout << "Square colour: " << colour << endl; }
};
Shape *GetNewShape()
{
int n;
for(;
{
cout << "Enter 1 for Circle or 2 for Square (0 to quit)" << endl;
cin >> n;
if (n == 1)
return new Circle(6.0);
if (n == 2)
return new Square(3.0);
if (n == 0)
return 0;
}
}
int main(void)
{
Circle circle(5.);
Square square(4.);
Shape *shape_ptr;
shape_ptr = &circle;
shape_ptr->Show();
shape_ptr = □
shape_ptr->Show(); // same function, different results
for (;
{
shape_ptr = GetNewShape();
if (shape_ptr == 0)
break;
shape_ptr->Show();
delete shape_ptr; // we must now destroy the shape we "new"'d
}
return 0;
// "circle" and "square" get destroyed automatically because they weren't
// created via "new"
}
If you want to update this example to allow user input of the
radius and colour, for example, you could add another virtual
member function UpdateProperties(). This would take one existing
circle and allow the user to change its radius (but NOT allow
the user to change its type).
object-oriented design. I really recommend you buy a GOOD book
(not a book written before 1999, and not a silly book). There are
lists of recommended books at http://www.accu.org/ .
Also, you are having trouble enough with one class, let alone
a hierarchy of classes. I suggest you try writing "class Circle"
without deriving it from Shape, and get everything working.
Then you can write "class Triangle" (without deriving it from Shape)
and get everything working. Then you can look at the two classes
and move their common features into the class Shape.
class Circle: public Shape
{
public:
void DrawCircle();
void SetVolume(){
cout<<"Enter the radius in cm: " ; cin>> itsRadius;
volume = (4/3) * pow(itsRadius,3) * PI ;}
void ShowCircle(Circle* s, int* count);
protected:
float itsRadius;
};
You seem to think that 'class Circle' is an umbrella for all things
related to circles. This is wrong. A Circle object is ONE CIRCLE.
This one circle has some properties: a centre and a radius. There
are things you can do with this one circle: eg. change its radius,
draw it, colour it in, move it, etc.
Your function "ShowCircle" seems to be a function that draws
_different_ circles. Showing other circles is not something that
you do with one circle. You should change this to be a function
that shows the one circle, eg.
void ShowCircle(void)
{ cout << "this circle has radius: "<< itsRadius << endl; }
and then if you want a function to display many circles, create
a free function:
void ShowCircles(Circle *ptr, int n_circles)
{ for (int i = 0; i < n_circles; ++i) ptr.ShowCircle(); }
This is GOOD design. I don't know why you think it violates
encapsulation.
Now let me return to your Shape class:
class Shape
{
public:
void CreateShape();
What is this function supposed to do ?? A shape object is a shape
(that already exists). How can you create an object that already exists?
If this function is not meant to apply to one shape, then it should
not be a member of this class.
void Shape::CreateShape()
{
Circle draw1[10];
Cylinder draw2[10];
Triangle draw3[10];
This sticks out like a sore thumb. All of the code that relates
to the Shape class should make no mention of Circle, Cylinder, etc.
If it does, you need to rethink your design.
You appear to be creating a Shape and then trying to convert that
Shape into a Circle or whatever, based on what the user wants.
That is WRONG. You cannot do that in C++. What you must do is find
out what the user wants, and then create a Circle, Cylinder, etc.
(Note - in order to do this, you need to use language features which
you haven't used in your program so far (so I'm guessing you wouldn't
have any idea how to do this). Objects are either one type, or another.
A Circle is NOT a shape object. If a function has to return a Shape by
value, or take a Shape by value, you cannot give it a Circle.
You have to use a pointer or a reference to a Shape (which can, in fact,
point or refer to a Circle).
float ChooseColor();
void DisplayShape();
float GetVolume(){ return volume; }
protected:
float volume;
};
"volume" should not really be a member variable. It's bad design
(although not as awful as the things I pointed out above).
The reason it is bad is that in all of your derived classes (eg.
Circle), then whenever you change "radius" you also have to remember
to change "volume". It would be better if there were a function
to calculate the volume based on the radius.
Finally, the topic of this thread was "sharing information in a class".
Classes do not have information. Objects have information. An object's
member functions have access to all the information of that ONE OBJECT.
A class is a set of instructions that say how to make an object, and what
that object can do once it has been created.
What don't you understand about that?
Here is some better code. Study the differences between this code and yours.
#include <iostream>
using namespace std;
class Shape
{
public:
virtual double CalculateVolume() { return 0; }
virtual void Show()
{ cout << "Shape volume: " << CalculateVolume() << endl; }
};
// The word 'virtual' means that if we have a pointer/reference to
// a Shape, and then call CalculateVolume(), then if the Shape is
// actually a Circle, then the circle version of CalculateDouble()
// will be called.
class Circle: public Shape
{
private:
double radius;
public:
Circle(double r): radius(r) {}
double CalculateVolume() { return 3.14159 * radius * radius; }
};
class Square: public Shape
{
private:
double side_length;
public:
Square(double len): side_length(len), colour(0) {}
double CalculateVolume() { return side_length * side_length; }
int colour;
virtual void Show()
{ Shape::Show(); cout << "Square colour: " << colour << endl; }
};
Shape *GetNewShape()
{
int n;
for(;
{
cout << "Enter 1 for Circle or 2 for Square (0 to quit)" << endl;
cin >> n;
if (n == 1)
return new Circle(6.0);
if (n == 2)
return new Square(3.0);
if (n == 0)
return 0;
}
}
int main(void)
{
Circle circle(5.);
Square square(4.);
Shape *shape_ptr;
shape_ptr = &circle;
shape_ptr->Show();
shape_ptr = □
shape_ptr->Show(); // same function, different results
for (;
{
shape_ptr = GetNewShape();
if (shape_ptr == 0)
break;
shape_ptr->Show();
delete shape_ptr; // we must now destroy the shape we "new"'d
}
return 0;
// "circle" and "square" get destroyed automatically because they weren't
// created via "new"
}
If you want to update this example to allow user input of the
radius and colour, for example, you could add another virtual
member function UpdateProperties(). This would take one existing
circle and allow the user to change its radius (but NOT allow
the user to change its type).