My programming lab 9.2 C++ Write a full class definition for a class named Counter, and containing the following members:_______.
A) A data member counter of type int.
B) A constructor that takes one int argument and assigns its value to counter
C) A function called increment that accepts no parameters and returns no value.
D) Increment adds one to the counter data member.
E) A function called decrement that accepts no parameters and returns no value.
F) Decrement subtracts one from the counter data member.
G) A function called getValue that accepts no parameters.
H) It returns the value of the instance variable counter.

Respuesta :

Answer:

class Counter {    

 public:

   int counter;

   Counter(int counter) {     // Constructor

     counter = counter;

   }

   void increment(){

      counter++;

  }

 

   void decrement(){

      counter--;

   }

    int getValue(){

       return counter;

   }

};

Explanation:

// Class header definition for Counter class

class Counter {    

// Write access modifiers for the properties(data members) and methods

 public:

  //a. Create data member counter of type int

   int counter;

   //b. Create a constructor that takes one int argument and assigns

   // its value to counter.

   Counter(int counter) {     // Constructor  with argument counter

    counter = counter;         // Assign the argument counter to the data

                                              // member counter

   }

   // c. Create a function increment that accepts no parameters

   // and returns no value (i.e void)

   void increment(){

      counter++;   // d. increment adds one to the counter data member

  }

 

   // e. Create a function decrement that accepts no parameters

   // and returns no value (i.e void)

   void decrement(){

      counter--;   // f. decrement subtracts one from the counter data member

   }

   // g. Create a function called getValue that accepts no parameters.

   // The return type is int, since the data member to be returned is of

   // type int.

    int getValue(){

       return counter; // h. it returns the value of the instance var counter

   }

}; // End of class declaration

Hope this helps!