0
Help I am stuck on c++ creating classes please help me!
#include <iostream> using namespace std; class Number { private: int num; public: Number(int n) { num = n; } //complete the method int square() { }; }; int main() { int x; cin >> x; const Number myNum(x); cout << myNum.square(); }
4 Answers
+ 2
Because you defined <myNum> as a const object, you need to also specify member function square() as const by adding `const` after the parameter parentheses.
int square() const
{
// function body here ...
}
In that function also, you need to return member data <num> multiplied by itself.
(Edited)
+ 1
Hi Ipang, I follow your instruction to write the code as below:
int square() const{
return (n*n);
}
But it says n was not declared. Please help.
+ 1
Chin Eu,
I made a typo then, it should be referring to private member <num> instead of <n>.
Thank you for correcting me ... : )
+ 1
Thanks Ipang, I didnât know actually, just following your hint