The document describes two programming tasks:
1) Write a program that overloads the += operator to concatenate two string objects. The operator allows statements like s1 += s2 to add s2 to s1 and store the result in s1.
2) Create an Int class that overloads the +, -, *, and / operators to perform integer arithmetic. The operators check for overflow and print a warning if the result exceeds the normal integer range, terminating the program. A sample program tests the Int class.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
49 views3 pages
6 Post Lab Tasks
The document describes two programming tasks:
1) Write a program that overloads the += operator to concatenate two string objects. The operator allows statements like s1 += s2 to add s2 to s1 and store the result in s1.
2) Create an Int class that overloads the +, -, *, and / operators to perform integer arithmetic. The operators check for overflow and print a warning if the result exceeds the normal integer range, terminating the program. A sample program tests the Int class.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3
6
Post Lab Tasks:
Write a program that uses an overloaded += operator for adding(concatenating) two strings together. This operator should allow statements like s1 += s2; where s2 is added (concatenated) to s1 and the result is left in s1 . The operator should also permit the results of the operation to be used in other calculations, as in s3 = s1 += s2; #include <iostream> #include <cstring> using namespace std; #include <process.h> class String { private: enum { SZ = 80 }; char str[SZ]; public: String() { strcpy(str, ""); } String( char s[] ) { strcpy(str, s); } void display() { cout << str; } String operator += (String ss) { if( strlen(str) + strlen(ss.str) >= SZ ) { cout << "\nString overflow"; exit(1); } strcat(str, ss.str); return String(str); } }; int main() { String s1 = "online classes! "; String s2 = "kuch samaj nahi ati!"; String s3; s3 = s1 += s2; cout << "\ns1="; s1.display(); cout << "\ns2="; s2.display(); cout << "\ns3="; s3.display(); cout << endl; return 0; } 5. Home Tasks 5.1. Create a class Int. Overload four integer arithmetic operators (+, -, *, and /) so that they operate on objects of type Int. If the result of any such arithmetic operation exceeds the normal range of ints (in a 32-bit environment)— from 2,147,483,648 to – 2,147,483,647—have the operator print a warning and terminate the program. Such a data type might be useful where mistakes caused by arithmetic overflow are unacceptable. Hint: To facilitate checking for overflow, perform the calculations using type long double. Write a program to test this class. #include <iostream> using namespace std; #include <process.h> class Int { private: int i; public: Int() : i(0) {} Int(int ii) : i(ii) {} void putInt() { cout << i; } void getInt() { cin >> i; } operator int() { return i; } Int operator + (Int i2) { return checkit( double(i)+ double(i2) ); } Int operator - (Int i2) { return checkit( double(i)- double(i2) ); } Int operator * (Int i2) { return checkit( double(i)* double(i2) ); } Int operator / (Int i2) { return checkit( double(i)/ double(i2) ); } Int checkit(long double answer) { if( answer > 2147483647.0L || answer < -2147483647.0L ) { cout << "\nOverflow Error\n"; exit(1); } return Int( int(answer) ); } }; int main() { Int alpha = 20; Int beta = 7; Int delta, gamma; gamma = alpha + beta; cout << "\ngamma="; gamma.putInt(); gamma = alpha - beta; cout << "\ngamma="; gamma.putInt(); gamma = alpha * beta; cout << "\ngamma="; gamma.putInt(); gamma = alpha / beta; cout << "\ngamma="; gamma.putInt(); delta = 2147483647; gamma = delta + alpha; delta = -2147483647; gamma = delta - alpha; cout << endl; return 0; }