Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
lump = lump - fullback; cout << lump;
lump = 1.3 * lump; lump.set_mode('s'); cout << lump;
return 0; }
PE 11-7
// pe11-7.cpp
#include
#include \int main() {
using std::cout; using std::endl; using std::cin;
complex a(3.0, 4.0); // initialize to (3,4i) complex c;
cout << \ while (cin >> c) {
cout << \
cout << \ cout << \
cout << \ cout << \ cout << \ cout << \
cout << \ }
cout << \ return 0; }
// complex0.h
#ifndef COMPLEX0_H_ #define COMPLEX0_H_ #include
class complex {
private:
double r; double i; public:
complex();
complex(double real);
complex(double real, double imag); double magnitude();
complex operator+(const complex & z) const; complex operator-(const complex & z) const; complex operator~() const;
friend complex square(const complex & z);
friend complex operator*(const complex & z, const complex & w);
friend std::ostream & operator<<(std::ostream & os, const complex & z); friend std::istream & operator>>(std::istream & is, complex & z); };
#endif
SP 31 of 65 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
// complex0.cpp
#include
#include \
complex::complex() {
r = i = 0.0; }
complex::complex(double real) {
r = real; i = 0.0; }
complex::complex(double real, double imag) {
r = real; i = imag; }
double complex::magnitude() {
return std::sqrt(r*r + i*i); }
complex complex::operator+(const complex & z) const {
complex sum;
sum.r = r + z.r; sum.i = i + z.i; return sum; }
complex complex::operator-(const complex & z) const {
complex sum;
sum.r = r + z.r; sum.i = i + z.i; return sum; }
complex complex::operator~() const {
complex conjugate; conjugate.r = r; conjugate.i = -i; return conjugate; }
complex square (const complex & z) {
complex sq;
sq.r = z.r * z.r - z.i * z.i; sq.i = 2.0 * z.r * z.i; return sq; }
complex operator*(const complex & z, const complex & w) {
complex sq;
sq.r = w.r * z.r - w.i * z.i; sq.i = w.r * z.i + w.i * z.r; return sq; }
SP 32 of 65 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
std::ostream & operator<<(std::ostream & os, const complex & z) {
os << '(' << z.r << ',' << z.i << \ return os; }
std::istream & operator>>(std::istream & is, complex & z) {
std::cout << \ if (is >> z.r) {
std::cout << \ is >> z.i; }
return is; }
Chapter 12
PE 12-2
// pe12-2.cpp
#include
#include \int main() {
using std::cout; using std::cin;
String s1(\ String s2 = \ String s3;
cout << s2; // overloaded << operator cin >> s3; // overloaded >> operator s2 = \ cout << s2 << \ s2 = s2 + s1;
s2.stringup(); // converts string to uppercase cout << \ << \ s1 = \
// then String & operator=(const String&)
String rgb[3] = { String(s1), String(\ cout << \ String ans;
bool success = false; while (cin >> ans) {
ans.stringlow(); // converts string to lowercase for (int i = 0; i < 3; i++) {
if (ans == rgb[i]) // overloaded == operator {
cout << \ success = true; break; } }
if (success)
SP 33 of 65 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
break; else
cout << \ }
cout << \ return 0; }
// pe12strg.h
#ifndef PE12STRG_H_ #define PE12STRG_H_ #include
class String { private:
char * str; // pointer to a string int chars; // number of characters static int strings; // total number of strings public:
String();
String(const char * ps); // converts C++ string to String String(const String & s); ~String();
int numstrings(); int len();
void stringup(); void stringlow(); int has(char ch);
String & operator=(const String & s);
friend std::ostream & operator<<(std::ostream & os, const String & s); friend std::istream & operator>>(std::istream & os, String & s); friend String operator+(const String & s1, const String & s2); friend int operator==(const String & s1, const String & s2); friend int operator<(const String & s1, const String & s2); friend int operator>(const String & s1, const String & s2); };
#endif
// pe12strg.cpp
#include
//#include \
#include \int String::strings = 0;
String::String() {
str = NULL; chars = 0; strings++; }
String::String(const char * ps) {
chars = std::strlen(ps); str = new char [chars + 1]; std::strcpy(str, ps); strings++; }
String::String(const String & s) {
chars = s.chars;
SP 34 of 65 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
str = new char [chars + 1]; std::strcpy(str, s.str); strings++; }
String::~String() {
strings--;
delete [] str; }
int String::numstrings() {
return strings; }
int String::len() {
return chars; }
void String::stringup() {
for (int i = 0; i < chars; i++) str[i] = std::toupper(str[i]); }
void String::stringlow() {
for (int i = 0; i < chars; i++) str[i] = std::tolower(str[i]); }
String & String::operator=(const String & s) // allows chaining {
if (this == &s) // assignment to self return * this;
delete [] str; // free old contents, if any chars = s.chars;
str = new char [chars + 1]; std::strcpy(str, s.str); return * this; }
std::ostream & operator<<(std::ostream & os, const String & s) {
os << s.str; return os; }
std::istream & operator>>(std::istream & is, String & s) {
char temp[80];
is.getline(temp,80); s = temp; return is; }
String operator+(const String & s1, const String & s2) {
int len = s1.chars + s2.chars; char * ps = new char [len + 1]; std::strcpy(ps, s1.str); std::strcat(ps, s2.str);
SP 35 of 65 September 2, 2004