
lafore_robert_objectoriented_programming_in_c
.pdf

936 |
Appendix G |
|
public: |
|
|
|
Distance() |
//constructor (no args) |
||
{ feet = 0; inches = 0; } |
|
|
|
Distance(int ft, float in) |
//constructor (two args) |
||
{ feet = ft; inches = in; } |
|
||
void |
getdist() |
//get length from user |
|
{ |
|
|
|
cout << “\nEnter feet: “; |
cin >> feet; |
||
cout << “Enter inches: “; |
cin >> inches; |
||
} |
|
|
|
void |
showdist() |
//display distance |
|
{ |
cout << feet << “\’-” << inches << ‘\”’; } |
||
void |
add_dist( Distance, Distance ); |
//declarations |
|
void |
div_dist( Distance, int ); |
|
|
}; |
|
|
|
//--------------------------------------------------------------
//add Distances d2 and d3 void Distance::add_dist(Distance d2, Distance d3)
{ |
|
inches = d2.inches + d3.inches; |
//add the inches |
feet = 0; |
//(for possible carry) |
if(inches >= 12.0) |
//if total exceeds 12.0, |
{ |
//then decrease inches |
inches -= 12.0; |
//by 12.0 and |
feet++; |
//increase feet |
} |
//by 1 |
feet += d2.feet + d3.feet; |
//add the feet |
} |
|
//--------------------------------------------------------------
//divide Distance by int |
|
|
void Distance::div_dist(Distance d2, int divisor) |
|
|
{ |
|
|
float fltfeet = d2.feet + d2.inches/12.0; |
//convert to |
float |
fltfeet /= divisor; |
//do division |
|
feet = int(fltfeet); |
//get feet part |
|
inches = (fltfeet-feet) * 12.0; |
//get inches |
part |
} |
|
|
////////////////////////////////////////////////////////////////
int main() |
|
|
{ |
|
|
Distance distarr[100]; |
//array of |
100 Distances |
Distance total(0, 0.0), average; //other |
Distances |
|
int count = 0; |
//counts Distances input |


Appendix G
938
10.It increments the variable prior to use, the same as a non-overloaded ++operator.
11.c, e, b, a, d
12.true
13.b, c
14.
String String::operator ++ ()
{
int len = strlen(str); for(int j=0; j<len; j++)
str[j] = toupper( str[j] ) return String(str);
}
15.d
16.false if there is a conversion routine; true otherwise
17.b
18.true
19.constructor
20.true, but it will be hard for humans to understand
21.d
22.attributes, operations
23.false
24.a
Solutions to Exercises
1.
//ex8_1.cpp
//overloaded ‘-’ operator subtracts two Distances #include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class Distance |
//English Distance class |
{ |
|
private: |
|
int feet; |
|
float inches; |
|
public: |
//constructor (no args) |
Distance() : feet(0), inches(0.0) |
|
{ } |
//constructor (two args) |


Appendix G
940
dist3 = dist1 - dist2; |
//subtract |
//display all lengths cout << “\ndist1 = “; dist1.showdist();
cout << “\ndist2 = “; dist2.showdist(); cout << “\ndist3 = “; dist3.showdist(); cout << endl;
return 0;
}
2.
//ex8_2.cpp
//overloaded ‘+=’ operator concatenates strings #include <iostream>
#include |
<cstring> |
//for strcpy(), strlen() |
using namespace std; |
|
|
#include |
<process.h> |
//for exit() |
////////////////////////////////////////////////////////////////
class String |
//user-defined string type |
||
{ |
|
|
|
private: |
|
|
|
enum |
{ SZ = 80 }; |
|
//size of String objects |
char |
str[SZ]; |
|
//holds a C-string |
public: |
|
|
|
String() |
|
//no-arg constructor |
|
{ strcpy(str, “”); } |
|
||
String( char s[] ) |
|
//1-arg constructor |
|
{ strcpy(str, s); } |
|
||
void display() |
|
//display the String |
|
{ cout << str; } |
|
|
|
String operator += (String ss) //add a String to this one |
|||
{ |
|
|
//result stays in this one |
if( strlen(str) + strlen(ss.str) >= SZ ) |
|||
|
{ cout << “\nString overflow”; exit(1); } |
||
strcat(str, ss.str); |
//add the argument string |
||
return String(str); |
//return temp String |
||
} |
|
|
|
}; |
|
|
|
////////////////////////////////////////////////////////////////
int main() |
|
|
|
|
{ |
|
|
|
|
String s1 |
= “Merry Christmas! “; |
//uses 1-arg |
ctor |
|
String s2 |
= “Happy new year!”; |
//uses |
1-arg |
ctor |
String s3; |
//uses |
no-arg ctor |


Appendix G
942
time3 = |
time1 |
+ time2; |
//add two times |
|
cout |
<< |
“\ntime3 = “; time3.display(); //display result |
||
cout |
<< |
endl; |
|
|
return 0;
}
4.
//ex8_4.cpp
//overloaded arithmetic operators work with type Int #include <iostream>
using namespace std;
#include <process.h> //for exit()
////////////////////////////////////////////////////////////////
class Int |
|
|
|
{ |
|
|
|
private: |
|
|
|
int i; |
|
|
|
public: |
|
|
|
Int() : i(0) |
//no-arg constructor |
||
{ |
} |
|
|
Int(int ii) : i(ii) |
//1-arg constructor |
||
{ |
} |
// |
(int to Int) |
void |
putInt() |
//display Int |
|
{ |
cout << i; } |
|
|
void |
getInt() |
//read Int from kbd |
|
{ cin >> i; } |
|
|
|
operator int() |
//conversion operator |
||
{ return i; } |
// |
(Int to int) |
|
Int operator + (Int i2) |
//addition |
||
{ return checkit( long double(i)+long double(i2) ); } |
|||
Int operator - (Int i2) |
//subtraction |
||
{ return checkit( long double(i)-long double(i2) ); } |
|||
Int operator * (Int i2) |
//multiplication |
||
{ return checkit( long double(i)*long double(i2) ); } |
|||
Int operator / (Int i2) |
//division |
||
{ return checkit( long double(i)/long double(i2) ); } |
|||
Int checkit(long double answer) |
|
//check results |
|
{ |
|
|
|
if( answer > 2147483647.0L || answer < -2147483647.0L ) { cout << “\nOverflow Error\n”; exit(1); }
return Int( int(answer) );
}
};
////////////////////////////////////////////////////////////////
int main()
{


Appendix G
944
15.a
16.true
17.c
18.class Tire : public Wheel, public Rubber
19.Base::func();
20.false
21.generalization
22.d
23.false
24.stronger, aggregation
Solutions to Exercises
1.
//ex9_1.cpp
//publication class and derived classes #include <iostream>
#include <string> using namespace std;
////////////////////////////////////////////////////////////////
class publication |
// base class |
{ |
|
private: |
|
string title; |
|
float price; |
|
public: |
|
void getdata() |
|
{ |
|
cout << “\nEnter title: “; cin >> title; cout << “Enter price: “; cin >> price;
}
void putdata() const
{
cout << “\nTitle: “ << title; cout << “\nPrice: “ << price;
}
};
////////////////////////////////////////////////////////////////
class book : private publication |
// derived class |
{ |
|
private: |
|
int pages; |
|