Question

Write the implementation (.cpp file) // i cant figure out what it wants where the is a ? the...

Write the interface (.h file) of a class Accumulator containing:
A data member named sum of type integer.
A constructor accepting an integer parameter.
A function named getSum that accepts no parameters and returns an integer.
A function named add that accepts an integer parameter and returns no value.

class Accumulator
{
int sum;
Accumulator(int);
int getSum ();
void add (int);
};



Write the implementation (.cpp file) of the Accumulator class of the previous exercise. The full specification of the class is:
An data member named sum of type integer.
A constructor that accepts an integer parameter. THe constructor initializes the data member sum to the value of the parameter.
A function named getSum that accepts no parameters and returns an integer. getSum returns the value of sum .
A function named add that accepts an integer parameter and returns no value. add increases the value of sum by the value of the parameter.


Accumulator::Accumulator(int ? )
{sum= ?;}

int Accumulator::getSum()
{return sum;}

void Accumulator::add(int ?)
{sum+= ? ;}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear,

Here is the code

#ifndef ACCUMULATOR_H
#define ACCUMULATOR_H
class Accumulator
{
int sum;
public: Accumulator(int);
int getSum ();
void add (int);
};
#endif

#include "Accumulator.h
Accumulator:: Accumulator(int num)
{
sum=num;
}
void Accumulator::add(int num)
{
sum+= num ;
}
int Accumulator::getSum()
{
return sum;
}
//Test program
void main()
{
Accumulator obj(5);
obj.add(3);
cout<< "Sum:"<< obj.getSum() << endl;
//pause system for a while
system("pause");
}//end main



Hope this will help you..

answered by: abu
Add a comment
Answer #2

Program Screenshots:

//Accumulator.h #ifndef ACCUMULATOR H #define ACCUMULATOR. H // declare a class. class Accumulator // declare private class mint main() Declare the object of the class Accumulator obj = NULL; // call to the function add obj.add(3); // Display the out

Sample output:

Sum:3

Code to Copy:

//Accumulator.h

#ifndef ACCUMULATOR_H

#define ACCUMULATOR_H

// declare a class.

class Accumulator

{

       // declare private class members.

private:

       int sum;

       // declare public members.

public:

       Accumulator(int sum);

       int getSum();

       void add(int num);

};

#endif

// main.cpp

#include

#include "Accumulator.h"

using namespace std;

Accumulator::Accumulator(int sum)

{

       // Initialize the variable.

       sum = 0;

}

// Declare the function add.

void Accumulator::add(int num)

{

       // Update the value of sum.

       sum += num;

}

// declare the function.

int Accumulator::getSum()

{

       // return the value of sum.

       return sum;

}

int main()

{

       // Declare the object of the class.

       Accumulator obj = NULL;

       // call to the function add.

       obj.add(3);

       // Display the output

       cout << "Sum:" << obj.getSum() << endl;

}

Add a comment
Answer #3

Accumulator.h

class Accumulator{

              private:

                             int sum;

              public:

                             Accumulator();

                             int getSum();

                             void add(int x);

};

Add a comment
Know the answer?
Add Answer to:
Write the implementation (.cpp file) // i cant figure out what it wants where the is a ? the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write the implementation (.cpp file) of the GasTank class of the previous exercise. The full specification...

    Write the implementation (.cpp file) of the GasTank class of the previous exercise. The full specification of the class is: A data member named amount of type  double . An data member named capacity of type  double . A constructor that accepts a parameter of type  double . The value of the parameter is used to initialize the value of capacity. The constructor also sets amount to zero. A function named addGas that accepts a parameter of type  double . The value of the...

  • This is the code I wrote for myprogramminglab It works for 8 out of 9 inputs they use and I...

    Write a full class definition for a class named GasTank , and containing the following members:A data member named amount of type double.A constructor that no parameters. The constructor initializes the data member amount to 0.A function named addGas that accepts a parameter of type double . The value of the amount instance variable is increased by the value of the parameter.A function named useGas that accepts a parameter of type double . The value of the amount data member...

  • Write a full class definition for a class named Player , and containing the following members: A data member name of t...

    Write a full class definition for a class named Player , and containing the following members: A data member name of type string . A data member score of type int . A member function called setName that accepts a parameter and assigns it to name . The function returns no value. A member function called setScore that accepts a parameter and assigns it to score . The function returns no value. A member function called getName that accepts no...

  • Code Lab Help (C++)

    Write the interface (.h file) of a class Player containing:A data member name of type string .A data member score of type int .A member function called setName that accepts a parameter and assigns it to name . The function returns no value.A member function called setScore that accepts a parameter and assigns it to score . The function returns no value.A member function called getName that accepts no parameters and returns the value of name .A member function called...

  • 1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files)...

    1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files) 1b. Add the following private member variables - a int variable named itemNumber - an int variable named quantity - a double variable named cost - a double variable named totalCost. Total cost is defined as quantity X cost. - updateTotalCost() which takes no arguments and sets the values of the variable totalCost. 1c. Add the following public functions: - Accessor and Mutators for...

  • C++ program please provide code only for class counterType.h, counterTypeImp.cpp, and main.cpp. Separately code it. Define...

    C++ program please provide code only for class counterType.h, counterTypeImp.cpp, and main.cpp. Separately code it. Define a class counterType to implement a counter. Your class must have a private data member counter of type int. Define a constructor that accepts a parameter of type int and initializes the counter data member. Add functions to: Set counter to the integer value specified by the user. Initialize counter to 0. Return the value of counter with a function named getCounter. Increment and...

  • I need help with my homework please. I should write this program in C++ language and...

    I need help with my homework please. I should write this program in C++ language and use Inventory.h file (header file), Inventory.cpp file (implementation file), and main.cpp file (main program). This is the program: Demonstrate the class in a driver program. Input validation, don’t accept negative values for item number, quantity, or cost. 6. Inventory Class Design an Inventory class that can hold information and calculate data for items ma retail store's inventory. The class should have the following private...

  • Write a class called Player that has four data members: a string for the player's name,...

    Write a class called Player that has four data members: a string for the player's name, and an int for each of these stats: points, rebounds and assists. The class should have a default constructor that initializes the name to the empty string ("") and initializes each of the stats to -1. It should also have a constructor that takes four parameters and uses them to initialize the data members. It should have get methods for each data member. It...

  • Please submit a .cpp file or upload zip if you have more than one file before...

    Please submit a .cpp file or upload zip if you have more than one file before the time up. No library function is allowed. ***The code must contain your name and has proper format. Create a class named CupCake. It contains: • Has private instance variables 1. egg 2. butter 3. baking powder 4. sugar 5. flour 6. milk • All members must have public methods: getter() and setter(). • A constructor - a default constructor with no arguments. The...

  • Design a class that contains: we have to make 3 files header file , one .cpp...

    Design a class that contains: we have to make 3 files header file , one .cpp file for function and one more main cpp file 9.3 (The Account class) Design a class named Account that contains: An int data field named id for the account. A double data field named balance for the account. A double data field named annualInterestRate that stores the current interest rate. A no-arg constructor that creates a default account with id 0, balance 0, and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT