CP1_11b_string_Class_2_1112

CP1_11b_string_Class_2_1112 - The The string class (pt. 2)...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
he tring class The string class t. ) (pt. 2)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Unsigned int An int may be signed or unsigned . If neither is specified the int is signed . Signed int : range is from -2 31 to 2 31 -1 inclusive for most 32- bit platforms (-2,147,483,648 to 2,147,483,647). Unsigned int : only non-negative integers, ranging from 0 to 2 32 -1 inclusive (0 to 4,294,967,295). As the length of a string must be a non-negative number, we can use an unsigned int to represent it. unsigned int len_s = s.length(); or member functions like length the return type is often ize t For member functions like length, the return type is often size_t , which is an unsigned integral type. ost compilers will convert this silently to t hen necessary but 2 CSIS1117B Computer Programming 1 2011-2012 Most compilers will convert this silently to int when necessary, but some compilers will warn about the different types.
Background image of page 2
Unsigned int Overflow occurs when the result of an arithmetic operation exceeds the representable range. In C++, no warning or error message will be given for integer overflow, but the results may be incorrect. int i = 2147483647; // 2^31 - 1 nsigned nt 4294967295u; / ^32 ; unsigned int j = 4294967295u; // 23 2 1; cout << i << ", " << j << endl; ++i; +j; This is an unsigned integer ++j; cout << i << ", " << j << endl; i = -1; constant. The u is required if it is larger than can be represented as an int j = -1; cout << i << ", " << j << endl; 2147483647, 4294967295 147483648 0 3 CSIS1117B Computer Programming 1 2011-2012 No complaint here either. Compiler will not prevent this 2147483648, 0 -1, 4294967295
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Mixing signed and unsigned ints These can be mixed as long as you know what you are doing. But it is easy to make a mistake and so it best to avoid it. f an expression uses both signed and unsigned ts If an expression uses both signed and unsigned ints then the value of the signed int is promoted to unsigned when the expression is evaluated. unsigned int ui = 1000; int j = 999; int k = -1; ool 1 = i j ; bool b1 (ui > j); bool b2 = (ui > k); b1 = true b2 = false 4 CSIS1117B Computer Programming 1 2011-2012
Background image of page 4
Constructors of string Classes have a special kind function that is called automatically when we create an object of that class. The purpose of the constructor is to initialize the member variables of the object and perform any other initialization that is eeded. needed.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 15

CP1_11b_string_Class_2_1112 - The The string class (pt. 2)...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online