PART 1 below description Task 1: Implement tostring() Member Method If you haven't already, create the Task 1 issue on G

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
correctanswer
Posts: 43759
Joined: Sat Aug 07, 2021 7:38 am

PART 1 below description Task 1: Implement tostring() Member Method If you haven't already, create the Task 1 issue on G

Post by correctanswer »

PART 1
below description Task 1: Implement tostring() Member
Method
If you haven't already, create the Task 1 issue on GitHub and
associate the task 1 issue with the Feedback pull request
in your class repository.
For the first task you will provide an implementation of
the tostring() method. We need this method to perform
some of our unit testing on the LargeInteger class.
This function will return a string representation of
the LargeInteger. You should use
a <sstream> output string streams to implement this
function. There were some examples of using string streams in this
weeks lecture videos and materials.
Your LargeInteger class will have an array of integer
values. This array will be stored in the member variable
named digit. If you look in
the LargeInteger.hpp header file you were given that
gives the declarations of this class, you will notice
that digit is defined as:
This is a pointer to an int. However, as discussed in our
materials for this unit, we can allocate a block of integers
that digit will point to, and after we do that we can use
it just like a regular C array of int values,
e.g. digit[0] to access the first digit of
our LargeInteger, etc.
However, note that the digits of the LargeInteger are
to be stored in reverse of their display order. That is to say that
the 1's place (10^0) is in index 0 of digit[], the 10's place
(10^1) is in index 1, etc. So to be clear, if we are representing
the integer value 487 in our LargeInteger, we should
end up with
You should be clear about this representation. This makes it
easy to interpret the individual digits according to their correct
magnitude as a power of 10, as the index in the array corresponds
to the power of 10 of the digit.
So back to implementing the tostring() method. Given a
value of 487, notice that you will have to display
the 4 first in the output string, followed by 8 and 7. So
you will have to iterate through the digit[] array in
reverse order. Notice that the other member variable,
named numDigits will tell you how many digits are in
your LargeInteger currently.
Your tostring() is a member method of
the LargeInteger class. So don't forget when you add the
method to the implementation file, you will need to indicate that
the method is a member of the LargeInteger class.
The tostring() member function does not take any input,
and it should return a C++ string as its result. Also you
should be aware that the tostring() method does not
modify the value of the LargeInteger if it is called.
Thus it is an accessor method that simply returns information about
the LargeInteger. Any accessor method that doesn't cause the
object to change needs to be declared as a const member
function. We discussed const member functions in this
unit when we looked at class members. If you are unclear on the
concept, you should review what it means to be
a const member function, and how to declare a function to
be a const member function.
It is suggested, as usual, that you start by uncommenting the
first test case in the unit test file, which will be testing basics
of the tostring() method. A constructor has already been
implemented for you that allows for LargeInteger objects
to be created with an initial value. You should start by adding in
the correct prototype of your member function into the header file,
and adding a stub function that simply returns an empty string ""
initially. Do this to make sure that your project can compile and
run the tests (though they will be failing initially) before
continuing. Don't forget that your tostring() must be a
member of the LargeInteger class. This means that you
have to indicate it is a member function in the implementation
file, something like this:
This is an example of the stub function you need so your project
can compile and run the tests after uncommenting the first test
case. Once you add the stub implementation, build your project and
run the tests. The project should compile, and the test(s) for the
stub you just added should fail. After that you can do the actual
implementation of the tostring() member method so that is
passes the first set of tests.
When you are satisfied with your work and you can compile and
pass the first set of tests, you should commit your changes and
push them to the Feedback pull request of your class
repository.
#main.cpp
* Practice with classes and dynamic memory allocation. In this assignment we
* build a class to represent a large integer. We use dynamic memory allocation
* to manage an array of digits for the LargeInteger object. This file is a
* stub for a main() function so that we can build a version suitable for
* running in the debugger.
*/
#include "LargeInteger.hpp"
#include <cassert>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
/** @brief Main entry point
*
* Main entry point for debugging functions.
*
* @param argc The command line argument count, the number of arguments
* provided by user on the command line.
* @param argv An array of char* old style c-strings. Each argv[x]
* that is passed in holds one of the command line arguments provided
* by the user to the program when started.
*
* @returns int Returns 0 to indicate successfull completion of program,
* and a non-zero value to indicate an error code.
*/
int main(int argc, char** argv)
{
// an example of invoking the tostring member function and the array based constructor
/* uncomment the following to debug tostring() and the array based constructor
int digits1[] = {8, 4, 6, 3, 8, 4, 7, 4, 1, 2};
LargeInteger li1(10, digits1);
string res;
res = li1.tostring();
cout << "Expected result 2147483648, got result: " << res << endl;
assert(res == "2147483648"); // will fail assertion if we don't get what we expect
*/
cout << "Assignment Classes and Memory hello world" << endl;
// return 0 to indicate successful completion of program
return 0;
}
#Test-largerInteger.cpp
* Practice with classes and dynamic memory allocation. In this
* assignment we build a class to represent a large integer. We use
* dynamic memory allocation to manage an array of digits for the
* LargeInteger object. This file is a set of unit tests of the
* LargeInteger class using the catch2 unit test framework.
*/
#include "LargeInteger.hpp"
#include "catch.hpp"
#include <iostream>
using namespace std;
/** Task 1: test default constructor and tostring() implementation
* Uncomment the following test case block and write your code to pass
* the tests. You were given a default and standard constructor for
* the class. You need to implement the tostring() method, which we
* will use extensively in the tests to test your other member
* functions.
*/
/* uncomment the tests cases 1 at a time. This test case tests implementation
* of the tostring() member function which we use for further testing.
*/ TEST_CASE("<tostring()> member function tests using default and standard constructor",
"[task1]")
{
LargeInteger li1;
CHECK( li1.tostring() == "0" );
LargeInteger li2(12345);
CHECK( li2.tostring() == "12345" );
// an even larger value
LargeInteger li3(1234567890);
CHECK( li3.tostring() == "1234567890" );
}
Register for solutions, replies, and use board search function. Answer Happy Forum is an archive of questions covering all technical subjects across the Internet.
Post Reply