sharing information in a class

J

John Harrison

Thanks for the help, Ya I know what systematic means. BUt I think learning
a new language is confusing, or this could only be me. I did the
modification but , I still don't get a value for Getvolume in ShowCircle.
Would passing those values by reference work ? right now I have only one
value , but later I would like to create many circles and store them into an
array. Would your method work ? I tried using pointer and passing by
reference but with no luck, but if you tell me thats the way I will do
more work on it.
thanks

I think what you need to do is post a small *complete* program that
illustrates the behaviour you don't understand.

Its very hard to help when you don't post complete programs, because almost
always with newbies the part that is wrong is in the code that they don't
post.

Post a complete program, with main and everything else.

john
 
D

Dan

I think what you need to do is post a small *complete* program that
illustrates the behaviour you don't understand.

Its very hard to help when you don't post complete programs, because almost
always with newbies the part that is wrong is in the code that they don't
post.

Post a complete program, with main and everything else.


Ok, there you go : Again the ShowCircle do not show me the circle. I know it
might be easy for a lot of you. but it is not obvious for me.



#include <iostream>
#include <cmath>
using namespace std;

const double PI = 3.14159;

class Shape
{
public:
Shape(){};
void CreateShape(); //choosing the shape
void DrawShape();
void DisplayShape();
};

class Circle : public Shape
{
public:
Circle (){};

Circle (int radius){
itsRadius = 0;
volume =0;
}
void CreateCircle();
void ShowCircle();

void SetVolume( float radius){
volume = PI * pow(radius,2); }
float GetVolume(){ return volume; }

private:
int count;
float itsRadius;
float volume;
};

void Shape::CreateShape()
{ Circle circ;
char choice;
Circle s[10];
int count =0;

do {
cout<<" Shape Management System "<<endl;
cout<< " ============================================== "<<endl;
cout <<endl <<endl;
cout<<" 1. Create a Circle "<<endl;
cout<<" 2. Create a Cylinder "<<endl;
cout<<" 3. Create a Triangle "<<endl;
cout<<" 4. Go back to main menu "<<endl;
cout << " Your choice please: " ;
cin >> choice;

switch (choice)
{
case '1':
circ.CreateCircle() ;
break;
case '2':

break;
case '3':

break;
case '4': cout<<"This exit will go back to the previous menu !" <<endl;
break;
default: cout<<"Error: Invalid option, Please try again" <<endl;
break;
}
}while ( (choice != '4') ) ;
}

void Shape::DisplayShape()
{ Circle cir;

char choice;

do {
cout<<" Shape Management System "<<endl;
cout<< " ============================================== "<<endl;
cout <<endl <<endl;
cout<<" 1. Show Circles created "<<endl;
cout<<" 2. Show Cylinders created "<<endl;
cout<<" 3. Show Triangles created "<<endl;
cout<<" 4. Go back to main menu "<<endl;
cout << " Your choice please: " ;
cin >> choice;

switch (choice)
{
case '1':
cir.ShowCircle();
break;
case '2':
break;
case '3':
break;
case '4': cout<<"This exit will go back to the previous menu !" <<endl;
break;
default: cout<<"Error: Invalid option, Please try again" <<endl;
break;
}

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

void menu()
{ char choice;
Circle draw;

float *count =0;

do {
cout<<endl <<" Shape Management System "<<endl;
cout<< " ============================================== "<<endl;
cout<<" 1. Create Object "<<endl;
cout<<" 2. Display Object Created "<<endl;
cout<<" 3. Quit "<<endl;
cout << " Your choice please: " ;
cin >> choice;

switch (choice)
{
case '1':
draw.CreateShape(); //SetTime( &s[0], &count, &totalTime[0] );
break;
case '2':
draw.DisplayShape();
break;
case '3': cout<<"Thank you for having used this system, Bye Bye!!!";
break;
default: cout<<"Error: Invalid option, Please try again" << endl <<endl;
}

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

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

void Circle::CreateCircle() //Draw a circle
{
cout<<"Enter the radius in cm: " ;
cin>> itsRadius;

this->SetVolume(itsRadius);


cout<<" Volume: " << this->GetVolume() <<endl;

}

void Circle::ShowCircle() //This Should give me the volume Instead it gives
me an extremely small number
{
cout <<" Volume is : " << Circle::GetVolume() <<" cm cube " <<endl <<endl;
//This will be later an array of circles

}
 
D

Dan

I think what you need to do is post a small *complete* program that
illustrates the behaviour you don't understand.

Its very hard to help when you don't post complete programs, because almost
always with newbies the part that is wrong is in the code that they don't
post.

Post a complete program, with main and everything else.

here is my more complete version with all the arrays :
Again only Circles are working

#include <iostream>
#include <cmath>
using namespace std;

const double PI = 3.14159;

class Shape
{
public:
void CreateShape();
float ChooseColor();
void DisplayShape();

float GetVolume(){ return volume; }

protected:
float volume;
};

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;
};

class Cylinder: public Shape
{
/* Cylinder ()
{
itsRadius = 0;
itsHeight = 0;
volume = 0;
}
~Cylinder(){}
*/
public:
void DrawCylinder(Cylinder *a, int * count);
void SetVolume(){
cout<<"Enter the radius in cm: " ; cin>>itsRadius;
cout<<"Enter the height in cm: " ; cin>>itsHeight;
volume = PI *itsRadius * itsHeight ;}


protected:
float itsRadius;
float itsHeight;
};

class Triangle: public Shape
{

public:
void DrawTriangle();
void SetVolume(){
cout<<"Enter the radius in cm: " ; cin>>itsRadius;
cout<<"Enter the height in cm: " ; cin>>itsHeight;
volume = ( PI * itsHeight * pow(itsRadius,2) ) /3 ; }


protected:
float itsRadius;;
float itsHeight;
};

void DrawShape(Shape);

void menu()
{ char choice;
Shape draw;

float count =0;

do {
cout<<endl <<" Shape Management System "<<endl;
cout<< " ============================================== "<<endl;
cout<<" 1. Create Object "<<endl;
cout<<" 2. Display Object Created "<<endl;
cout<<" 3. Quit "<<endl;
cout << " Your choice please: " ;
cin >> choice;


switch (choice)
{
case '1':
draw.CreateShape();
break;
case '2':
cout<<" hello1 "<<endl;
break;
case '3': cout<<"Thank you for having used this system, Bye Bye!!!";
break;
default: cout<<"Error: Invalid option, Please try again" << endl <<endl;
}

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

}

void Shape::CreateShape()
{
Circle draw1[10];
Cylinder draw2[10];
Triangle draw3[10];
char choice;



int count_circle =0;
int count_cylinder =0;
int count_triangle =0;

do {
cout<<" Shape Management System "<<endl;
cout<< " ============================================== "<<endl;
cout <<endl <<endl;
cout<<" 1. Draw a Circle (Sphere) "<<endl;
cout<<" 2. Draw a Cylinder "<<endl;
cout<<" 3. Draw a Triangle (Cone) "<<endl;
cout<<" 4. Go back to main menu "<<endl;
cout << " Your choice please: " ;
cin >> choice;

switch (choice)
{
case '1':
draw1[count_circle].DrawCircle() ;
break;
case '2':
draw2[count_cylinder].DrawCylinder(&draw2[0], &count_cylinder) ;
break;
case '3':
draw3[count_triangle].DrawTriangle() ;
break;
case '4': cout<<"This exit will go back to the previous menu !" <<endl;
break;
default: cout<<"Error: Invalid option, Please try again" <<endl;
break;
}
count_circle ++;


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

cout<< count_circle<<" " <<count_cylinder <<" " <<count_triangle;
}



void Shape::DisplayShape()
{ Circle cir;

char choice;

do {
cout<<" Shape Management System "<<endl;
cout<< " ============================================== "<<endl;
cout <<endl <<endl;
cout<<" 1. Show Circles created "<<endl;
cout<<" 2. Show Cylinders created "<<endl;
cout<<" 3. Show Triangles created "<<endl;
cout<<" 4. Go back to main menu "<<endl;
cout << " Your choice please: " ;
cin >> choice;

switch (choice)
{
case '1':
Circle::ShowCircle(Circle* s, int* count);
break;
case '2':

break;
case '3':

break;
case '4': cout<<"This exit will go back to the previous menu !" <<endl;
break;
default: cout<<"Error: Invalid option, Please try again" <<endl;
break;
}

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


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

void DrawShape(Shape shapesize)
{
cout<<"hello" ;
}

void Circle::DrawCircle()
{ Shape draw;
float color, total;
Circle::SetVolume();
total = Circle::GetVolume() ;
color = draw.ChooseColor();

cout<<" "<<color <<" " <<endl;
cout<<" "<<color <<" " <<color <<" " <<endl;
cout<<" "<<color <<" " <<color <<" " <<endl;
cout<<" "<<color <<" " <<color <<" " <<endl;
cout<<" "<<color <<" " <<endl;
cout <<endl;
cout <<" Volume is : " <<total <<" cm cube " <<endl;
cout<< "And the color is: " << color <<endl;

}

void Cylinder::DrawCylinder(Cylinder* a, int* count)
{Shape draw;
float color, total;

Cylinder::SetVolume();
total = Cylinder::GetVolume() ;

color = draw.ChooseColor();

cout<<" "<<color <<color <<color<<color<<color <<color<<" " <<endl;
cout<<" "<<color <<" " <<color <<" " <<endl;
cout<<" "<<color <<" " <<color <<" " <<endl;
cout<<" "<<color <<" " <<color <<" " <<endl;
cout<<" "<<color <<" " <<color <<" " <<endl;
cout<<" "<<color <<color <<color<<color<<color <<color<<" " <<endl;
cout <<endl;

cout <<" Volume is : " <<total <<" cm cube " <<endl;
cout<< "And the color is: " << color <<endl;
count++;
cout<< "Count:" <<count <<endl;
}

void Triangle::DrawTriangle()
{Shape draw;
float color, total;

Triangle::SetVolume();
total = Triangle::GetVolume();

color = draw.ChooseColor();

cout<<" "<<color <<" " <<endl;
cout<<" "<<color <<" " <<color <<" " <<endl;
cout<<" "<<color <<" " <<color <<" " <<endl;
cout<<" "<<color <<" " <<color <<" " <<endl;
cout<<" "<<color <<" " <<color <<" " <<endl;
cout<<" "<<color <<color <<color<<color<<color
<<color<<color<<color<<color<<color<<color<<" " <<endl;
cout <<endl;
cout <<" Volume is : " <<total <<" cm cube " <<endl;
cout<< "And the color is: " << color ;

}


float Shape::ChooseColor() //Functions for the base class
{ float input;
cout<<" Choose a color from the following menu: " <<endl;
cout<<" 1. Blue "<<endl;
cout<<" 2. Green "<<endl;
cout<<" 3. Red "<<endl;
cout<<" 4. Orange "<<endl;
cout<<" 5. Yellow "<<endl;
cout<<" 6. Purple "<<endl;

cin >> input ;
if (input == 1 || input == 2 || input == 3 || input == 4 || input == 5 ||
input == 6) return input ;
else
{ cout<<"You have not choosen a color, Please choose a number between 1 to 6
!" <<endl;
}

}

void Circle::ShowCircle(Circle* s, int* count)
{
int i;
cout<<"count is: " << *count <<endl;
for (i=0; i<=(*count); i++)
{
cout<<"For circle #: " <<i <<" Here is the volume: " << s.GetVolume()
<<endl;

}
}
 
J

John Harrison

Dan said:
Ok, there you go : Again the ShowCircle do not show me the circle. I know it
might be easy for a lot of you. but it is not obvious for me.

[snip]

I'm afraid it is bad news, the design of your program is fundamentally
flawed. I can explain what is wrong (actually Victor has already done that)
but I can't quickly explain how to fix it because there is no easy fix. You
are simply going about things in completely the wrong way. I would say that
you are attempting a program that is too complex for your current abilities.

Anyway, here is what is wrong.

Here you create a circle, it's the variable called circ in the method
Shape::CreateShape
case '1':
circ.CreateCircle() ;
break;

Here you try to show the circle
case '1':
cir.ShowCircle();
break;

it's the variable called cir, in the method Shape::DisplayShape.

But these are two completely different circles, you create one and then you
try to show a completely different one.

But this is far from the only thing wrong with your code, really you might
as well throw it away. I don't think you understand inheritance and
polymorphism, even more fundamentally I don't think you understand object
lifetimes. One basic problem is that the object you create in
Shape::CreateShape is destroyed at the end of that function. It is
completely impossible with your current code to do anything with the shapes
you create in Shape::CreateShape because they don't exist outside of the
Shape::CreateShape method. This isn't something you can fix with a little
tweak of the syntax, it a fundamental problem in the structure of your code.

I think Victor was right, you need to do some studying and probably you need
to work on less ambitious projects for a while.

john
 
D

Dan

Here you create a circle, it's the variable called circ in the method
Shape::CreateShape


Here you try to show the circle


it's the variable called cir, in the method Shape::DisplayShape.

But these are two completely different circles, you create one and then you
try to show a completely different one.

But this is far from the only thing wrong with your code, really you might
as well throw it away. I don't think you understand inheritance and
polymorphism, even more fundamentally I don't think you understand object
lifetimes. One basic problem is that the object you create in
Shape::CreateShape is destroyed at the end of that function. It is
completely impossible with your current code to do anything with the shapes
you create in Shape::CreateShape because they don't exist outside of the
Shape::CreateShape method. This isn't something you can fix with a little
tweak of the syntax, it a fundamental problem in the structure of your code.

I think Victor was right, you need to do some studying and probably you need
to work on less ambitious projects for a while.


Ok John, so one question. (or anyone) how do you transfer information from
one f'unction to another within the same class ? The only way I know is
through passing by reference ( and passing by value) or I belive a pointer
might do) right ?


D
 
J

John Harrison

Dan said:
Ok John, so one question. (or anyone) how do you transfer information from
one f'unction to another within the same class ? The only way I know is
through passing by reference ( and passing by value) or I belive a pointer
might do) right ?

There are several ways.

1) global variables

2) return values

3) passing a reference or pointer as a parameter

4) member variables

1, 2 and 3 apply to all situations, 4 is only for with the same *object* not
within the same *class* (newbies often get objects and classes confused).

Don't necessarily think of sharing information, think of transferring
information. A return value is the usual way to transfer some information
from one function to answer. However arrays are an exceptions, you cannot
return arrays as values in C++.

Maybe the way to go for your program (I'm thinking of the longer array
version you have) is to throw away the Shape class and have one ShapeHolder
object which holds the arrays

class ShapeHolder
{
public:
CreateShape();
private:
Circle draw1[10];
Cylinder draw2[10];
Triangle draw3[10];
int count_circle;
int count_cylinder;
int count_triangle;
};

void menu()
{
ShapeHolder holder; // the one and only shape holder object
...
}

Just a thought, you don't have to go that way. But the main point is that
you cannot put those arrays inside the CreateShape method, because then they
cannot exist outside of it.

john
 
D

Dan

Maybe the way to go for your program (I'm thinking of the longer array
version you have) is to throw away the Shape class and have one ShapeHolder
object which holds the arrays

class ShapeHolder
{
public:
CreateShape();
private:
Circle draw1[10];
Cylinder draw2[10];
Triangle draw3[10];
int count_circle;
int count_cylinder;
int count_triangle;
};

void menu()
{
ShapeHolder holder; // the one and only shape holder object
...
}

Just a thought, you don't have to go that way. But the main point is that
you cannot put those arrays inside the CreateShape method, because then they
cannot exist outside of it.

I did what you said, I kept everything in one area, which is actually easier
to understand and put everything in one menu.
I have one more problem and hopefully the last one.
I created the array of everything I want. I initialized the constructor. BUt
I cannot seem to bind those two together . This is my big problem now.
Class Point initialize the center point .
alors the getvolume and getarea does not work, but I should be able to fix
that


#include <iostream>
#include <conio.h>
#include <cmath>

using namespace std;
const double PI = 3.14159;

class Point { // initialize the Center point only
int x,y, color;
public:
Point(int a=0, int b=0, int c=0){
x=a; y=b; color=c;
}

};

class Shape //base class for any shape
{
public:
Shape(Point p) :p1(p) {}

Shape ()
{
_centerpoint = 0;
_radius = 0;


}

Shape (int c, int r)
{
_centerpoint = c;
_radius = r;

}

~Shape(){}

int GetCenterPoint()
{
return _centerpoint;
}
void SetCenterPoint(int centerpoint)
{
_centerpoint = centerpoint;
}


int GetRadius()
{
return _radius;
}

void SetRadius(int Radius)
{
_radius = Radius;
}


protected:
Point p1;
int _centerpoint, _radius;

};

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

void CreateCircle(Shape *a, int * count, Circle *v);
void DisplayCircle(Shape *a, int * count, Circle *v);
void SetArea( float radius){
area = PI * pow(radius,2); }
float GetArea(){ return area; }

private:
float area;
float radius;

};

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

void CreateTriangle(Shape *a, int * count, Triangle *v);
void DisplayTriangle(Shape *a, int * count, Triangle *v);
void SetArea( float radius){
area = PI * pow(radius,2); }
float GetArea(){ return area; }

private:
float area;
float radius;

};

class Cylinder: public Shape
{
public:
Cylinder(){};
Cylinder(Point p, float r=0, float h=0) : Shape(p) {radius = r, height =
h;}
void CreateCylinder(Shape *a, int * count, Cylinder *v);
void DisplayCylinder(Shape *a, int * count, Cylinder *v);
void SetVolume( float radius){
volume = height * PI * pow(radius,2); }
float GetVolume(){ return volume; }

private:
float volume;
float height;
float radius;

};

void menu()
{ char abc;
Shape circle[10];
Shape triangle[10];
Shape cylinder[10];

Circle area[10];
Triangle area_1[10];
Cylinder volume1[10];

Circle cir;
Triangle tri;
Cylinder cyl;

int count_circle =0;
int count_triangle =0;
int count_cylinder =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 >> abc;

switch (abc)
{
case '1':
cir.CreateCircle( &circle[0], &count_circle, &area[0]);
break;
case '2':
tri.CreateTriangle( &triangle[0], &count_triangle, &area_1[0]);
break;
case '3':
cyl.CreateCylinder( &cylinder[0],&count_cylinder, &volume1[0]);
break;
case '4':
cir.DisplayCircle( &circle[0],&count_circle, &area[0]);
break;
case '5':
tri.DisplayTriangle( &triangle[0],&count_triangle, &area_1[0]);
break;
case '6':
cyl.DisplayCylinder( &cylinder[0],&count_cylinder, &volume1[0]);
break;
case '7': cout<<"Thank you for having used this system, Bye Bye!!!";
break;
default: cout<<"Error: Invalid option, Please try again" ;
}

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

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

void Circle::CreateCircle( Shape* s, int * count, Circle* volume)
{
int value;
// float p1, p2;
// char color;

cout<<" Enter the Center Point of the circle: " <<endl;
cin>> value;
s[*count].SetCenterPoint(value);
// p1 = s[*count].SetCenterPoint(value);

// cout<<" Enter the color for this point: " <<endl;
// cin>> value;
// s[*count].SetColor(color);

cout<<" Enter the radius of the circle: " <<endl;
cin>> value;
s[*count].SetRadius(value);
// p2 = s[*count].SetRadius(value);

// cout<<" Enter the color for this point: " <<endl;
// cin>> value;
// s[*count].SetColor(color);

//Circle point1(p1, p2);

(*count) ++;


}


void Triangle::CreateTriangle(Shape *s, int *count, Triangle* volume)
{
int value;

cout<<" Enter the CenterPoint: " <<endl;
cin>> value;
s[*count].SetCenterPoint(value);

cout<<" Enter Radius: " <<endl;
cin>> value;
s[*count].SetRadius(value);

cout<<" Enter seconds: " <<endl;
cin>> value;
volume[*count].SetArea(value);

(*count) ++;
}


void Cylinder::CreateCylinder(Shape *s, int *count, Cylinder* volume)
{
int value;

cout<<" Enter the CenterPoint: " <<endl;
cin>> value;
s[*count].SetCenterPoint(value);

cout<<" Enter Radius: " <<endl;
cin>> value;
s[*count].SetRadius(value);

cout<<" Enter seconds: " <<endl;
cin>> value;
volume[*count].SetVolume(value);

(*count) ++;

}

void Circle::DisplayCircle(Shape *s, int * count, Circle* area)
{
if ( (*count) <= 0)
{ cout<< "There are no Circles to display! Please choose another option "
<<endl; }
else
{
for (int i=0; i< (*count); i++)
{
cout<<" Here is the area for circle #" << i+1 <<endl;
cout<<"CenterPoint: " <<s.GetCenterPoint() <<endl <<"Radius: " <<
s.GetRadius() <<endl <<"Area: " << area.GetArea() <<endl;

cout <<endl;
}
}
}

void Triangle::DisplayTriangle(Shape *s, int * count, Triangle* area)
{
if ( (*count) <= 0)
{ cout<< "There are no Triangles to display! Please choose another option
" <<endl; }
else
{

for (int i=0; i< (*count); i++)
{
cout<<" Here is the area for triangle #" << i+1 <<endl;
cout<<"CenterPoint: " <<s.GetCenterPoint() <<endl <<"Radius: " <<
s.GetRadius() <<endl <<"Area: " << area.GetArea() <<endl;

cout <<endl;
}
}
}

void Cylinder::DisplayCylinder(Shape *s, int * count, Cylinder* volume)
{
if ( (*count) <= 0)
{ cout<< "There are no Cylinders to display! Please choose another option
" <<endl; }
else
{
for (int i=0; i< (*count); i++)
{
cout<<" Here is the area for cylinder #" << i+1 <<endl;
cout<<"CenterPoint: " <<s.GetCenterPoint() <<endl <<"Radius: " <<
s.GetRadius() <<endl <<"Volume: " << volume.GetVolume() <<endl;

cout <<endl;
}
}
}
 
J

John Harrison

Dan said:
Maybe the way to go for your program (I'm thinking of the longer array
version you have) is to throw away the Shape class and have one ShapeHolder
object which holds the arrays

class ShapeHolder
{
public:
CreateShape();
private:
Circle draw1[10];
Cylinder draw2[10];
Triangle draw3[10];
int count_circle;
int count_cylinder;
int count_triangle;
};

void menu()
{
ShapeHolder holder; // the one and only shape holder object
...
}

Just a thought, you don't have to go that way. But the main point is that
you cannot put those arrays inside the CreateShape method, because then they
cannot exist outside of it.

I did what you said, I kept everything in one area, which is actually easier
to understand and put everything in one menu.
I have one more problem and hopefully the last one.
I created the array of everything I want. I initialized the constructor. BUt
I cannot seem to bind those two together . This is my big problem now.
Class Point initialize the center point .
alors the getvolume and getarea does not work, but I should be able to fix
that

Dan your design is still completely wrong. You're all mixed up about what
information should go where, what parameters to use what to but in classes
and what not to, etc. etc. The reason you cannot bind together the
information you want is because you are putting it in the wrong place in the
first place.

For instance this is how you Circle class should look

class Circle : public Shape
{
public:
Circle(Point center, float radius) : Shape(center), _radius(radius)
{
}
void Display() const;
float GetArea() const;
float GetRadius() const { return radius; }
private:
float radius;
};

void Circle::Display() const
{
cout<<"CenterPoint: " << GetCenterPoint() << endl <<"Radius: " <<
GetRadius() <<endl <<"Area: " << GetArea() <<endl;
}

float Circle::GetArea() const
{
return PI * pow(radius,2);
}

No SetArea method, the area is calculated from the radius, you don't need a
SetArea method.

The Display method doesn't take all those incorrect parameters, the Display
method displays a circle using the class member variables and methods, its
does not pick a Circle from an array and display that.

You could write a function to pick a circle from and array if you wanted to,
but that function should not be a member of the Circle class.

Also what's this about
Shape circle[10];
Circle area[10];

Why have you got two different arrays for the same objects? All you need is
this

Circle circle[10];

I suspect this is the problem you are complaining about when you say you
can't 'bind together'. If you weren't using two separate arrays in the first
place there wouldn't be a problem.

Really you should start again, I know it difficult to do this, but you are
really flogging a dead horse with your current code. Make the Circle class
look like the one I have above, same for the other classes. Then declare
your arrays like this

Circle circle[10];
Cylinder cylinder[10];
Triangle triangle[10];
int count_circle;
int count_cylinder;
int count_triangle;

Don't be tempted to add any more arrays, those are all you need.

And think carefully before writing any methods in any classes whether that
method belongs in that class. If you have to pass all the information into
the method as parameters and if the method does not use any of the classes
member variables then that is a pretty good indication that the method
should not be in the class at all, and should be a function outside of any
class instead. In your code all the Create... and Display... code should not
be class methods, they should be functions outside of any class.

Just because a function has got something to do with circles does not mean
it should go in the Circle class.

john
 
D

Dan

class Circle : public Shape
{
public:
Circle(Point center, float radius) : Shape(center), _radius(radius)
{
}

I don't get this _radius(radius

float Circle::GetArea() const
{
return PI * pow(radius,2);

Ya thats smart, one function instead of one , neat.

The Display method doesn't take all those incorrect parameters, the Display
method displays a circle using the class member variables and methods, its
does not pick a Circle from an array and display that.

You could write a function to pick a circle from and array if you wanted to,
but that function should not be a member of the Circle class.

Did you tell me the other day that whenever I leave a function I also loose
my information unless it is in an array ?? so This is why I was doing that

Really you should start again, I know it difficult to do this, but you are
really flogging a dead horse with your current code. Make the Circle class
look like the one I have above, same for the other classes. Then declare
your arrays like this

Circle circle[10];
Cylinder cylinder[10];
Triangle triangle[10];
int count_circle;
int count_cylinder;
int count_triangle;

Ok creating an array there is fine ( along my menu)
But I need to pass the array to my function no ? The user is going have to
put in his information, and that information will be put in Circle array. If
I just call a void function in my menu how can I insert that information
into my array. ? This is my problem in c++ is how to point object put
and take values and being to play as much as.. well as need anyway

I tried adding the const atthe end of the function but I get this error:
DisplayCircle' : overloaded member function 'void (void)' not found in
'Circle'

And if you are not passing anything with you void function, how can you keep
track of you count if it was created in the menu function.?


BTW , where do you live ?
and thanks a million for you help John
 
D

Dan

void Circle::Display() const
{
cout<<"CenterPoint: " << GetCenterPoint() << endl <<"Radius: " <<
GetRadius() <<endl <<"Area: " << GetArea() <<endl;
}

You showed me this function. I would say that is good for one value, cause
Get Radius will get one value from that function in the class. But I need
to save those values into an array. Up to ten times because my array is only
that long.

K
 
J

John Harrison

Dan said:
void Circle::Display() const
{
cout<<"CenterPoint: " << GetCenterPoint() << endl <<"Radius: " <<
GetRadius() <<endl <<"Area: " << GetArea() <<endl;
}

You showed me this function. I would say that is good for one value, cause
Get Radius will get one value from that function in the class. But I need
to save those values into an array. Up to ten times because my array is only
that long.

Right. There is nothing in the above function to stop you calling it ten
times for ten different objects

Circle circle[10];

for (int i = 0; i < 10; i++)
{
cout << "circle #" << i + 1 << " " << circle.Display() << endl;
}

This is how you write class methods, the class method works for *one* object
(usually), you then call it as many times as you need for as many different
objects as you have. This is more flexible and simpler.

john
 
J

John Harrison

Dan said:
I don't get this _radius(radius

Its more or less the same as this

Circle(Point center, float radius) : Shape(center)
{
_radius = radius;
}

But there are situations when the first form is better, look up initialiser
lists in your favourite C++ book. But its a minor point, no big deal.
Ya thats smart, one function instead of one , neat.



Did you tell me the other day that whenever I leave a function I also loose
my information unless it is in an array ?? so This is why I was doing that

You lose information from variables (and arrays) which are local to that
function (unless you return them from that function). But class member
variables are not local to any one function, they are only destroyed when
the object itself is destroyed.

I think this is the most important thing you aren't getting. The lifetime
and scope of objects and variables. I guess the only answer is more practice
and study. I'd still advise you work with a simpler program. For instance
its not a good idea to be trying to learn inheritance as well, while you are
still struggling with the basics of objects and classes.

Really you should start again, I know it difficult to do this, but you are
really flogging a dead horse with your current code. Make the Circle class
look like the one I have above, same for the other classes. Then declare
your arrays like this

Circle circle[10];
Cylinder cylinder[10];
Triangle triangle[10];
int count_circle;
int count_cylinder;
int count_triangle;

Ok creating an array there is fine ( along my menu)
But I need to pass the array to my function no ?

Maybe see below
The user is going have to
put in his information, and that information will be put in Circle array. If
I just call a void function in my menu how can I insert that information
into my array. ? This is my problem in c++ is how to point object put
and take values and being to play as much as.. well as need anyway

I think you were doing that more or less OK, there were two problems, first
you had two different arrays, secondly your CreateCircle function was
wrongly part of the Circle class.

Something like this should do the trick, this is using a return value.

void menu()
{
Circle circle[10];
Cylinder cylinder[10];
Triangle triangle[10];
int count_circle = 0;
int count_cylinder = 0;
int count_triangle = 0;

switch (...)
{
case ...:
circle[count_circle] = CreateCircle();
count_circle++;
break;
...
}
}

Circle CreateCircle()
{
cout << "enter circle radius ";
float radius;
cin >> radius;
cout << "enter circle center point";
Point center;
cin >> center;
return Circle(center, radius);
}

but you could do it with parameters like you were doing, the important thing
is that CreateCircle is not, and doesn't need to be, part of the Circle
class.
I tried adding the const atthe end of the function but I get this error:
DisplayCircle' : overloaded member function 'void (void)' not found in
'Circle'

Probably you didn't add it in both places.

class Circle
{
void DisplayCircle() const; // const here
};

void Circle::Display() const // and here
{
...
}

but it not a big deal for you at the moment (its an important topic for
later) so leave it out if it is causing problems.
 
J

John Harrison

Right. There is nothing in the above function to stop you calling it ten
times for ten different objects

Circle circle[10];

for (int i = 0; i < 10; i++)
{
cout << "circle #" << i + 1 << " " << circle.Display() << endl;
}

This is how you write class methods, the class method works for *one* object
(usually), you then call it as many times as you need for as many different
objects as you have. This is more flexible and simpler.


Sorry the above code is garbage, something like this is what I meant

Circle circle[10];

for (int i = 0; i < 10; i++)
{
cout << "circle #" << i + 1 << endl;
circle.Display();
}

john
 
D

Dan

Circle circle[10];
for (int i = 0; i < 10; i++)
{
cout << "circle #" << i + 1 << " " << circle.Display() << endl;
}

This is how you write class methods, the class method works for *one* object
(usually), you then call it as many times as you need for as many different
objects as you have. This is more flexible and simpler.


Ok I tried this , but it still not showing me the right radius value,
I am passing it by reference, and my count by value.


class Circle: public Shape
{
public:
Circle(){};
Circle(Point p, float r) : Shape(p) {radius = r;
cout<<"rad:" << radius; }

void CreateCircle() ;
void DisplayCircle(Circle* circle ,int);

float GetArea(){ return PI * ( pow(radius,2) ); }

private:
float radius;

};

void Circle::DisplayCircle(Circle* circle, int count_circle)
{
if ( (count_circle) <= 0)
{ cout<< "There are no Circles to display! Please choose another option "
<<endl; }
else
{
for (int i=0; i< (count_circle); i++)
{
cout<<" Here is the area for circle #" << i+1 <<endl;
cout <<"Area: " << circle.GetArea() <<endl;
cout <<endl;
}
}
}

and in my switch :
( and this is where I initialize the Circle array and counter.

case '1':
circle[count_circle].CreateCircle();
count_circle++;
break;
 
D

Dan

void menu()
{
Circle circle[10];
Cylinder cylinder[10];
Triangle triangle[10];
int count_circle = 0;
int count_cylinder = 0;
int count_triangle = 0;

switch (...)
{
case ...:
circle[count_circle] = CreateCircle();
count_circle++;
break;

This is giving me an error,: undeclared identifier
so I used this:
circle[count_circle]=circle.CreateCircle();
but then error: '.CreateCircle' must have class/struct/union type

But It is, circle is the object , and that object is calling the function.
This is what I am seeing in my documentation.


The problem is , that books gives you a very basic example of classes,
functions, but very rarely a mixed of all that. This is why I am having
problem manipulating the information, but not so much with the basic coding
and syntax.
 
J

John Harrison

Dan said:
Circle circle[10];

for (int i = 0; i < 10; i++)
{
cout << "circle #" << i + 1 << " " << circle.Display() << endl;
}

This is how you write class methods, the class method works for *one* object
(usually), you then call it as many times as you need for as many different
objects as you have. This is more flexible and simpler.


Ok I tried this , but it still not showing me the right radius value,
I am passing it by reference, and my count by value.


class Circle: public Shape
{
public:
Circle(){};
Circle(Point p, float r) : Shape(p) {radius = r;
cout<<"rad:" << radius; }

void CreateCircle() ;
void DisplayCircle(Circle* circle ,int);

float GetArea(){ return PI * ( pow(radius,2) ); }

private:
float radius;

};

void Circle::DisplayCircle(Circle* circle, int count_circle)
{
if ( (count_circle) <= 0)
{ cout<< "There are no Circles to display! Please choose another option "
<<endl; }
else
{
for (int i=0; i< (count_circle); i++)
{
cout<<" Here is the area for circle #" << i+1 <<endl;
cout <<"Area: " << circle.GetArea() <<endl;
cout <<endl;
}
}
}

and in my switch :
( and this is where I initialize the Circle array and counter.

case '1':
circle[count_circle].CreateCircle();
count_circle++;
break;


You're making the same mistake again!!! You've got everything I said
backwards, Sorry but I give up.

john
 
J

John Harrison

Dan said:
void menu()
{
Circle circle[10];
Cylinder cylinder[10];
Triangle triangle[10];
int count_circle = 0;
int count_cylinder = 0;
int count_triangle = 0;

switch (...)
{
case ...:
circle[count_circle] = CreateCircle();
count_circle++;
break;

This is giving me an error,: undeclared identifier
so I used this:
circle[count_circle]=circle.CreateCircle();
but then error: '.CreateCircle' must have class/struct/union type

Jesus, look at the code I wrote

circle[count_circle] = CreateCircle();

look at the code you wrote

circle[count_circle]=circle.CreateCircle();

See the difference? In my version CreateCircle is *not* a member of the
Circle class, I really don't know why you think it should be. I said it
should not in black and white.
The problem is , that books gives you a very basic example of classes,
functions, but very rarely a mixed of all that. This is why I am having
problem manipulating the information, but not so much with the basic coding
and syntax.

Yes I can see that. Work with simpler examples until you get it

john
 
J

John Harrison

You're making the same mistake again!!! You've got everything I said
backwards, Sorry but I give up.

Sorry about that but I think newsgroups are not a good place to learn the
kind of things you need to learn. As you realise you're struggling with
concepts not with syntax (your syntax is pretty good). What you really need
is a teacher who can explain this kind of stuff to you. I don't think you
are going to get it from a newsgroup, it just not the right medium.

John
 
D

Dan

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 ?
 
D

Dan

John Harrison said:
Sorry about that but I think newsgroups are not a good place to learn the
kind of things you need to learn. As you realise you're struggling with
concepts not with syntax (your syntax is pretty good). What you really need
is a teacher who can explain this kind of stuff to you. I don't think you
are going to get it from a newsgroup, it just not the right medium.

John

I know I am trying to learn this, and I know I am not the fastest person on
earth to learn this crap. BUt this is where I am at know. I know how
function arryas and classes work individually, but when comes time to mixe
them all up together I'm mixed up, I have been working on this since 8:00
this morning and I have not advanced very much. Thanks for you help and all
those posts. I will be reading all those posts again now see if I missed
anything.
D
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,196
Latest member
ScottChare

Latest Threads

Top