![]() |
|
|
|
#1 |
|
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; } Dan |
|
|
|
|
#2 |
|
Posts: n/a
|
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 Victor Bazarov |
|
|
|
#3 |
|
Posts: n/a
|
> > 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 Dan |
|
|
|
#4 |
|
Posts: n/a
|
> 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) 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: { 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; } Dan |
|
|
|
#5 |
|
Posts: n/a
|
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) > > 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: > { 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 Victor Bazarov |
|
|
|
#6 |
|
Posts: 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() <<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 Dan |
|
|
|
#7 |
|
Posts: 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 <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; } Dan |
|
|
|
#8 |
|
Posts: n/a
|
"Dan" <0m> wrote in message
news:5Dryc.38649$... > > > 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 Victor Bazarov |
|
|
|
#9 |
|
Posts: n/a
|
"Dan" <0m> 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; > } > > Victor Bazarov |
|
|
|
#10 |
|
Posts: 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() ) Dan |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| 70-536, 3 questions | blade | MCTS | 11 | 03-23-2008 03:47 PM |
| Computer Security Information and What You Can Do To Keep Your SystemSafe! | Ann.Anderson.group.com@gmail.com | A+ Certification | 0 | 12-06-2007 01:55 AM |
| Computer Security Information (Free Articles and eBooks) | aditya.jaiswal.com.use@gmail.com | DVD Video | 0 | 10-10-2007 04:53 AM |
| Re: File Sharing Problem | Steven L Umbach | A+ Certification | 2 | 01-21-2006 02:32 AM |
| Re: 7. The truth about our creator. .7 | john smith | DVD Video | 2 | 07-25-2003 03:54 AM |