440,130 Members | 2,141 Online
Need help? Post your question and get tips & solutions from a community of 440,130 IT Pros & Developers. It's quick & easy.

# sharing information in a class

 P: n/a 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() <
42 Replies

 P: n/a > > 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() <

 P: n/a 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 #include 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() <

 P: n/a "Dan" 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() <

 P: n/a "Dan" 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 #include 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() <

 P: n/a > 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

 P: n/a 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 #include 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() <

 P: n/a "Dan" 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 #include 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() <

 P: n/a "Dan" 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

 P: n/a > 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() <

 P: n/a 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

 P: n/a > > 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() <" with "this->". } void Circle::ShowCircle() { cout <<" Volume is : " << GetVolume() <<" cm cube " <

 P: n/a 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

 P: n/a > 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

 P: n/a 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 #include 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 "<> 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 !" <> 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 !" <> 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 <> itsRadius; this->SetVolume(itsRadius); cout<<" Volume: " << this->GetVolume() <

 P: n/a "Dan" 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

 P: n/a 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

 P: n/a "Dan" 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

 P: n/a void Circle::Display() const { cout<<"CenterPoint: " << GetCenterPoint() << endl <<"Radius: " << GetRadius() <

 P: n/a "Dan" wrote in message news:fr**************@news20.bellglobal.com... void Circle::Display() const { cout<<"CenterPoint: " << GetCenterPoint() << endl <<"Radius: " << GetRadius() <

 P: n/a > 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

 P: n/a > 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 " <

 P: n/a 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

 P: n/a "Dan" 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 " <

 P: n/a "Dan" 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

 P: n/a > 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

 P: n/a > 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

 P: n/a "John Harrison" 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