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(); }

27th Sep 2021, 2:31 PM
Boong Bing
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)
27th Sep 2021, 2:41 PM
Ipang
+ 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.
24th Dec 2021, 12:47 AM
Chin Eu
Chin Eu - avatar
+ 1
Chin Eu, I made a typo then, it should be referring to private member <num> instead of <n>. Thank you for correcting me ... : )
24th Dec 2021, 4:04 AM
Ipang
+ 1
Thanks Ipang, I didn’t know actually, just following your hint
24th Dec 2021, 5:53 AM
Chin Eu
Chin Eu - avatar