
lafore_robert_objectoriented_programming_in_c
.pdf

Appendix G
956
Solutions to Exercises
1.
//ex11_1.cpp
//publication class and derived classes #include <iostream>
#include <string> using namespace std;
////////////////////////////////////////////////////////////////
class publication
{
private:
string title; float price;
public:
virtual void getdata()
{
cout << “\nEnter title: “; cin >> title; cout << “Enter price: “; cin >> price;
}
virtual void putdata()
{
cout << “\n\nTitle: “ << title; cout << “\nPrice: “ << price;
}
};
////////////////////////////////////////////////////////////////
class book : public publication
{
private:
int pages; public:
void getdata()
{
publication::getdata();
cout << “Enter number of pages: “; cin >> pages;
}
void putdata()
{
publication::putdata();
cout << “\nPages: “ << pages;
}
};
////////////////////////////////////////////////////////////////
class tape : public publication
{


Appendix G
958
////////////////////////////////////////////////////////////////
class Distance |
//English Distance class |
{ |
|
private: |
|
int feet; |
|
float inches; |
|
public: |
|
Distance() |
//constructor (no args) |
{ feet = 0; inches = 0.0; } |
|
Distance(float fltfeet) |
//constructor (one arg) |
{ |
//feet is integer part |
feet = static_cast<int>(fltfeet); |
|
inches = 12*(fltfeet-feet); //inches is what’s left |
|
} |
//constructor (two args) |
Distance(int ft, float in) : feet(ft), inches(in)
|
{ } |
|
|
void showdist() |
//display distance |
||
|
{ cout << feet << “\’-” << inches << ‘\”’; } |
||
friend Distance operator * (Distance, Distance); //friend |
|||
}; |
|
|
|
//-------------------------------------------------------------- |
|
|
|
|
|
//multiply d1 by d2 |
|
Distance |
operator * (Distance d1, Distance d2) |
||
{ |
|
|
|
float fltfeet1 = d1.feet + d1.inches/12; |
//convert to float |
||
float fltfeet2 = d2.feet + d2.inches/12; |
|
||
float multfeet = fltfeet1 * fltfeet2; |
//find the product |
||
return Distance(multfeet); |
//return temp Distance |
||
} |
|
|
|
////////////////////////////////////////////////////////////////
int main() |
|
|
{ |
|
|
Distance dist1(3, 6.0); |
//make some distances |
|
Distance dist2(2, 3.0); |
|
|
Distance dist3; |
|
|
dist3 = |
dist1 * dist2; |
//multiplication |
dist3 = |
10.0 * dist3; |
//mult and conversion |
|
|
//display all distances |
cout << |
“\ndist1 = “; dist1.showdist(); |
|
cout << |
“\ndist2 = “; dist2.showdist(); |
|
cout << |
“\ndist3 = “; dist3.showdist(); |
|
cout << |
endl; |
|
return 0;
}


960 |
Appendix G |
|
Array arr1(ASIZE); |
//make an array |
|
for(int |
j=0; j<ASIZE; j++) //fill it with squares |
|
arr1[j] = j*j; |
|
|
Array arr2(arr1); |
//use the copy constructor |
|
cout << |
“\narr2: “; |
|
for(j=0; j<ASIZE; j++) |
//check that it worked |
|
cout |
<< arr2[j] << “ |
“; |
Array arr3, arr4; |
//make two empty Array objects |
|
arr4 = arr3 = arr1; |
//use the assignment operator |
|
cout << |
“\narr3: “; |
|
for(j=0; j<ASIZE; j++) |
//check that it worked on arr3 |
|
cout |
<< arr3[j] << “ |
“; |
cout << |
“\narr4: “; |
|
for(j=0; j<ASIZE; j++) |
//check that it worked on arr4 |
|
cout |
<< arr4[j] << “ |
“; |
cout << |
endl; |
|
return 0;
}
Chapter 12
Answers to Questions
1.b, c
2.ios
3.ifstream, ofstream, and fstream
4.ofstream salefile (“SALES.JUN”);
5.true
6.if(foobar)
7.d
8.fileOut.put(ch); (where ch is the character)
9.c
10.ifile.read( (char*)buff, sizeof(buff) );
11.a, b, d
12.the byte location at which the next read or write operation will take place
13.false; file pointer can be a synonym for current position
14.f1.seekg(-13, ios::cur);


962 |
Appendix G |
|
do |
|
// data from user to file |
|
|
{ |
|
|
|
cout << “\nDistance”; |
|
|
|
dist.getdist(); |
// get a distance |
|
|
|
// write to file |
|
|
file.write( (char*)&dist, sizeof(dist) ); |
|
|
|
cout << “Enter another distance (y/n)? “; |
|
|
|
cin >> ch; |
|
|
|
} |
|
|
while(ch==’y’); |
// quit on ‘n’ |
|
|
file.seekg(0); |
// reset to start of file |
||
|
|
// read first distance |
|
file.read( (char*)&dist, sizeof(dist) ); |
|
||
int count = 0; |
|
|
|
while( !file.eof() ) |
// quit on EOF |
|
|
|
{ |
|
|
|
cout << “\nDistance “ << ++count << “: “; |
// display dist |
|
|
dist.showdist(); |
|
|
|
file.read( (char*)&dist, sizeof(dist) ); |
// read another |
|
|
} |
|
// distance |
cout << endl; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
2. |
|
|
|
// ex12_2.cpp |
|
|
|
// imitates COPY command |
|
|
|
#include <fstream> |
//for file functions |
||
#include <iostream> |
|
|
|
using |
namespace std; |
|
|
#include <process.h> |
//for exit() |
|
|
int main(int argc, char* argv[] ) |
|
|
|
{ |
|
|
|
if( argc != 3 ) |
|
|
|
|
{ cerr << “\nFormat: ocopy srcfile destfile”; exit(-1); } |
||
char ch; |
//character to read |
||
ifstream infile; |
//create file for input |
||
infile.open( argv[1] ); |
//open file |
|
|
if( !infile ) |
//check for errors |
||
|
{ cerr << “\nCan’t open “ << argv[1]; exit(-1); } |
||
ofstream outfile; |
//create file for output |
||
outfile.open( argv[2] ); |
//open file |
|


Appendix G
964
6.true
7.c, d
8.true
9.false
10.a, c, d
11.linking
12.false
13.d
14.scope
15.object
16.declared, file B
17.true
18.b
19.false
20.d
21.b
22.namespace
22. b, d
Chapter 14
Answers to Questions
1.b and c
2.class
3.false; different functions are created at compile time
template<class T> T times2(T arg)
{
return arg*2;
}
5.b
6.true
7.instantiating