473,775 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sharing information in a class

Dan
Hello,

I have trouble with class calling. I am calling getvolume() with succes in
the function CreateCircle but it do not want to call it in ShowCircle()
function. I am staying in the same class. I thought that was the point of
encapsulation. When the function ShowCircle() is called I get very large
number -1.07374e+008
can anyone help me ?
class Circle
{
public:

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

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

private:
float itsRadius;
float volume;
};
void Circle::CreateC ircle() //Draw a circle
{ char color;
float itsRadius, volume;

cout<<"Enter the radius in cm: " ;
cin>> itsRadius;

SetVolume(itsRa dius);
cout<<" Volume: " << GetVolume() <<endl;
}
void Circle::ShowCir cle()
{ int i;
cout <<" Volume is : " <<GetVolume() <<" cm cube " <<endl;
}

Jul 22 '05
42 3211
>
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
Jul 22 '05 #21
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::CreateSh ape()
{ 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.CreateCirc le() ;
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::DisplayS hape()
{ 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.CreateShap e(); //SetTime( &s[0], &count, &totalTime[0] );
break;
case '2':
draw.DisplaySha pe();
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::CreateC ircle() //Draw a circle
{
cout<<"Enter the radius in cm: " ;
cin>> itsRadius;

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

}

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

}


Jul 22 '05 #22
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(Circ le* s, int* count);

protected:
float itsRadius;
};

class Cylinder: public Shape
{
/* Cylinder ()
{
itsRadius = 0;
itsHeight = 0;
volume = 0;
}
~Cylinder(){}
*/
public:
void DrawCylinder(Cy linder *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.CreateShap e();
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::CreateSh ape()
{
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_cylinde r <<" " <<count_triangl e;
}

void Shape::DisplayS hape()
{ 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::ShowCir cle(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::DrawCir cle()
{ Shape draw;
float color, total;
Circle::SetVolu me();
total = Circle::GetVolu me() ;
color = draw.ChooseColo r();

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::DrawC ylinder(Cylinde r* a, int* count)
{Shape draw;
float color, total;

Cylinder::SetVo lume();
total = Cylinder::GetVo lume() ;

color = draw.ChooseColo r();

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::DrawT riangle()
{Shape draw;
float color, total;

Triangle::SetVo lume();
total = Triangle::GetVo lume();

color = draw.ChooseColo r();

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::ChooseCo lor() //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::ShowCir cle(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[i].GetVolume()
<<endl;

}
}

Jul 22 '05 #23
"Dan" <le*********@ya h00.c0m> wrote in message
news:Zb******** **********@news 20.bellglobal.c om...
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.

[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::CreateSh ape
case '1':
circ.CreateCirc le() ;
break;
Here you try to show the circle
case '1':
cir.ShowCircle( );
break;


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

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::CreateSh ape 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::CreateSh ape because they don't exist outside of the
Shape::CreateSh ape 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

Jul 22 '05 #24
Dan
Here you create a circle, it's the variable called circ in the method
Shape::CreateSh ape
case '1':
circ.CreateCirc le() ;
break;
Here you try to show the circle
case '1':
cir.ShowCircle( );
break;


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

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::CreateSh ape 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::CreateSh ape because they don't exist outside of the
Shape::CreateSh ape 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
Jul 22 '05 #25

"Dan" <le*********@ya h00.c0m> wrote in message
news:fZ******** **********@news 20.bellglobal.c om...
Here you create a circle, it's the variable called circ in the method
Shape::CreateSh ape
case '1':
circ.CreateCirc le() ;
break;
Here you try to show the circle
case '1':
cir.ShowCircle( );
break;


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

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::CreateSh ape 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::CreateSh ape because they don't exist outside of the
Shape::CreateSh ape 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 ?


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
Jul 22 '05 #26
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(Sh ape *a, int * count, Circle *v);
void DisplayCircle(S hape *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.CreateCircl e( &circle[0], &count_circl e, &area[0]);
break;
case '2':
tri.CreateTrian gle( &triangle[0], &count_triangle , &area_1[0]);
break;
case '3':
cyl.CreateCylin der( &cylinder[0],&count_cylinde r, &volume1[0]);
break;
case '4':
cir.DisplayCirc le( &circle[0],&count_circl e, &area[0]);
break;
case '5':
tri.DisplayTria ngle( &triangle[0],&count_triangl e, &area_1[0]);
break;
case '6':
cyl.DisplayCyli nder( &cylinder[0],&count_cylinde r, &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::CreateC ircle( 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(valu e);
// p2 = s[*count].SetRadius(valu e);

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

//Circle point1(p1, p2);

(*count) ++;
}
void Triangle::Creat eTriangle(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(valu e);

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

(*count) ++;
}
void Cylinder::Creat eCylinder(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(valu e);

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

(*count) ++;

}

void Circle::Display Circle(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<<"CenterPo int: " <<s[i].GetCenterPoint () <<endl <<"Radius: " <<
s[i].GetRadius() <<endl <<"Area: " << area[i].GetArea() <<endl;

cout <<endl;
}
}
}

void Triangle::Displ ayTriangle(Shap e *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<<"CenterPo int: " <<s[i].GetCenterPoint () <<endl <<"Radius: " <<
s[i].GetRadius() <<endl <<"Area: " << area[i].GetArea() <<endl;

cout <<endl;
}
}
}

void Cylinder::Displ ayCylinder(Shap e *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<<"CenterPo int: " <<s[i].GetCenterPoint () <<endl <<"Radius: " <<
s[i].GetRadius() <<endl <<"Volume: " << volume[i].GetVolume() <<endl;

cout <<endl;
}
}
}


Jul 22 '05 #27

"Dan" <le*********@ya h00.c0m> wrote in message
news:5C******** *****@news20.be llglobal.com...
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<<"CenterPo int: " << 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
Jul 22 '05 #28
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


Jul 22 '05 #29
Dan

void Circle::Display () const
{
cout<<"CenterPo int: " << 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

Jul 22 '05 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1436
by: Magic1812 | last post by:
Magic Software invites you to join us this coming Tuesday (January 13th, 2004) at 12:00 EDT / 17:00 GMT for a FREE live Webinar: Title: Accelerated Application Integration and Information Sharing Date: January 13, 2004 Time: 12:00 PM EST / 17:00 GMT Presenter: Lee Sutton
0
1535
by: Magic1812 | last post by:
Magic Software invites you to join us this coming Tuesday (January 13th, 2004) at 12:00 EDT / 17:00 GMT for a FREE live Webinar: Title: Accelerated Application Integration and Information Sharing Date: January 13, 2004 Time: 12:00 PM EST / 17:00 GMT Presenter: Lee Sutton
16
2256
by: Robert W. | last post by:
I'm building a solution that has 1 component for the Desktop and 1 component for the Pocket PC. (Though this isn't a mobile question). I have a data library that will be shared on both platforms. So I've adopted the approach of having two projects share the same set of files. In my case, I have two similarly named projects: DataObjects DataObjectsCF The former is for the Desktop app and the latter is for the Compact Framework.
5
10525
by: BPearson | last post by:
Hello I would like to have several sites share a single web.config file. To accomplish this, I would point the root of these sites to the same folder. Is there any reason why I might not want to do this? (IIS 5 or 6 In case you're wondering why I would do this, we have many web sites on a single server, and there are groups of sites that need to share common configuration information. I'd like to ease some administration by having one...
5
2434
by: Jeff | last post by:
Hi - I'm creating an installation program that will install a VB.NET program and an instance of MSDE on an unknown desktop (commercial application). The install will include the MSDE redistributable files (and it will launch the MSDE Setup.exe to install MSDE). I've read that "Except on Win 98 and Millennium, file and print sharing must be active"
0
1557
by: Emily | last post by:
Imagine a world where everybody shares and has faith in each other. We have all struggled at one time or another with our jobs or careers and have wondered if there was a better way to make a living. What if the solution was just to help each other, simply by giving and receiving. This would be a radical global experiment in faith. Imagine people all around the world connecting instantaneously and just giving and receiving money to each...
8
4851
by: mc | last post by:
I would like to be able to send from an ASP.NET page an email which when recieved takes the form of a "Sharing Invitation for a RSS Feed" (http://office.microsoft.com/en-us/outlook/HA101595391033.aspx) I've found a MSDN article about it (http://msdn2.microsoft.com/en-us/library/bb176432.aspx) but that example is presented as a vb(a) script from within outlook. Can this functionality be emulated from sending an email from C#? TIA
1
241
by: Jesse Aufiero | last post by:
What is the most sure-fire way to prevent users of my web site from sharing their logon information with others? i want to guarantee that only paying customers are allowed into the site. Thanks
45
3019
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I'm trying to find another way to share an instance of an object with other classes. I started by passing the instance to the other class's constructor, like this: Friend Class clsData Private m_objSQLClient As clsSQLClient Private m_objUsers As clsUsers
0
9625
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9459
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10056
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9920
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6721
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5365
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5489
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2857
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.