473,322 Members | 1,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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::CreateCircle() //Draw a circle
{ char color;
float itsRadius, volume;

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

SetVolume(itsRadius);
cout<<" Volume: " << GetVolume() <<endl;
}
void Circle::ShowCircle()
{ int i;
cout <<" Volume is : " <<GetVolume() <<" cm cube " <<endl;
}

Jul 22 '05 #1
42 3135
Dan wrote:
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; }
When you construct a Circle, why not initialise the volume as well?
void CreateCircle();
void ShowCircle();

void SetVolume( float radius){
volume = PI * pow(radius,2); }
In this function you set the volume, but 'itsRadius' never changes
(and stays 0). Is that intentional?

What's "PI"? How is it defined?
float GetVolume(){ return volume; }

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

cout<<"Enter the radius in cm: " ;
cin>> itsRadius;
If you enter the radius, wouldn't it make sense to set the member
variable to have the same value?

Notice, that you're in the member function. You declared two
_LOCAL_ variables here 'itsRadius' and 'volume'. They have the
same names as _member_ variable, and therefore _hide_ the data
members. Somehow I don't think that was your intention.

SetVolume(itsRadius);
cout<<" Volume: " << GetVolume() <<endl;
}
void Circle::ShowCircle()
{ int i;
cout <<" Volume is : " <<GetVolume() <<" cm cube " <<endl;
}


I (and am sure others too) would like to see how you call those
functions. It's not enough to see the class definition and the
implementation, one always has to see how the class is used.

My suspicion is that you use different circles without realising
it.

Victor
Jul 22 '05 #2
Dan
class Circle
{
public:

Circle (int radius){
itsRadius = 0; }
When you construct a Circle, why not initialise the volume as well?

Ok I will
void CreateCircle();
void ShowCircle();

void SetVolume( float radius){
volume = PI * pow(radius,2); }
In this function you set the volume, but 'itsRadius' never changes
(and stays 0). Is that intentional?

There is a function Set volume, that the radius value is return to calculate
the volume


What's "PI"? How is it defined?

It is a constant in the First line of the program before class definition
PI= 3.14;

float GetVolume(){ return volume; }

private:
float itsRadius;
float volume;
};

void Circle::CreateCircle() //Draw a circle
{ char color;
float itsRadius, volume;

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


If you enter the radius, wouldn't it make sense to set the member
variable to have the same value?

Notice, that you're in the member function. You declared two
_LOCAL_ variables here 'itsRadius' and 'volume'. They have the
same names as _member_ variable, and therefore _hide_ the data
members. Somehow I don't think that was your intention.

SetVolume(itsRadius);
cout<<" Volume: " << GetVolume() <<endl;
}
void Circle::ShowCircle()
{ int i;
cout <<" Volume is : " <<GetVolume() <<" cm cube " <<endl;
}


I (and am sure others too) would like to see how you call those
functions. It's not enough to see the class definition and the
implementation, one always has to see how the class is used.

My suspicion is that you use different circles without realising
it.

Victor



Jul 22 '05 #3
Dan
I (and am sure others too) would like to see how you call those
functions. It's not enough to see the class definition and the
implementation, one always has to see how the class is used.


Ok so here is my full program. it compiles but the answer like I says is
wrong:

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

const double PI = 3.14159;

class Point
{ int x,y, color;

public:
Point(int a=0, int b=0, int c=0){
x=a; y=b; color=c; }
};

class Shape
{ protected:
Point p1;

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

Shape(){};
int SetColor();
void CreateShape(); //choosing the shape
void DrawShape();
void DisplayShape();
protected:

};

class Circle : public Shape
{
public:
Circle (){};
Circle(Point p, int r=0) : Shape(p) {itsRadius = r;}
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:

float itsRadius;
float volume;
};

void Shape::CreateShape()
{ Circle circ;
char choice;
// Circle circle_array[10];
//int count_circle =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 circ;

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':
circ.ShowCircle() ; //SetTime( &s[0], &count, &totalTime[0] );
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') ) ;
}

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

void Circle::CreateCircle() //Draw a circle
{ Circle cir(0);
char color;
float itsRadius;

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

cir.SetVolume(itsRadius);

cout<<" Volume: " << cir.GetVolume() <<endl;

color = SetColor();
}

void Circle::ShowCircle()
{
cout <<" Volume is : " << GetVolume() <<" cm cube " <<endl;

}

int Shape::SetColor() //Functions for the base class
{ char 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;
}

return 0;
}


Jul 22 '05 #4
Dan wrote:
I (and am sure others too) would like to see how you call those
functions. It's not enough to see the class definition and the
implementation, one always has to see how the class is used.

Ok so here is my full program. it compiles but the answer like I says is
wrong:


Of course. Your program contains several logical errors that your
compiler cannot detect. I says you should works on it some more.

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

const double PI = 3.14159;

class Point
{ int x,y, color;

public:
Point(int a=0, int b=0, int c=0){
x=a; y=b; color=c; }
Use initialisation instead of assignment wherever possible.
See FAQ for more explanation on that.
};

class Shape
{ protected:
Point p1;

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

Shape(){};
int SetColor();
void CreateShape(); //choosing the shape
void DrawShape();
void DisplayShape();
protected:

};

class Circle : public Shape
{
public:
Circle (){};
Circle(Point p, int r=0) : Shape(p) {itsRadius = r;}
So, you initialise the base, but not the radius. Why? Do the
same for 'itsRadius' member as you do for 'Shape' base class.


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:

float itsRadius;
float volume;
};

void Shape::CreateShape()
{ Circle circ;
So, 'circ' is a local circle.
char choice;
// Circle circle_array[10];
//int count_circle =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() ;
OK, here you "create" a local Circle object. It will be destroyed
(_lost_, that is) as soon as this function exits.
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 circ;
Now, here is another local Circle object. Initialised to what?
Zero for the radius and what for the volume?

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':
circ.ShowCircle() ; //SetTime( &s[0], &count, &totalTime[0] );
And what do you expect to see? The contents of the local (to this
function) Circle object. Two different local Circle objects have
absolutely _nothing_ to do with each other.
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;
Another local Circle object. They are like cockroaches, everywhere,
aren't they?

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') ) ;
Well, at least in this function you create and show it in one function,
so it doesn't change between "create" and "show"...
}

float main()
"float" main? You're not allowed to overload 'main', IIRC.
{
menu();
getch();
return 0;
}

void Circle::CreateCircle() //Draw a circle
{ Circle cir(0);
char color;
float itsRadius;
Well, and here, as I already mentioned to you, the 'itsRadius'
variable has nothing to do with the 'itsRadius' data member except
the same name. Did you intend to use 'itsRadius' member here or
did you intend to use a local variable? It's better to name them
differently, unless _hiding_ (sometimes called "shadowing") is the
effect you desire.

cout<<"Enter the radius in cm: " ;
cin>> itsRadius;
You're entering the value that will be stored only in the local
float variable, not in the data member.

cir.SetVolume(itsRadius);

cout<<" Volume: " << cir.GetVolume() <<endl;

color = SetColor();
}

void Circle::ShowCircle()
{
cout <<" Volume is : " << GetVolume() <<" cm cube " <<endl;

}

int Shape::SetColor() //Functions for the base class
{ char 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
'chosen' is misspelled.
6 !" <<endl;
}

return 0;
}


Victor
Jul 22 '05 #5
Dan
> > cout<<"Enter the radius in cm: " ;
cin>> itsRadius;


You're entering the value that will be stored only in the local
float variable, not in the data member.

cir.SetVolume(itsRadius);

cout<<" Volume: " << cir.GetVolume() <<endl;

color = SetColor();
}

void Circle::ShowCircle()
{
cout <<" Volume is : " << GetVolume() <<" cm cube " <<endl;

}

int Shape::SetColor() //Functions for the base class
{ char input;


Ok , so How do I make this value volume global to the class then ? is it
possible??.
So that other function can use it, Now like you said it does get removed
everytime I exit

Jul 22 '05 #6
Dan
Here is a anotehr wy to ask my question:
How can I get the value of ''pConstRect->GetWidth() '' outside of the
function where pConstRect was declared:

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

class Rectangle
{
public:
Rectangle();
~Rectangle();
void SetLength(int length) { itsLength = length; }
int GetLength() const { return itsLength; }

void SetWidth(int width) { itsWidth = width; }
int GetWidth() const { return itsWidth; }

void ShowWidth();

private:
int itsLength;
int itsWidth;
};
Rectangle::Rectangle():
itsWidth(5),
itsLength(10)
{}
Rectangle::~Rectangle()
{}

int main()
{
Rectangle* pRect = new Rectangle;
const Rectangle * pConstRect = new Rectangle;

cout << "pConstRect width: " << pConstRect->GetWidth() << "
feet\n";

getch();
return 0;
}

void Rectangle::ShowWidth()
{

cout<<"hello: " << pConstRect->GetWidth() <<endl;
}
Jul 22 '05 #7
"Dan" <le*********@yah00.c0m> wrote in message
news:5D******************@news20.bellglobal.com...
cout<<"Enter the radius in cm: " ;
cin>> itsRadius;


You're entering the value that will be stored only in the local
float variable, not in the data member.

cir.SetVolume(itsRadius);

cout<<" Volume: " << cir.GetVolume() <<endl;

color = SetColor();
}

void Circle::ShowCircle()
{
cout <<" Volume is : " << GetVolume() <<" cm cube " <<endl;

}

int Shape::SetColor() //Functions for the base class
{ char input;


Ok , so How do I make this value volume global to the class then ? is it
possible??.
So that other function can use it, Now like you said it does get removed
everytime I exit


Just remove its declaration from that function. The member variable
will be used instead of that local one.

V
Jul 22 '05 #8
"Dan" <le*********@yah00.c0m> wrote...
Here is a anotehr wy to ask my question:
How can I get the value of ''pConstRect->GetWidth() '' outside of the
function where pConstRect was declared:
You either pass the value as an argument or pass the 'pConstRect'
an an argument.

Where do you need it "outside"?

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

class Rectangle
{
public:
Rectangle();
~Rectangle();
void SetLength(int length) { itsLength = length; }
int GetLength() const { return itsLength; }

void SetWidth(int width) { itsWidth = width; }
int GetWidth() const { return itsWidth; }

void ShowWidth();

private:
int itsLength;
int itsWidth;
};
Rectangle::Rectangle():
itsWidth(5),
itsLength(10)
{}
Rectangle::~Rectangle()
{}

int main()
{
Rectangle* pRect = new Rectangle;
const Rectangle * pConstRect = new Rectangle;

cout << "pConstRect width: " << pConstRect->GetWidth() << "
feet\n";

getch();
return 0;
}

void Rectangle::ShowWidth()
{

cout<<"hello: " << pConstRect->GetWidth() <<endl;
}

Jul 22 '05 #9
Dan
> Just remove its declaration from that function. The member variable
will be used instead of that local one.

There are no declaration in ShowCircle()
and If I remove the one in CreateCircle(), then it still gives me the same
eronous results when I choose to display them ( the other function() )
Jul 22 '05 #10
Dan

Where do you need it "outside"?

In ShowWidth() I want to get pConstRect->GetWidth() , but it do not
work, only in the function where pConstRect has been declared. I need it to
get this information in another function., This is the same problem as the
other problem with the radius..


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

class Rectangle
{
public:
Rectangle();
~Rectangle();
void SetLength(int length) { itsLength = length; }
int GetLength() const { return itsLength; }

void SetWidth(int width) { itsWidth = width; }
int GetWidth() const { return itsWidth; }

void ShowWidth();

private:
int itsLength;
int itsWidth;
};
Rectangle::Rectangle():
itsWidth(5),
itsLength(10)
{}
Rectangle::~Rectangle()
{}

int main()
{
Rectangle* pRect = new Rectangle;
const Rectangle * pConstRect = new Rectangle;

cout << "pConstRect width: " << pConstRect->GetWidth() << "
feet\n";

getch();
return 0;
}

void Rectangle::ShowWidth()
{

cout<<"hello: " << pConstRect->GetWidth() <<endl;
}


Jul 22 '05 #11
"Dan" <le*********@yah00.c0m> wrote...

Where do you need it "outside"? In ShowWidth() I want to get pConstRect->GetWidth() , but it do not
work, only in the function where pConstRect has been declared. I need it

to get this information in another function., This is the same problem as the other problem with the radius..
Perhaps you need to give a systematic C++ study a try. You
apparently have no understanding of objects, members, and how
they fit together.

OK, I don't know if this is going to help your efforts or hurt
them in the long run, but here is the solution for you: just
remove the "pConstRect->" from the 'ShowWidth' function.


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

class Rectangle
{
public:
Rectangle();
~Rectangle();
void SetLength(int length) { itsLength = length; }
int GetLength() const { return itsLength; }

void SetWidth(int width) { itsWidth = width; }
int GetWidth() const { return itsWidth; }

void ShowWidth();

private:
int itsLength;
int itsWidth;
};
Rectangle::Rectangle():
itsWidth(5),
itsLength(10)
{}
Rectangle::~Rectangle()
{}

int main()
{
Rectangle* pRect = new Rectangle;
const Rectangle * pConstRect = new Rectangle;

cout << "pConstRect width: " << pConstRect->GetWidth() << "
feet\n";

getch();
return 0;
}

void Rectangle::ShowWidth()
{

cout<<"hello: " << pConstRect->GetWidth() <<endl;
}



Jul 22 '05 #12
"Dan" <le*********@yah00.c0m> wrote...
Just remove its declaration from that function. The member variable
will be used instead of that local one.

There are no declaration in ShowCircle()
and If I remove the one in CreateCircle(), then it still gives me the same
eronous results when I choose to display them ( the other function() )


Dan, you need to sit down either with a good book that would teach
you object-oriented features of C++ step by step, or with a teacher
who will hold your hand while answering all your questions and all
your concerns about that program of yours. I am not a book and the
Usenet is not a good place for hand-holding of pupils. I strongly
recommend you to start over. Without thorough understanding of the
basics you won't be able to move ahead even if we write the program
for you. Start with simpler things. Work your way up. Visit the
alt.comp.lang.learn.c-c++ newsgroup, it's more for newcomers like
you.

Good luck!
Jul 22 '05 #13
Dan

There are no declaration in ShowCircle()
and If I remove the one in CreateCircle(), then it still gives me the same eronous results when I choose to display them ( the other function() )


Dan, you need to sit down either with a good book that would teach
you object-oriented features of C++ step by step, or with a teacher
who will hold your hand while answering all your questions and all
your concerns about that program of yours. I am not a book and the
Usenet is not a good place for hand-holding of pupils. I strongly
recommend you to start over. Without thorough understanding of the
basics you won't be able to move ahead even if we write the program
for you. Start with simpler things. Work your way up. Visit the
alt.comp.lang.learn.c-c++ newsgroup, it's more for newcomers like
you.

Good luck!


Well I know what you mean, But I am starting over reading my stuff. BUt
like many books I have seen so far, They teach about basic functions and
another chapter about class. But they don't cover all the possibilities
mixing all of those types together and the diferent syntax these involves.
One of the book that I bought, and recently fount it on the net at:
http://newdata.box.sk/bx/c/ seems to be good, But like they tell you what
and how to call a function from the main ( or only one function) tell what
a class is and bl;a bla bla, but but the syntax changes when calling a
function through another function from the same class. Maybe O have not
picked up the right book yet for me or I just can't understand straight.
Jul 22 '05 #14

"Dan" <le*********@yah00.c0m> wrote in message
news:M4******************@news20.bellglobal.com...

There are no declaration in ShowCircle()
and If I remove the one in CreateCircle(), then it still gives me the same eronous results when I choose to display them ( the other
function() )
Dan, you need to sit down either with a good book that would teach
you object-oriented features of C++ step by step, or with a teacher
who will hold your hand while answering all your questions and all
your concerns about that program of yours. I am not a book and the
Usenet is not a good place for hand-holding of pupils. I strongly
recommend you to start over. Without thorough understanding of the
basics you won't be able to move ahead even if we write the program
for you. Start with simpler things. Work your way up. Visit the
alt.comp.lang.learn.c-c++ newsgroup, it's more for newcomers like
you.

Good luck!
Well I know what you mean, But I am starting over reading my stuff. BUt
like many books I have seen so far, They teach about basic functions and
another chapter about class. But they don't cover all the possibilities
mixing all of those types together and the diferent syntax these involves.
One of the book that I bought, and recently fount it on the net at:
http://newdata.box.sk/bx/c/ seems to be good, But like they tell you what
and how to call a function from the main ( or only one function) tell

what a class is and bl;a bla bla, but but the syntax changes when calling a
function through another function from the same class. Maybe O have not
picked up the right book yet for me or I just can't understand straight.


Often recommended in this group is Thinking in C++ by Bruce Eckel, free from
www.mindview.net.

Your book looks reasonable (I doubt its legal though) but it is of course
completely impossible to learn C++ in 21 days. Two years is a more likely
figure.

john
Jul 22 '05 #15
Dan
>
Well I know what you mean, But I am starting over reading my stuff. BUt
like many books I have seen so far, They teach about basic functions and
another chapter about class. But they don't cover all the possibilities
mixing all of those types together and the diferent syntax these involves.
One of the book that I bought, and recently fount it on the net at:
http://newdata.box.sk/bx/c/ seems to be good, But like they tell you what
and how to call a function from the main ( or only one function) tell what a class is and bl;a bla bla, but but the syntax changes when calling a
function through another function from the same class. Maybe O have not
picked up the right book yet for me or I just can't understand straight.


And again , I am using the same technique as above but with a different
program , this time I not using main but two other functions and it still
wont give me the right answer.This is what I mean , two funtions, two
programs and two different rules this is why I am confused. This below
compiles but showCircle don't give me the right answer.
void Circle::CreateCircle()
{ Circle * cir = new Circle;

float itsRadius;

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

cir->SetVolume(itsRadius);
cout<<" Volume: " << cir->GetVolume() <<endl;

}
void Circle::ShowCircle()
{
cout <<" Volume is : " << GetVolume() <<" cm cube " <<endl;
}

Jul 22 '05 #16
"Dan" <le*********@yah00.c0m> wrote...

Well I know what you mean, But I am starting over reading my stuff. BUt
like many books I have seen so far, They teach about basic functions and another chapter about class. But they don't cover all the possibilities
mixing all of those types together and the diferent syntax these involves. One of the book that I bought, and recently fount it on the net at:
http://newdata.box.sk/bx/c/ seems to be good, But like they tell you what and how to call a function from the main ( or only one function) tell what
a class is and bl;a bla bla, but but the syntax changes when calling a
function through another function from the same class. Maybe O have not
picked up the right book yet for me or I just can't understand

straight.
And again , I am using the same technique as above but with a different
program , this time I not using main but two other functions and it still
wont give me the right answer.This is what I mean , two funtions, two
programs and two different rules this is why I am confused. This below
compiles but showCircle don't give me the right answer.
void Circle::CreateCircle()
{ Circle * cir = new Circle; ^^^^^^^^^^^^^^^^^^^^^^^^^^
Throw this declaration/definition/initialisation out. You don't need it.
float itsRadius;
Throw this declaration/definition out. It screws things up for you.

cout<<"Enter the radius in cm: " ;
cin>> itsRadius;
Now it will read into the _member_ variable. That's what you need.

cir->SetVolume(itsRadius);
Don't use "cir->". Throw "cir->" away. Use "this->":

this->SetVolume(itsRadius);
cout<<" Volume: " << cir->GetVolume() <<endl;
Again, replace "cir->" with "this->".

}
void Circle::ShowCircle()
{
cout <<" Volume is : " << GetVolume() <<" cm cube " <<endl;
}


Learning C++ like this is detrimental to the cause. You should use
_systematic_ _study_. Do you understand the word "systematic"?
Jul 22 '05 #17
Dan wrote:
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;
};


BTW, circles don't have volumes, they have areas.
Cylinders, spheres and cones have volumes.
Three dimensional objects have volumes, two
dimensional have areas.

The same with rectangles and boxes. Rectangles
have areas, boxes have volume.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 22 '05 #18
Dan
> > void Circle::CreateCircle()
{ Circle * cir = new Circle;

^^^^^^^^^^^^^^^^^^^^^^^^^^
Throw this declaration/definition/initialisation out. You don't need it.
float itsRadius;


Throw this declaration/definition out. It screws things up for you.

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


Now it will read into the _member_ variable. That's what you need.

cir->SetVolume(itsRadius);


Don't use "cir->". Throw "cir->" away. Use "this->":

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


Again, replace "cir->" with "this->".

}
void Circle::ShowCircle()
{
cout <<" Volume is : " << GetVolume() <<" cm cube " <<endl;
}


Learning C++ like this is detrimental to the cause. You should use
_systematic_ _study_. Do you understand the word "systematic"?


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
Jul 22 '05 #19
Dan
BTW, circles don't have volumes, they have areas.
Cylinders, spheres and cones have volumes.
Three dimensional objects have volumes, two
dimensional have areas.

The same with rectangles and boxes. Rectangles
have areas, boxes have volume.


I know that , I just want to get the program, or part of it working, I dont
need to be mathematically correct for now.
Jul 22 '05 #20
>
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::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

}


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(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[i].GetVolume()
<<endl;

}
}

Jul 22 '05 #23
"Dan" <le*********@yah00.c0m> wrote in message
news:Zb******************@news20.bellglobal.com...
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::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

Jul 22 '05 #24
Dan
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.

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*********@yah00.c0m> wrote in message
news:fZ******************@news20.bellglobal.com...
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.

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(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[i].GetCenterPoint() <<endl <<"Radius: " <<
s[i].GetRadius() <<endl <<"Area: " << area[i].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[i].GetCenterPoint() <<endl <<"Radius: " <<
s[i].GetRadius() <<endl <<"Area: " << area[i].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[i].GetCenterPoint() <<endl <<"Radius: " <<
s[i].GetRadius() <<endl <<"Volume: " << volume[i].GetVolume() <<endl;

cout <<endl;
}
}
}


Jul 22 '05 #27

"Dan" <le*********@yah00.c0m> wrote in message
news:5C*************@news20.bellglobal.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<<"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
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<<"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

Jul 22 '05 #30

"Dan" <le*********@yah00.c0m> wrote in message
news:fr**************@news20.bellglobal.com...

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[i].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
Jul 22 '05 #31

"Dan" <le*********@yah00.c0m> wrote in message
news:_b*************@news20.bellglobal.com...
class Circle : public Shape
{
public:
Circle(Point center, float radius) : Shape(center), _radius(radius)
{
}
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.
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
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.
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 #32
> 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[i].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[i].Display();
}

john
Jul 22 '05 #33
Dan
> Circle circle[10];

for (int i = 0; i < 10; i++)
{
cout << "circle #" << i + 1 << " " << circle[i].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[i].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;
Jul 22 '05 #34
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.


Jul 22 '05 #35

"Dan" <le*********@yah00.c0m> wrote in message
news:HF**************@news20.bellglobal.com...
Circle circle[10];

for (int i = 0; i < 10; i++)
{
cout << "circle #" << i + 1 << " " << circle[i].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[i].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
Jul 22 '05 #36

"Dan" <le*********@yah00.c0m> wrote in message
news:0g**************@news20.bellglobal.com...
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
Jul 22 '05 #37
>
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


Jul 22 '05 #38
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 ?
Jul 22 '05 #39
Dan

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2j************@uni-berlin.de...

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


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
Jul 22 '05 #40
"Dan" <le*********@yah00.c0m> wrote:

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[i].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).
Jul 22 '05 #41

"Dan" <le*********@yah00.c0m> wrote in message
news:6s**************@news20.bellglobal.com...

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
Jul 22 '05 #42
Dan

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


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[i].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[i].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[i].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;
}

Jul 22 '05 #43

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

Similar topics

0
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...
0
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...
16
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....
5
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...
5
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...
0
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...
8
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"...
1
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.