sharing information in a class

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.
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 = &square;
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).
 
J

John Harrison

Dan said:
Jesus, look at the code I wrote

circle[count_circle] = CreateCircle();

look at the code you wrote

circle[count_circle]=circle.CreateCircle();


I thought that was not good encapsulation. I was just trying to put whatever
is concern with circle in the Circle class. Now you are asking me to make
CreateCircle global right ?

Right! Your way is a bad way. Encapsulation does not mean put everything to
do with a circle in the Circle class. It means making Circle a logical
entity with a clean reusable interface.

A Circle is a concept, it has radius, a center point etc. Asking the user
how big a circle radius is has nothing to do with the concept of a circle,
and should not go in the circle class.

Think about reusability. Suppose you wanted to reuse your Circle class in a
different program, a program that did some calculation with circles but
didn't ask the user about how big the radius was. What would the point of
you CreateCricle method be then? Every circle has a radius, so radius is a
good thing to put in a Circle class. Not every program need to ask the user
how big a circle is, so asking that is not a good thing to put in a Circle
class.

john
 
D

Dan

Right! Your way is a bad way. Encapsulation does not mean put everything to
do with a circle in the Circle class. It means making Circle a logical
entity with a clean reusable interface.

A Circle is a concept, it has radius, a center point etc. Asking the user
how big a circle radius is has nothing to do with the concept of a circle,
and should not go in the circle class.

Think about reusability. Suppose you wanted to reuse your Circle class in a
different program, a program that did some calculation with circles but
didn't ask the user about how big the radius was. What would the point of
you CreateCricle method be then? Every circle has a radius, so radius is a
good thing to put in a Circle class. Not every program need to ask the user
how big a circle is, so asking that is not a good thing to put in a Circle
class.

john

Yes Well here it is John,
I should of inplemented Display outside the class, but I will work on that
later:
The trianlge part is out for now. it does not work ..
BUT WOuld any one know how to modify my valadation function ( at the end
of the code) so that the program don't exit , but just return to the menu.
I tried a call to the menu , but it wouls keep finishing to ask the
questions from the function it came from.. So I had to put exit, but , that
is not good. ..


#include <iostream>
#include<conio.h>
#include<cmath>
#include <algorithm>
using namespace std;

const double PI = 3.14159;
void Validate(float &);
int Validate_positive(float &);


class Point {
int x,y,c;
public:
Point(int x1, int y1, int c1) {x = x1; y = y1; c = c1;}
Point() { x = y = c = 0;}
void get(int &u, int &v, int &w) {
u = x; v = y;w = c; }
};

class Shape {
Point p1;
public:
Shape() { }
Shape(Point p) : p1(p)
{ }

void get(int &a, int &b, int &c) {
p1.get(a,b,c); }

};

class Circle: public Shape
{
public:
Circle(){};
Circle(Point p, float r) : Shape(p) {radius = r;SetArea(); }

void SetArea() {area= PI * radius * radius; }
float GetArea(){ return area; }

void display() {
int x,y,c;
get(x,y,c);
cout <<"This circle has center : ("<<x<<","<<y<<","<<c<<") and radius :"<<
radius<<" and area:" <<area<<endl; }
private:
float radius;
float area;
};

class Cylinder: public Shape {

public:
Cylinder(){};
Cylinder(Point p, float r, float l) : Shape(p) {radius = r; height = l;
SetArea(); }

void SetArea() {area= PI * radius * radius * height; }
float GetArea(){ return area; }

void display() {
int x,y,c;
get(x,y,c);
cout <<"This cylinder has center : ("<<x<<","<<y<<","<<c<<") and radius :"<<
radius <<endl;
cout<< " and height:" << height <<" and volume: " <<area <<endl; }

private:
float height;
float radius;
float area;

};
/*
class Triangle: public Shape {
public:
Triangle(){};
Triangle(Point p, Point p1, Point p2,) : Shape(p){}
/*float b,h;

void SetArea() {
int x,y,c;
get(x,y,c);
b = sqrt( (pow(p1.x - p.x,2)) + (pow(p1.y -p.y,2)) );
h = sqrt( ( pow(p1.x - p2.x, 2) + pow(p1.y - p2.y ,2) ) - (
ow(b,2) ) );
area = (b * h) /2 ; }

float GetArea(){ return 333;} //area; }

void display() {
int x,y,c;
get(x,y,c);
cout <<"This triangle has points : ("<<p.x<<","<<p.y<<","<<p.c<<") and
"<<endl;
cout <<"This triangle has points : ("<<p1.x<<","<<p1.y<<","<<p1.c<<") and
"<<endl;
cout <<"This triangle has points : ("<<p2.x<<","<<p2.y<<","<<p2.c<<") and
area :"<< area <<endl; }

private:
float area;
Point p;
Point p1;
Point p2;

};
*/
void CreateCircle(Circle& oneCircle) {
float x,y,c, z;

cout<<" Enter the Center Point of the circle, X-Axis: " ;
cin>> x ;
Validate(x);
Validate_positive(x);
cout<<endl;
cout<<" Enter the Center Point of the circle, Y-Axis: " ;
cin>> y ;
Validate(y);
Validate_positive(y);
cout<<" Enter the Color of that point: " ;
cin>> c ;
Validate(c);
Validate_positive(c);

cout<<" Enter the radius of the circle: " ;
cin>> z;
Validate(z);
Validate_positive(z);

Point center(x,y,c);
oneCircle = Circle(center, z);
}

void DisplayCircle(Circle *myCircles, int *countCircles)
{
for (int i = 0; i < *countCircles; i++)
{cout<<" Here is circle # " << i+1 <<endl;
myCircles.display();
}
}
/*
void CreateTriangle(Triangle& oneTriangle) {

float x1,y1,c1,x2,y2,c2,x3,y3,c3;
//Point 1
cout<<" Enter the Lower Left point, X-Axis: " ;
cin>> x1 ;
cout<<endl;
cout<<" Enter the Lower Left point, Y-Axis: " ;
cin>> y1 ;
cout<<endl;
cout<<" Enter the Color of that point: " ;
cin>> c1 ;
cout<<endl;
Point p1((x1,y1,c1));


//Point 2
cout<<" Enter the Lower Right point, X-Axis: " ;
cin>> x2 ;
cout<<endl;
cout<<" Enter the Lower Right point, Y-Axis: " ;
cin>> y2 ;
cout<<endl;
cout<<" Enter the Color of that point: " ;
cin>> c2 ;
cout<<endl;

Point p2((x2,y2,c2));

//Point 3
cout<<" Enter the Upper point, X-Axis: " ;
cin>> x3 ;
cout<<endl;
cout<<" Enter the Upper Left point, Y-Axis: " ;
cin>> y3 ;
cout<<endl;
cout<<" Enter the Color of that point: " ;
cin>> c3 ;
cout<<endl;

Point p3((x3,y3,c3));


// t[*count_triangle].SetArea(tri);
//Point center(x,y,c);
//oneCircle = Circle(center, z);

//Point points(x,y,c);
oneTriangle = Triangle(p1,p2,p3);
}

void DisplayTriangle(Triangle *myTriangles, int *countTriangles)
{
for (int i = 0; i < *countTriangles; i++)
{cout<<" Here is triangle # " << i+1 <<endl;
myTriangles.display();
}
}
*/

void CreateCylinder(Cylinder& oneCylinder) {
float x,y,c, z,l;

cout<<" Enter the Center Point of the cylinder, X-Axis: " ;
cin>> x ;
cout<<endl;
cout<<" Enter the Center Point of the cylinder, Y-Axis: " ;
cin>> y ;
cout<<" Enter the Color of that point: " ;
cin>> c ;

cout<<" Enter the radius of the cylinder: " ;
cin>> z;

cout<<" Enter the lenght of the cylinder: " ;
cin>> l;

Point center(x,y,c);
oneCylinder = Cylinder(center, z,l);
}

void DisplayCylinder(Cylinder *myCylinders, int *countCylinders)
{
for (int i = 0; i < *countCylinders; i++)
{cout<<" Here is cylinder # " << i+1 <<endl;
myCylinders.display();
}
}


void menu();
void CreateCircle(Circle& oneCircle);
void DisplayCircle(Circle , int );

//void CreateTriangle(Triangle& oneTriangle);
//void DisplayTriangle(Triangle , int );

void CreateCylinder(Cylinder& oneCylinder);
void DisplayCylinder(Cylinder *myCylinders, int *countCylinders);


int main()
{
menu();
getch();
return 0;
}

void menu()
{ char choice;

Circle myCircles [10];
int countCircles = 0;

// Triangle myTriangles [10];
// int countTriangles = 0;

Cylinder myCylinders [10];
int countCylinders = 0 ;

do {
cout << endl << endl;
cout << " Shape Management System "<<endl;
cout << "============================================== "<<endl;
cout << " 1: Create a Circle "<<endl;
cout << " 2: Create a Triangle "<<endl;
cout << " 3: Create a Cylinder "<<endl;
cout << " 4: Display all Circles "<<endl;
cout << " 5: Display all Triangles "<<endl;
cout << " 6: Display all Cylinders "<<endl;
cout << " 7: Quit "<<endl;
cout << "============================================== "<<endl;
cout << " Your choice please: " ;
cin >> choice;

switch (choice)
{
case '1':
CreateCircle(myCircles[countCircles]);
countCircles++;
break;
case '2':
// CreateTriangle(myTriangles[countTriangles]);
// countTriangles++;
break;
case '3':
CreateCylinder(myCylinders[countCylinders]);
countCylinders++;
break;
case '4':
DisplayCircle(&myCircles[0], &countCircles);
break;
case '5':
// DisplayTriangle(&myTriangles[0], &countTriangles);
break;
case '6':
DisplayCylinder(&myCylinders[0], &countCylinders);
break;
case '7': cout<<"Thank you for having used this system, Bye Bye!!!" ;
break;
default: cout<<"Error: Invalid option, Please try again" <<endl;
}

}while ( (choice != '7') ) ;

}

template<class T>
bool isbetween(T c, T l, T u)
{
if(l > u) swap(l, u);
return c >= l && c <= u;
}

void Validate(float &c) //Validate to see if it is a letter

{
if (isbetween<char>(c, 64, 122))
{
cout << "You have type an integer, Please try again with a positive
number! " ;
cout << "Do NOT type a letter " ;
exit(1);
}
}

int Validate_positive(float &c)
{
if (c < 0)
{ cout<< "You have type a negative integer, Please try again with a
positive number! " <<endl;

exit(1); } //exits the program and starts over with a positive number
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,191
Latest member
BuyKetoBeez

Latest Threads

Top