Help with C++ Classes

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2006
Posts: 9
Reputation: bg123 is an unknown quantity at this point 
Solved Threads: 0
bg123 bg123 is offline Offline
Newbie Poster

Help with C++ Classes

 
0
  #1
Apr 11th, 2006
I am having trouble understanding classes, I'm new at this and have not had any problems with c++ until now. This is what I have so far. I was supplied the driver and cannot make any changes to it. Any help would be appreciated greatly. Thanks.

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. const pi = 3.1415926;//global constant Pi
  6.  
  7. /*
  8.   A cylindrical can.
  9. */
  10.  
  11.  
  12. class SodaCan
  13. {
  14. private:
  15. int height, radius;
  16. public:
  17. void get_volume (double);
  18. void get_surface_area (double);
  19.  
  20. };
  21.  
  22.  
  23. void SodaCan::get_volume (int height, int radius)//can volume = piR^2H
  24. {
  25. volume=pi* ?????
  26. }
  27.  
  28. void SodaCan::get_surface_area (int height, int radius);//can surface area = 2piRH+2piR^2
  29. {
  30. surface=2*pi ??????
  31. }
  32.  
  33.  
  34. // a driver:
  35. int main()
  36. {
  37. SodaCan can(10, 5); //height is 10, and radius is 5
  38. cout << can.get_volume() << "\n";
  39. cout << can.get_surface_area() << "\n";
  40. return 0;
  41. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 486
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 48
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Help with C++ Classes

 
0
  #2
Apr 11th, 2006
Your SodaCan class needs a constructor to assign something to the private height and radius data members. eg, the line
  1. SodaCan can(10,5);
requires a constructor which looks like
  1. SodaCan::SodaCan(int, int)

Much in the same way that, for an library type, such as std::string you can say
  1. std::string s("Hello");
which will initialise s with the value "Hello" - Of course, the constructor for std::string is well hidden inside the standard library, but there's a constructor somewhere, which may have a signature like
  1. string::string(const char*)

Check out this link for more info
C++ FAQ - Constructors
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Help with C++ Classes

 
0
  #3
Apr 11th, 2006
I like to think of them as getter and setter methods...

Take a look at this example I did...

  1. #include <iostream>
  2. #include <string>
  3.  
  4. class Student
  5. {
  6. public:
  7. void set(int,std::string);
  8. void get_student_number();
  9. void get_student_name();
  10.  
  11. private:
  12. int student_number;
  13. std::string student_name;
  14.  
  15. };
  16. //setter methods
  17. void Student::set(int a, std::string b)
  18. {
  19. student_number = a;
  20. student_name = b;
  21. }
  22. //getter methods
  23. void Student::get_student_number()
  24. {
  25. std::cout << student_number;
  26. }
  27. void Student::get_student_name()
  28. {
  29. std::cout << student_name;
  30. }
  31.  
  32.  
  33. int main()
  34. {
  35. Student kid;
  36. kid.set(1232,"thwee");
  37. kid.get_student_number();
  38. std::cout << "\n";
  39. kid.get_student_name();
  40.  
  41. std::cin.get();
  42. }
http://img476.imageshack.us/img476/5171/cut20ln.png
Piworld
[Tis simple as Pie]
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 9
Reputation: bg123 is an unknown quantity at this point 
Solved Threads: 0
bg123 bg123 is offline Offline
Newbie Poster

Re: Help with C++ Classes

 
0
  #4
Apr 11th, 2006
I think I've gotten a little bit further, but still have yet to grasp classes, this is what I've got now.


  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. const pi = 3.1415926;//global constant Pi
  6.  
  7. /*
  8.   A cylindrical can.
  9. */
  10.  
  11.  
  12. class SodaCan
  13. {
  14. private:
  15. int height, radius;
  16. public:
  17. double surface;
  18. double volume;
  19. void get_volume ();
  20. void get_surface_area ();
  21. SodaCan (int, int);
  22.  
  23. };
  24.  
  25. SodaCan::SodaCan (int a, int b)
  26. {
  27. height = a;
  28. radius = b;
  29. }
  30.  
  31. void SodaCan::get_volume ()//can volume = piR^2H
  32. {
  33. volume=((pi*(radius^2))*height);
  34. }
  35.  
  36. void SodaCan::get_surface_area ()//can surface area = 2piRH+2piR^2
  37. {
  38. surface=((2*pi*radius*height)+(2*pi*(radius^2)));
  39. }
  40.  
  41.  
  42. // the driver:
  43. int main()
  44. {
  45. SodaCan can(10, 5); //height is 10, and radius is 5
  46. cout << can.get_volume() << "\n";
  47. cout << can.get_surface_area() << "\n";
  48. return 0;
  49. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 486
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 48
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Help with C++ Classes

 
0
  #5
Apr 12th, 2006
Originally Posted by bg123
I think I've gotten a little bit further, but still have yet to grasp classes, this is what I've got now.
What exactly are you having trouble understanding? Your class looks fine for the most part
  1. void SodaCan::get_volume ()//can volume = piR^2H
  2. {
  3. volume=((pi*(radius^2))*height);
  4. }
  5.  
  6. void SodaCan::get_surface_area ()//can surface area = 2piRH+2piR^2
  7. {
  8. surface=((2*pi*radius*height)+(2*pi*(radius^2)));
  9. }
  10.  
  11.  
  12. // the driver:
  13. int main()
  14. {
  15. SodaCan can(10, 5); //height is 10, and radius is 5
  16. cout << can.get_volume() << "\n";
  17. cout << can.get_surface_area() << "\n";
  18. return 0;
  19. }
Your problem here is that get_surface_area() and get_volume() functions both have return type void - so passing them to cout won't do much Remember that those 2 functions run a calculation and assign them to a variable, they don't output anything.

If you edit those functions so that they return the result of the calculations instead (a double), then you won't need the two values inside your SodaCan class surface and volume.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 9
Reputation: bg123 is an unknown quantity at this point 
Solved Threads: 0
bg123 bg123 is offline Offline
Newbie Poster

Re: Help with C++ Classes

 
0
  #6
Apr 12th, 2006
I got it to work only that I'm not getting the right values, and I can't figure out why. The program should display 785.398 & 471.239, instead I get 210 & 342. Thanks for all the help.


  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. const pi = 3.1415926;//global constant Pi
  6.  
  7. /*
  8.   A cylindrical can.
  9. */
  10.  
  11.  
  12. class SodaCan
  13. {
  14. private:
  15. int height, radius;
  16. public:
  17. double surface;
  18. double volume;
  19. SodaCan (int, int);
  20. double get_volume();
  21. double get_surface_area();
  22.  
  23. };
  24.  
  25. SodaCan::SodaCan (int a, int b)
  26. {
  27. height = a;
  28. radius = b;
  29. }
  30.  
  31. double SodaCan::get_volume ()//can volume = piR^2H
  32. {
  33. volume=(pi*(radius^2)*height);
  34. return volume;
  35. }
  36.  
  37. double SodaCan::get_surface_area ()//can surface area = 2piRH+2piR^2
  38. {
  39. surface=((2*pi*radius*height)+(2*pi*(radius^2)));
  40. return surface;
  41. }
  42.  
  43.  
  44. // the driver:
  45. int main()
  46. {
  47. SodaCan can(10, 5); //height is 10, and radius is 5
  48. cout << can.get_volume() << "\n";
  49. cout << can.get_surface_area() << "\n";
  50. return 0;
  51. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Help with C++ Classes

 
0
  #7
Apr 12th, 2006
The problem is with your power function.

You're using '^' which is the xor operator, I think.

Try swapping radius^2 with radius*radius

and const pi = 3.1415926;//global constant Pi

with double pi = 3.1415926;//global constant Pi

http://img476.imageshack.us/img476/5171/cut20ln.png
Piworld
[Tis simple as Pie]
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 9
Reputation: bg123 is an unknown quantity at this point 
Solved Threads: 0
bg123 bg123 is offline Offline
Newbie Poster

Re: Help with C++ Classes

 
0
  #8
Apr 12th, 2006
Thanks a lot.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC