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.

MY header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef _MYMATH
#define _MYMATH

class mymath
{
private:


 int up;
 

public:

mymath();
void calculate();


};
#endif 



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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

#include <cmath> 

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) <<endl ;
	cout << endl;
	cout <<"Rounded down " << f << " To the power of " <<rPower << " = " << pow(f, rPower) << endl;


}


and my main.cpp file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include "mymath.h"
#include <cmath> 

using namespace std;

 

int main()
{
	void calculate();
	
	cout << calculate << endl;


 

    return 0;

 

}


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include "mymath.h"
#include <cmath> 

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream> //only include files if you need them
#include "mymath.h"
#include <cmath>  //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.

Have a read of this:

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


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

Your code is still incorrect.

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:
1
2
3
4
5
6
7
8
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.