174k views
4 votes
c++ design a class named myinteger which models integers. interesting properties of the value can be determined. include these members: a int data field named value that represents the integer's value. a constructor that creates a myinteger object with a specified int value. a constant getter that returns the value

User Guari
by
4.9k points

1 Answer

1 vote

Answer:

  1. #include <iostream>
  2. using namespace std;
  3. class myinteger {
  4. private:
  5. int value;
  6. public:
  7. myinteger(int x){
  8. value = x;
  9. }
  10. int getValue(){
  11. return value;
  12. }
  13. };
  14. int main()
  15. {
  16. myinteger obj(4);
  17. cout<< obj.getValue();
  18. return 0;
  19. }

Step-by-step explanation:

Firstly, we use class keyword to create a class named myinteger (Line 5). Define a private scope data field named value in integer type (Line 7 - 8).

Next, we proceed to define a constructor (Line 11 - 13) and a getter method for value (Line 15 -17) in public scope. The constructor will accept one input x and set it to data field, x. The getter method will return the data field x whenever it is called.

We test our class by creating an object from the class (Line 23) by passing a number of 4 as argument. And when we use the object to call the getValue method, 4 will be printed as output.

User David Strencsev
by
5.1k points