#define _CRT_NONSTDC_NO_DEPRECATE 1 #define _CRT_SECURE_NO_DEPRECATE 1 #include page 80 #include using namespace std ; class part { int i ; char s ; public: part() : i() { ;} part(int j) : p(j) { ;} } ; int main() { part p1(1), p2(2), p3 ; cin.get() ; return 0 ; } page 82 #include using namespace std ; class part { int i ; public: part() :i() { } part(int j) :i(j) { } part operator + (part p) { int k ; k = i + p.i ; return part(k) ; } }; int main() { part p1(1), p2(2), p3 ; p3 = p1 + p2 ; return 0 ; } page 83 #include using namespace std ; class part { int i ; public: part() :i() { } part(int j) :i(j) { } part operator + (part p) ; } ; part part::operator + (part p); { int k ; k = i + p.i ; return part(k) ; } int main() { part p1(1), p2(2), p3 ; p3 = p1 + p2 ; return 0 ; } page 84: #include #include // for strcpy(), strcat() using namespace std; const int SZ = 80; // size of all String objects class String // user-defined string type { private: char str[SZ] ; // holds a string public: String() // constructor, no args {strcpy(str, "") ; } String( char s[] ) // constructor, one arg {strcpy(str, s) ; } String operator + (String ss) // add a String to another { String temp; // make a temporary String strcpy(temp.str, str) ; // copy this string to temp strcat(temp.str, ss.str); // add the argument string return temp ; // return temp String } }; int main() { String s1 = "round" ; // uses one-argument constructor. String s2 = "about" ; // uses one-argument constructor. String s3; // uses no-argument constructor. s3 = s1 + s2 ; // add s2 to s1, assign to s3. cin.get(); return 0 ; } page 84 using _s: #include // for strcpy(), strcat() const int SZ = 80; // size of all String objects class String // user-defined string type { private: char str[SZ] ; // holds a string public: String() // constructor, no args {strcpy_s(str, "") ; } String( char s[] ) // constructor, one arg {strcpy_s(str, s) ; } String operator + (String ss) // add a String to another { String temp; // make a temporary String strcpy_s(temp.str, str) ; // copy this string to temp strcat_s(temp.str, ss.str); // add the argument string return temp ; // return temp String } }; int main() { String s1 = "round" ; // uses one-argument constructor. String s2 = "about" ; // uses one-argument constructor. String s3; // uses no-argument constructor. s3 = s1 + s2 ; // add s2 to s1, assign to s3. return 0 ; } page 86 #include using namespace std ; //////////////////////////////// class Distance { private: int feet ; public: Distance() :feet(1) {} void operator += (Distance dist) // doesn't need to return { // an object of type Distance! feet += dist.feet ; } ; }; int main() { Distance dist1,dist2 ; dist1 += dist2 ; // += is attached to dist1 return 0 ; } page 87 #include using namespace std ; int main() { int t ; int sal[3] ; t = sal[3] ; cout << t ; cin.get(); return 0 ; } page 87 #include #include using namespace std ; class safe { private: int arr[3] ; public: int operator [](int n) { if (n==3) {cout << "element 3 doesn't exist!" << endl ; exit(1) ;} return arr[n] ; } } ; int main() { safe sal ; int t ; t = sal[3] ; cout << t ; cin.get() ; return 0 ; } #include #include using namespace std ; class safe { private: int arr[3] ; public: safe(int i, int j, int k) { arr[0]= i ; arr[1]= j ; arr[2]= k ; } int operator [](int n) { if (n==3) {cout << "element 3 doesn't exist!" << endl ; cin.get() ; exit(1) ;} return arr[n] ; } } ; int main() { safe sa(1,2,3) ; int t ; t = sa[2] ; cout << t ; cin.get() ; return 0 ; } page 88 #include using namespace std ; int x = 5 ; int& getsetx() ; int main() { int y ; y= getsetx() ; cout << "y= " << y << endl ; getsetx() = 9 ; cout << "x= " << x << endl ; return 0 ; } //////////////////////////// int& getsetx() { return x ; } //////////////////////////// page 89 #include #include " using namespace std ; class safe { private: int arr[3] ; public: int& operator [](int n) { if (n == 3) {cout << "element 3 doesn't exist!" << endl ; cin.get() ; exit(1) ;} return arr[n] ; } } ; //////////////////////////////// int main() { safe sa ; sa[0]= 5 ; int t = sa[0] ; cout << t << endl ; t = sa[3] ; sa[3]=4 ; cin.get() ; return 0 ; } page 90 ///////////////////////////////////// #include using namespace std ; class part { private: int i, j ; public: part(): i(),j() { } part(int i_, int j_): i(i_),j(j_) { } operator float() { return static_cast(i + j) ; } }; void main() { float f ; part p(2,3) ; f = static_cast(p) ; cout << f << endl ; cin.get() ; } ///////////////////////////////////// page 90 operator float() const { //inches is a float member of the class. float fracfeet = inches/12;//convert the inches fracfeet += static_cast(feet); //add the feet return fracfeet/MTF; //convert to meters } page 91 #include using namespace std ; int main() { char str[] = "round\n" ; cout << str; cin.get() ; return 0 ; } page 91 #include #include using namespace std ; class String // user-defined string type { private: char str[20] ; // holds a string public: String() // constructor 0, no args { } String( char s[] ) // constructor 1, one arg {strcpy(str, s) ; } // convert string to String operator char*() // conversion function {return str ; } // convert String to string }; int main() { String s2 = "Bonne Annee!"; // uses constructor 1 cout << (char*)s2 << endl ; // use conversion function // to convert String to string cin.get() ; return 0 ; } page 92 #include using namespace std ; class dist { private: int d ; public: dist (): d() { } dist (int j): d(j) { } }; ////////////////////////////// class part { private: int i ; public: part (int j): i(j) { } }; /////////////////////////////// int main() { part p1(8) ; dist d1 ; return 0 ; } /////////////////////////////// page 93 page 94 #include using namespace std ; class part { private: int p ; public: part (): p() { } part (int j): p(j) { } int getp() {return p ;} }; /////////////////////////////// class dist { private: int d ; public: dist (): d() { ;} dist (int j): d(j) { ;} dist(part pt) { d = pt.getp() ; } } ; int main() { part p1(8) ; dist d1 ; d1 = dist(p1) ; return 0 ; } page 95 #include using namespace std ; class part { private: int i ; public: part () { ;} part (int j): i(j) { ;} } ; int main() { void fun(part) ; part p1 ; fun(p1) ; return 0 ; } void fun(part p) { ;} page 97 #include using namespace std ; class part { private: //mutable int i ; public: part () { ;} part (int j): i(j) { ;} void fun(void) const {i = 2 ;} } ; int main() { part p1(3) ; p1.fun() ; return 0 ; }