Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

lafore_robert_objectoriented_programming_in_c

.pdf
Скачиваний:
51
Добавлен:
27.03.2023
Размер:
8.94 Mб
Скачать

Answers to Questions and Exercises

3. base

4. virtual void dang(int); or void virtual dang(int);

5.late binding or dynamic binding

6.derived

7. virtual void aragorn()=0; or void virtual aragorn()=0;

8.a, c

9.dong* parr[10];

10.c

11.true

12.c, d

13.friend void harry(george);

14.a, c, d

15.friend class harry; or friend harry;

16.c

17.It performs a member-by-member copy.

18.zeta& operator = (zeta&);

19.a, b, d

20.false; the compiler provides a default copy constructor

21.a, d

22.Bertha(Bertha&);

23.true, if there was a reason to do so

24.a, c

25.true; trouble occurs if it’s returned by reference

26.They operate identically.

27.a, b

28.the object of which the function using it is a member

29.no; since this is a pointer, use this->da=37;

30.return *this;

31.c

32.links

33.true

34.a, b, c

955

G

Q E UESTIONS ANSWERS XERCISES AND TO

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

{

Answers to Questions and Exercises

private:

float time; public:

void getdata()

{

publication::getdata();

cout << “Enter playing time: “; cin >> time;

}

void putdata()

{

publication::putdata();

cout << “\nPlaying time: “ << time;

}

};

////////////////////////////////////////////////////////////////

int main()

 

 

 

{

 

 

 

publication* pubarr[100];

//array of ptrs

to pubs

int n = 0;

//number

of pubs in array

char choice;

//user’s

choice

 

do {

cout << “\nEnter data for book or tape (b/t)? “;

cin >> choice;

 

 

if( choice==’b’ )

//make book object

pubarr[n] = new book;

//

put in array

else

//make tape object

pubarr[n] = new tape;

//

put in array

pubarr[n++]->getdata();

//get data for object

cout << “ Enter another (y/n)? “;

//another pub?

cin >> choice;

 

 

}

 

 

while( choice ==’y’);

//cycle until not ‘y’

for(int j=0; j<n; j++)

//cycle thru all pubs

pubarr[j]->putdata();

//print data for pub

cout << endl;

 

 

return 0;

 

 

}

 

 

2.

//ex11_2.cpp

//friend square() function for Distance #include <iostream>

using namespace std;

957

G

Q E UESTIONS ANSWERS XERCISES AND TO

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;

}

Answers to Questions and Exercises

3.

//ex11_3.cpp

//creates array class

//overloads assignment operator and copy constructor #include <iostream>

using namespace std;

////////////////////////////////////////////////////////////////

class Array

{

private:

 

 

int* ptr;

//pointer to “array” contents

int size;

//size of array

public:

 

 

Array() : ptr(0), size(0)

//no-argument constructor

{ }

 

 

Array(int s) : size(s)

//one-argument constructor

{ ptr = new int[s]; }

 

 

Array(Array&);

//copy constructor

~Array()

//destructor

{ delete[] ptr; }

 

 

int& operator [] (int j)

//overloaded subscript op

{ return *(ptr+j); }

 

 

Array& operator = (Array&);

//overloaded = operator

};

 

 

//--------------------------------------------------------------

 

 

Array::Array(Array& a)

//copy constructor

{

 

 

size = a.size;

//new one is same size

ptr = new int[size];

//get space for contents

for(int j=0; j<size; j++)

//copy contents to new one

*(ptr+j) = *(a.ptr+j);

 

 

}

 

 

//--------------------------------------------------------------

 

 

Array& Array::operator = (Array& a)

//overloaded = operator

{

 

 

delete[] ptr;

//delete old contents (if any)

size = a.size;

//make this object same size

ptr = new int[a.size];

//get space for new contents

for(int j=0; j<a.size; j++)

//copy contents to this object

*(ptr+j) = *(a.ptr+j);

 

 

return *this;

//return this object

}

 

 

////////////////////////////////////////////////////////////////

int main()

 

{

 

const int ASIZE = 10;

//size of array

959

G

Q E UESTIONS ANSWERS XERCISES AND TO

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);

Answers to Questions and Exercises

961

15.b

16.b, c

17.skipws causes whitespace characters to be ignored on input so that cin will not assume the input has terminated.

18.int main(int argc, char *argv[] )

19.PRN, LPT1

20.istream& operator >> (istream&, Sample& )

Solutions to Exercises

1.

//

ex12_1.cpp

 

//

write

array

 

#include

<iostream>

 

#include

<fstream>

// for file streams

using namespace std;

////////////////////////////////////////////////////////////////

class Distance

// English Distance class

{

 

private:

 

int feet;

 

float inches;

 

public:

 

Distance() : feet(0), inches(0.0) // constructor (no args)

{ }

// constructor (two args)

Distance(int ft, float in) : feet(ft), inches(in)

{}

void

getdist()

// get length from user

{

 

 

 

cout << “\n

Enter feet: “;

cin >> feet;

cout << “

Enter inches: “;

cin >> inches;

}

 

 

 

void

showdist()

// display distance

{ cout << feet << “\’-” << inches << ‘\”’; }

};

////////////////////////////////////////////////////////////////

int main()

 

 

{

 

 

char ch;

 

 

Distance dist;

//

create a Distance object

fstream file;

//

create input/output file

 

// open it for append

file.open(“DIST.DAT”, ios::binary

| ios::app |

 

ios::out | ios::in );

G

Q E UESTIONS ANSWERS XERCISES AND TO

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

 

Answers to Questions and Exercises

963

if( !outfile )

//check for errors

{ cerr << “\nCan’t open “ << argv[2]; exit(-1); }

while( infile )

//until EOF

{

 

infile.get(ch);

//read a character

outfile.put(ch);

//write the character

}

 

return 0;

 

}

 

3.

//ex12_3.cpp

//displays size of file

#include <fstream>

//for

file functions

#include

<iostream>

 

 

using namespace std;

 

 

#include

<process.h>

//for

exit()

int main(int argc, char* argv[] )

{

if( argc != 2 )

{

cerr << “\nFormat: filename\n”; exit(-1); }

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); }

infile.seekg(0, ios::end);

//go to end of file

 

 

// report byte number

cout

<< “Size of “ << argv[1] << “ is “ << infile.tellg();

cout

<< endl;

 

return 0;

 

}

 

 

Chapter 13

Answers to Questions

1.a, b, c, d

2.#include directive

3.the compiler to compile the .CPP file and the linker to link the resulting .OBJ files

4.a, b

5.class library

G

Q E UESTIONS ANSWERS XERCISES AND TO

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