Ask Question
8 November, 00:18

Write a class named Accumulator containing: An instance variable named sum of type integer. A constructor that accepts an integer parameter, whose value is used to initialize the sum instance variable. A method named getSum that returns the value of sum. A method named add that accepts an integer parameter. The value of sum is increased by the value of the parameter.

+2
Answers (1)
  1. 8 November, 03:25
    0
    The following are the code in the C+ + Programming Language.

    //define header file

    #include

    / / using namespace

    using namespace std;

    //define a class

    class Accumulator

    {

    //set private access modifier

    private:

    //declare integer type variable

    int sum;

    //set public access modifier

    public:

    //define constructor

    Accumulator (int sum)

    {

    //refer the same class as instance variable

    this->sum = sum;

    }

    //define integer type function

    int getSum ()

    {

    //return the value of sum

    return sum;

    }

    //define void type function

    void add (int value)

    {

    //variable sum is increased by the argument value

    sum + = value;

    }

    };

    Explanation:

    The following are the description of the code.

    Firstly, set the required header file and namespace then, define a class 'Accumulator' and inside the class. Set private access modifier then, declare an integer data type variable 'sum'. Declare a class constructor whose name is the same as the class name 'Accumulator () ' and pass integer data type argument 'sum' in its parameter that refers to the same class as instance variable. Define a integer data type function 'getSum () ' that return the value of the variable sum. Finally, define a void type function 'add () ' and pass the integer data type argument 'value' in its parameter in which the variable sum is increased by the argument value.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Write a class named Accumulator containing: An instance variable named sum of type integer. A constructor that accepts an integer ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers