Number Class with Methods defined Inside it

Learn to implement a class with member methods defined inside it.

Challenge

Write a self-sufficient class called Number that maintains an int member variable. It should have the following methods inside it to perform various operations on the int:

  • void setNumber ( int n ) stores the value of n in the class’ member variable

  • int getNumber( ) returns the current value of Number class member variable

  • void printNumber( ) prints the value of int member variable

  • bool isNegative( ) checks whether the value of int member variable is negative

  • bool isDivisibleBy ( int n ) checks whether the int member variable is divisible by n

  • int absoluteValue( ) returns the absolute value of member variable

Coding exercise

Your job is to define the methods inside the Number class given below.

Get hands-on with 1400+ tech skills courses.