### Functions and classes

Hi, I'm trying to get a better grasp on functions and classes. I had a bit of a long break in between level one and two of my C++ programming classes, and I've been struggling with our assignments, (we're currently doing friend functions and they are kicking my rear). So, in order to help me get a grasp on what I'm having trouble with, I started up a simple little program and am now trying to get it to work through the use of a class. I have everything put where I think it should be, and I don't get any errors, but the only thing that prints up is what appears to be an address in memory. This is my own program, (based off an old tutorial from my level one class six years ago) and not homework, so you can feel free to post whatever code you'd like. So can somebody show me what I'm missing and explain what I am doing wrong, thanks.

 ``12345678910111213141516171819`` ``````#ifndef _MYMATH #define _MYMATH class mymath { private: int up; public: mymath(); void calculate(); }; #endif ``````

My header.cpp file (not a great name I know):

 ``1234567891011121314151617181920212223242526`` ``````#include #include using namespace std; #include "mymath.h" void calculate() { double input; cout << "Give me a double (i.e 3.4, or 6.7) " << endl; cin >> input; double c = ceil(input); double f = floor(input); double rPower = (rand() % 20) + 3; cout <<"Rounded up " << c << " To the power of " << rPower << " = " << pow(c, rPower) <

and my main.cpp file:

 ``12345678910111213141516171819202122`` ``````#include #include "mymath.h" #include using namespace std; int main() { void calculate(); cout << calculate << endl; return 0; }``````

Ok, I figgured out what I did wrong, here's my main.cpp now fixed:

 ``1234567891011121314151617181920212223`` ``````#include #include "mymath.h" #include using namespace std; void calculate(); int main() { mymath; calculate(); return 0; }``````

So that was simple, I just forgot that I don't have to use cout to print up my calculate object, it already has what it needs inside the function itself. Still, can anybody walk me though how this worked?
It works because the header file has a definition of the function calculate.

But you are not using classes properly.

First use the class name for filenames - mymath.h & mymath.cpp & you can call the file with main function in it anything with a .cpp extension. Could use main.cpp if you can't think of anything else.

Your header file is fine, the .cpp file is almost right - line 8 should be:

`void mymath::calculate() `

And in main you need to create an object and call the class functions through it:

 ``12345678910111213141516`` ``````#include //only include files if you need them #include "mymath.h" #include //already in the mymath.cpp file using namespace std; //there are no std things being used void calculate(); //it's already in the header file int main() { mymath MyMathObj(); //creates a mymath object calling the default constructor // default constructor is implicit because you haven't provided one MyMathObj.calculate(); //calls the mymath::calculate function return 0; }``````

I just edited this directly, hopefully I have done every thing correctly - I haven't compiled it.

Hope it all helps.

 http://www.cplusplus.com/doc/tutorial/classes/

And be aware of the reference & articles sections at the top left of this page.

To define a member function of a class, you have to indicate it belongs to the class.
In your header.cpp file, line8, replace `void calculate()` with `void mymath::calculate()`.

To use it in your main() function, since it's a class member function you must first declare an object of the class to be able to use it:
 ``12345678`` ``````int main(void) { mymath MyObject; MyObject.calculate(); return 0; }``````

As for your previous problem, it came from that: `cout << calculate << endl;`.
If the function had returned something, you may have wanted to write `cout << calculate() << endl;`, but you forgot the parentheses.
As it turns out, in C/C++ the name of a function is seen as a variable that behaves like a pointer and whose value is the address of the function's code in memory.
When you wrote `cout << calculate << endl;` the compiler thought you wanted to display that address.
Last edited on
Cool, thanks to both of you, that actually makes sense that I should be setting the values of the calculate function to the class, an that I need to create an object to call the calculate function in main. I wanted to do this because we were given a rather complex address book where we already had a class and a bunch of stuff already working in it, and we were just supposed to add stuff to it, but it was confusing the hell out of me.

Now I'm off to go make a simple address book of my own that only takes in a last name and see if I can get it to work with a class, then maybe I'll have a better chance of understanding how overloading operators with a friend function actually works. Thanks again!
Topic archived. No new replies allowed.