c++ - Why don't I get any output? -
i trying write first oop code in c++, reason not getting output. trying create class contain method getsquare() accept int n , returns number squared. can tell me doing wrong?
#include <iostream> using namespace std; class myclass { public: int square; void getsqure(int n); }; void myclass::getsqure(int n) { int square = n * n; } int main(){ int n = 5; myclass c; c.getsqure(5); cout << endl; return 0; }
your getsquare
function doesn't anything, defines variable square
(does not return though). make return int
, like
int myclass::getsqure(int n) { // make sure change declaration int square = n * n; return square; }
then
cout << c.getsquare(5) << endl;
and you'll have output.
Comments
Post a Comment