What can i do for add functions by using files .h and .cpp? | Sololearn: Learn to code for FREE!

0

What can i do for add functions by using files .h and .cpp?

code: source file include <iostream> include"calc.h" using namespace std; int main (){ calc (); return 0; } .h header #ifndef calc_h #define calc_h class functions { public: int calc (); }; #endif//calc_h .cpp function contains #include "calc.h" #include <iostream> using namespace std; int main (){ class functions { public: int calc (){ cout <<"hi"<<endl; }; }

7/20/2016 1:02:36 PM

Ismael Perbech

5 Answers

New Answer

+1

First of all, because your function does not return something it gets void instead of int. then the ".h" file is your template for your class. here you define the structure of your class. therefore all things you defined here goes out of your class. cpp in the class.cpp after your using namespace comes your function: void functions::calc() { cout.... } in your source is missing functions MyCalc; MyCalc.calc(); that should work then

0

thanks it s usseful to know what i did wrong

0

np... only additional Info. if you want to pass values into your class you have to do: in your ".h" void calc(int); in your ".cpp" void functions::calc(int x) { cout << 10*x << endl; in your source MyCalc.calc(5);

0

and if i have a lot of variables in my function ??

0

You can send aß much vars to your function as you need. But i have to admit, i never tried to get more than one result back from a function. But i would suggest that it's not possible because of your return Statement whats defined at the beginning of your function