answersLogoWhite

0


Best Answer

Am not sure it is a good idea to embed the integer as a string. Aside from the inefficiency of storing a 5-digit number as a string, what would be the expected result of adding two IntStr objects together? E.g., "40" + "2" could either be "42" (add), or "402" (append) depending on your intentions. And if you wished to add an IntStr and an integer, such as "40" + 2, then you'd have to convert the embedded string back to an int, perform the addition, and if the sum is less than 100000, then convert the sum back to a string. This is highly inefficient. Personally, I'd embed the integer itself and provide accessors and casts to explicitly convert it to a string on an as-required basis only, since the numeric form is likely to be far more useful to you than the string form.

However, to answer the question as it stands, the following is one possible solution.

#include<iostream>

#include<string>

#include<sstream>

class IntStr

{

private:

std::string m_string;

public:

IntStr():m_string("0") {}

IntStr(const unsigned int decimal):m_string("0")

{

set_int(decimal);

}

IntStr& operator=( const unsigned int decimal )

{

return( set_int( decimal ));

}

std::string get_int()const{ return(m_string); }

IntStr& set_int(const unsigned int decimal)

{

if( decimal<100000 )

{

std::stringstream ss;

ss<<decimal;

m_string = ss.str();

}

return( *this );

}

};

int main()

{

IntStr a; // default constructor.

a = 42; // assignment operator.

IntStr b = a; // copy constructor.

IntStr c(1000); // user-defined constructor.

IntStr d(100000); // overflow check.

std::cout<<"a = "<<a.get_int().c_str()<<std::endl;

std::cout<<"b = "<<b.get_int().c_str()<<std::endl;

std::cout<<"c = "<<c.get_int().c_str()<<std::endl;

std::cout<<"d = "<<d.get_int().c_str()<<std::endl;

}

Example output:

a = 42

b = 42

c = 1000

d = 0

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Create a class IntStr that stores a positive decimal integer as a string the maximum number of decimal digits in the string can be assumed to be only 5 digits so the maximum value that can be stored?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What happens if you assign a number with a decimal to an integer?

Some smart compilers will not allow you do that. If you managed to do that, there are couple different results, the most predictible is decimal part will be cut, only what left will be shown. If it exceed the amount of memory reserved for int (double &gt; float &gt; int), it will show the maximum or positive or negative value for int type, or it will shifted by some amount from the maximum or minimum.


What is the maximum integer allowed in an integer-type variable?

That varies from each programming language. As a matter of fact, many languages do not put a limit on the maximum size of a variable. It will handle any string, integer, resource, pointer, or other type size, as long as it fits into the memory of the machine running the process.


What are the smallest and the largest integer values for primitive type short?

The smallest value is -32,768 and the maximum is 32,767


What is the maximum value that can be stored in an integer in c?

if it is a signed int the the range is -32768 to 32767if its unsigned then 0 to 65535


Maximum value of an array?

The maximum number of elements will depend on the type of array and the available memory. An array of char requires only 1 byte per element but an array of pointers requires 4 bytes per element (8 bytes on 64-bit systems). Arrays of objects or structures would likely require more memory per element.For all practical purposes, the maximum size is 2,147,483,647 elements, which is the maximum positive range for a 4-byte integer (0x7FFFFFFF). At 1 byte per element, that works out at 2GB.

Related questions

Write an algorithm to find the largest number in a group of three integer numbers?

The following algorithm works for any number of integers: Assume the first number is the maximum - maximum = (first number). Compare your assumed maximum with the second number. If the second number is larger than the assumed maximum, replace the old assumed maximum with the second number. Repeat for the third number, for the fourth, etc. - always copying the nth. element to the assumed maximum if you find one that is larger than your previous maximum.


What is maximum number that can be represented by integer?

An integer is any whole number. For example, 4, 16, -8 are all integers. There is no decimal part. Obviously, there is no "maximum number" as these numbers continue up to infinity.


What happens if you assign a number with a decimal to an integer?

Some smart compilers will not allow you do that. If you managed to do that, there are couple different results, the most predictible is decimal part will be cut, only what left will be shown. If it exceed the amount of memory reserved for int (double &gt; float &gt; int), it will show the maximum or positive or negative value for int type, or it will shifted by some amount from the maximum or minimum.


What is the difference between signed integer and unsigned integer?

An unsigned integer cannot be negative. It has a maximum positive value twice that of a signed integer. Max signed: 128 Max signed: 256 I could be off by one there, though.


What is the maximum count of decimal of a 5-bit binary counter?

A 5-bit binary counter, interpreted as an unsigned integer, has a range of 0 to 31. Interpreted as a two's complement signed integer, it has a range of -16 to +15.


What is the maximum amount of money in rune scape?

A maximum cash stack is 2,147,483,647 gp. It only goes to this because it is the largest positive value of a signed integer on a 32 bit operating system. See the related links for a Wikipedia article for more information on it.


From 11 positive integer scores on a 10-point quiz the mean is 8 the median is 8 and the mode is 7 Find the maximum number of perfect scores possible on this test?

3 perfect scores.


What is the maximum and minimum of positive integers?

minimum is '0' and maximum cannot be defined


What is the highest decimal number that can be represented by eight binary digits using two's compliment?

Since you mention two's complement, we have to assume that you are dealing with positive and negative numbers which means that the highest order bit is the sign bit. So the maximum positive number would be hex 7F which is equivalent to 127 and the maximum negative number would be -128


What is the maximum number of decimal places allowed after a decimal point?

An infinite amount of numbers can theoretically be placed after the decimal point.


Why four billion studs max in Lego video games?

Four billion, 4294967296, is the maximum integer that can be stored into a 32 bit integer variable.


What is the maximum integer allowed in an integer-type variable?

That varies from each programming language. As a matter of fact, many languages do not put a limit on the maximum size of a variable. It will handle any string, integer, resource, pointer, or other type size, as long as it fits into the memory of the machine running the process.