| | |
Help with C++ Classes
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2006
Posts: 9
Reputation:
Solved Threads: 0
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.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; const pi = 3.1415926;//global constant Pi /* A cylindrical can. */ class SodaCan { private: int height, radius; public: void get_volume (double); void get_surface_area (double); }; void SodaCan::get_volume (int height, int radius)//can volume = piR^2H { volume=pi* ????? } void SodaCan::get_surface_area (int height, int radius);//can surface area = 2piRH+2piR^2 { surface=2*pi ?????? } // a driver: int main() { SodaCan can(10, 5); //height is 10, and radius is 5 cout << can.get_volume() << "\n"; cout << can.get_surface_area() << "\n"; return 0; }
Your SodaCan class needs a constructor to assign something to the private height and radius data members. eg, the line requires a constructor which looks like
Much in the same way that, for an library type, such as std::string you can say 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
Check out this link for more info
C++ FAQ - Constructors
C++ Syntax (Toggle Plain Text)
SodaCan can(10,5);
C++ Syntax (Toggle Plain Text)
SodaCan::SodaCan(int, int)
Much in the same way that, for an library type, such as std::string you can say
C++ Syntax (Toggle Plain Text)
std::string s("Hello");
C++ Syntax (Toggle Plain Text)
string::string(const char*)
Check out this link for more info
C++ FAQ - Constructors
I like to think of them as getter and setter methods...
Take a look at this example I did...
http://img476.imageshack.us/img476/5171/cut20ln.png
Piworld ™
[Tis simple as Pie]
Take a look at this example I did...
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> class Student { public: void set(int,std::string); void get_student_number(); void get_student_name(); private: int student_number; std::string student_name; }; //setter methods void Student::set(int a, std::string b) { student_number = a; student_name = b; } //getter methods void Student::get_student_number() { std::cout << student_number; } void Student::get_student_name() { std::cout << student_name; } int main() { Student kid; kid.set(1232,"thwee"); kid.get_student_number(); std::cout << "\n"; kid.get_student_name(); std::cin.get(); }
Piworld ™
[Tis simple as Pie]
•
•
Join Date: Feb 2006
Posts: 9
Reputation:
Solved Threads: 0
I think I've gotten a little bit further, but still have yet to grasp classes, this is what I've got now.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; const pi = 3.1415926;//global constant Pi /* A cylindrical can. */ class SodaCan { private: int height, radius; public: double surface; double volume; void get_volume (); void get_surface_area (); SodaCan (int, int); }; SodaCan::SodaCan (int a, int b) { height = a; radius = b; } void SodaCan::get_volume ()//can volume = piR^2H { volume=((pi*(radius^2))*height); } void SodaCan::get_surface_area ()//can surface area = 2piRH+2piR^2 { surface=((2*pi*radius*height)+(2*pi*(radius^2))); } // the driver: int main() { SodaCan can(10, 5); //height is 10, and radius is 5 cout << can.get_volume() << "\n"; cout << can.get_surface_area() << "\n"; return 0; }
•
•
•
•
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.
•
•
•
•
C++ Syntax (Toggle Plain Text)
void SodaCan::get_volume ()//can volume = piR^2H { volume=((pi*(radius^2))*height); } void SodaCan::get_surface_area ()//can surface area = 2piRH+2piR^2 { surface=((2*pi*radius*height)+(2*pi*(radius^2))); } // the driver: int main() { SodaCan can(10, 5); //height is 10, and radius is 5 cout << can.get_volume() << "\n"; cout << can.get_surface_area() << "\n"; return 0; }
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.
•
•
Join Date: Feb 2006
Posts: 9
Reputation:
Solved Threads: 0
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.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; const pi = 3.1415926;//global constant Pi /* A cylindrical can. */ class SodaCan { private: int height, radius; public: double surface; double volume; SodaCan (int, int); double get_volume(); double get_surface_area(); }; SodaCan::SodaCan (int a, int b) { height = a; radius = b; } double SodaCan::get_volume ()//can volume = piR^2H { volume=(pi*(radius^2)*height); return volume; } double SodaCan::get_surface_area ()//can surface area = 2piRH+2piR^2 { surface=((2*pi*radius*height)+(2*pi*(radius^2))); return surface; } // the driver: int main() { SodaCan can(10, 5); //height is 10, and radius is 5 cout << can.get_volume() << "\n"; cout << can.get_surface_area() << "\n"; return 0; }
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]
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]
![]() |
Similar Threads
- need idea for project using classes and inheritance (C++)
- How do i do chat program using MFC (Microsoft Foundation Classes) and Visual Basic? (Visual Basic 4 / 5 / 6)
- Loading classes from a .jar file (Java)
- Help with Classes (C++)
- Understanding classes (C++)
- Working with objects of different Classes (Java)
- Classes (C++)
- Summer Classes (Geeks' Lounge)
Other Threads in the C++ Forum
- Previous Thread: Word length sorting
- Next Thread: logical error (displaying words in order of length)
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






