sharing information in a class

D

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

Victor Bazarov

Dan said:
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
 
D

Dan

When you construct a Circle, why not initialise the volume as well?
Ok I will
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;
 
D

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

Victor Bazarov

Dan said:
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
 
D

Dan

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

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
 
D

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

Victor Bazarov

Dan said:
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
 
V

Victor Bazarov

Dan said:
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"?
 
D

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() )
 
D

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..
 
V

Victor Bazarov

Dan said:
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.
 
V

Victor Bazarov

Dan said:
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!
 
D

Dan

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.
 
J

John Harrison

Dan said:
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
 
D

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

Victor Bazarov

Dan said:
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"?
 
T

Thomas Matthews

Dan said:
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
 
D

Dan

void Circle::CreateCircle()
^^^^^^^^^^^^^^^^^^^^^^^^^^
Throw this declaration/definition/initialisation out. You don't need it.


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


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


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

this->SetVolume(itsRadius);


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


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
 
D

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.
 

Ask a Question

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

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top