Mitran PDF
Mitran PDF
Mitran PDF
Answers to Questions
1. procedural, object-oriented
2. b
4. a
5. data hiding
6. a, d
7. objects
9. encapsulation
10. d
11. false; most lines of code are the same in C and C++
12. polymorphism
13. d
14. b
15. b, d
Chapter 2
Answers to Questions
1. b, c
2. parentheses
3. braces { }
6. // this is a comment
/* this is a comment */
7. a, d
8. a. 4
b. 10
c. 4
d. 4
9. false
b. character constant
c. floating-point constant
e. function name
14. IOSTREAM
16. IOMANIP
18. true
19. 2
22. 1
23. 2020
24. to provide declarations and other data for library functions, overloaded operators, and objects
25. library
Solutions to Exercises
1. // ex2_1.cpp
// converts gallons to cubic feet
#include <iostream>
using namespace std;
int main()
{
float gallons, cufeet;
2. // ex2_2.cpp
// generates table
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << 1990 << setw(8) << 135 << endl
<< 1991 << setw(8) << 7290 << endl
<< 1992 << setw(8) << 11300 << endl
<< 1993 << setw(8) << 16200 << endl;
return 0;
}
3. // ex2_3.cpp
// exercises arithmetic assignment and decrement
#include <iostream>
using namespace std;
int main()
{
int var = 10;
Chapter 3
Answers to Questions
1. b, c
2. george != sally
4. The initialize expression initializes the loop variable, the test expression tests the loop
variable, and the increment expression changes the loop variable.
5. c, d
6. true
9. c
15. d
17. a, c
18. '\r'
20. reformatting
21. switch(ch)
{
case 'y':
cout << "Yes";
break;
case 'n':
cout << "No";
break;
default:
cout << "Unknown response";
}
23. d
28. b
Solutions to Exercises
1. // ex3_1.cpp
// displays multiples of a number
#include <iostream>
#include <iomanip> //for setw()
using namespace std;
int main()
{
2. // ex3_2.cpp
// converts fahrenheit to centigrad, or
// centigrad to fahrenheit
#include <iostream>
using namespace std;
int main()
{
int response;
double temper;
3. // ex3_3.cpp
// makes a number out of digits
#include <iostream>
using namespace std;
#include <conio.h> //for getche()
int main()
{
char ch;
unsigned long total = 0; //this holds the number
4. // ex3_4.cpp
// models four-function calculator
#include <iostream>
using namespace std;
int main()
{
double n1, n2, ans;
char oper, ch;
do {
cout << "\nEnter first number, operator, second number: ";
cin >> n1 >> oper >> n2;
switch(oper)
{
case '+': ans = n1 + n2; break;
case '-': ans = n1 - n2; break;
case '*': ans = n1 * n2; break;
case '/': ans = n1 / n2; break;
default: ans = 0;
}
cout << "Answer = " << ans;
cout << "\nDo another (Enter 'y' or 'n')? ";
cin >> ch;
} while( ch != 'n' );
return 0;
}
Chapter 4
Answers to Questions
1. b, d
2. true
3. semicolon
4. struct time
{
int hrs;
int mins;
int secs;
};
6. c
7. time2.hrs = 11;
10. true
12. c
13. enum players { B1, B2, SS, B3, RF, CF, LF, P, C };
15. a. no
b. yes
c. no
d. yes
16. 0, 1, 2
Solutions to Exercises
1. // ex4_1.cpp
// uses structure to store phone number
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
struct phone
{
int area; //area code (3 digits)
int exchange; //exchange (3 digits)
int number; //number (4 digits)
};
////////////////////////////////////////////////////////////////
int main()
{
phone ph1 = { 212, 767, 8900 }; //initialize phone number
phone ph2; //define phone number
// get phone no from user
cout << "\nEnter your area code, exchange, and number";
cout << "\n(Don't use leading zeros): ";
cin >> ph2.area >> ph2.exchange >> ph2.number;
2. // ex4_2.cpp
// structure models point on the plane
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
struct point
{
int xCo; //X coordinate
int yCo; //Y coordinate
};
////////////////////////////////////////////////////////////////
int main()
{
point p1, p2, p3; //define 3 points
3. // ex4_3.cpp
// uses structure to model volume of room
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
struct Distance
{
int feet;
float inches;
};
////////////////////////////////////////////////////////////////
struct Volume
{
Distance length;
Distance width;
Distance height;
};
////////////////////////////////////////////////////////////////
int main()
{
float l, w, h;
Volume room1 = { { 16, 3.5 }, { 12, 6.25 }, { 8, 1.75 } };
l = room1.length.feet + room1.length.inches/12.0;
w = room1.width.feet + room1.width.inches /12.0;
h = room1.height.feet + room1.height.inches/12.0;
cout << "Volume = " << l*w*h << " cubic feet\n";
return 0;
}
Chapter 5
Answers to Questions
2. definition
3. void foo()
{
cout << "foo";
}
4. declaration, prototype
5. body
6. call
7. declarator
8. c
9. false
10. To clarify the purpose of the arguments
11. a, b, c
13. one
14. Ttrue
16. void
17. main()
{
int times2(int); // prototype
int alpha = times2(37); // function call
}
18. d
19. to modify the original argument (or to avoid copying a large argument)
20. a, c
24. a, b
29. b, d
1. // ex5_1.cpp
// function finds area of circle
#include <iostream>
using namespace std;
float circarea(float radius);
int main()
{
double rad;
cout << "\nEnter radius of circle: ";
cin >> rad;
cout << "Area is " << circarea(rad) << endl;
return 0;
}
//--------------------------------------------------------------
float circarea(float r)
{
const float PI = 3.14159F;
return r * r * PI;
}
2. // ex5_2.cpp
// function raises number to a power
#include <iostream>
using namespace std;
double power( double n, int p=2); //p has default value 2
int main()
{
double number, answer;
int pow;
char yeserno;
3. // ex5_3.cpp
// function sets smaller of two numbers to 0
#include <iostream>
using namespace std;
int main()
{
void zeroSmaller(int&, int&);
int a=4, b=7, c=11, d=9;
zeroSmaller(a, b);
zeroSmaller(c, d);
4. // ex5_4.cpp
// function returns larger of two distances
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
struct Distance // English distance
{
int feet;
float inches;
};
////////////////////////////////////////////////////////////////
Distance bigengl(Distance, Distance); //declarations
void engldisp(Distance);
int main()
{
Distance d1, d2, d3; //define three lengths
//get length d1 from user
cout << "\nEnter feet: "; cin >> d1.feet;
cout << "Enter inches: "; cin >> d1.inches;
//get length d2 from user
cout << "\nEnter feet: "; cin >> d2.feet;
cout << "Enter inches: "; cin >> d2.inches;
//--------------------------------------------------------------
// bigengl()
// compares two structures of type Distance, returns the larger
Distance bigengl( Distance dd1, Distance dd2 )
{
if(dd1.feet > dd2.feet) //if feet are different, return
return dd1; //the one with the largest feet
if(dd1.feet < dd2.feet)
return dd2;
if(dd1.inches > dd2.inches) //if inches are different,
return dd1; //return one with largest
else //inches, or dd2 if equal
return dd2;
}
//--------------------------------------------------------------
// engldisp()
// display structure of type Distance in feet and inches
void engldisp( Distance dd )
{
cout << dd.feet << "\'-" << dd.inches << "\"";
}
Chapter 6
Answers to Questions
1. A class declaration describes how objects of a class will look when they are created.
2. class, object
3. c
4. class leverage
{
private:
int crowbar;
public:
void pry();
};
6. leverage lever1;
7. d
8. lever1.pry();
13. leverage()
{ crowbar = 0; }
14. true
15. a
16. int getcrow();
18. member functions and data are, by default, public in structures but private in classes
21. b, c, d
23. d
24. true
Solutions to Exercises
1. // ex6_1.cpp
// uses a class to model an integer data type
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class Int //(not the same as int)
{
private:
int i;
public:
Int() //create an Int
{ i = 0; }
2. // ex6_2.cpp
// uses class to model toll booth
#include <iostream>
using namespace std;
#include <conio.h>
////////////////////////////////////////////////////////////////
int main()
{
tollBooth booth1; //create a toll booth
char ch;
3. // ex6_3.cpp
// uses class to model a time data type
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class time
{
private:
int hrs, mins, secs;
public:
time() : hrs(0), mins(0), secs(0) //no-arg constructor
{ }
//3-arg constructor
time(int h, int m, int s) : hrs(h), mins(m), secs(s)
{ }
Chapter 7
Answers to Questions
1. d
2. same
3. double doubleArray[100];
4. 0, 9
6. c
8. d
9. twoD[2][4]
10. true
13. a, d
15. emplist[16].salary
16. d
19. manybirds[26].cheep();
21. char city[21] (An extra byte is needed for the null character.)
23. true
24. d
27. false
28. b, c
Solutions to Exercises
1. // ex7_1.cpp
// reverses a C-string
#include <iostream>
#include <cstring> //for strlen()
using namespace std;
int main()
{
void reversit( char[] ); //prototype
const int MAX = 80; //array size
char str[MAX]; //string
// reversit()
// function to reverse a string passed to it as an argument
void reversit( char s[] )
{
int len = strlen(s); // find length of string
for(int j = 0; j < len/2; j++) // swap each character
{ // in first half
char temp = s[j]; // with character
s[j] = s[len-j-1]; // in second half
s[len-j-1] = temp;
}
}
2. // ex7_2.cpp
// employee object uses a string as data
#include <iostream>
#include <string>
using namespace std;
////////////////////////////////////////////////////////////////
class employee
{
private:
string name;
long number;
public:
void getdata() //get data from user
{
cout << "\nEnter name: "; cin >> name;
cout << "Enter number: "; cin >> number;
}
void putdata() //display data
{
cout << "\n Name: " << name;
cout << "\n Number: " << number;
}
};
////////////////////////////////////////////////////////////////
int main()
{
employee emparr[100]; //an array of employees
int n = 0; //how many employees
char ch; //user response
3. // ex7_3.cpp
// averages an array of Distance objects input by user
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class Distance // English Distance class
{
private:
int feet;
float inches;
public:
Distance() //constructor (no args)
{ feet = 0; inches = 0; }
Distance(int ft, float in) //constructor (two args)
{ feet = ft; inches = in; }
do {
cout << "\nEnter a Distance"; //get Distances
distarr[count++].getdist(); //from user, put
cout << "\nDo another (y/n)? "; //in array
cin >> ch;
}while( ch != 'n' );
Chapter 8
Answers to Questions
1. a, c
2. x3.subtract(x2, x1);
3. x3 = x2 - x1;
4. true
6. none
7. b, d
8. void Distance::operator ++ ()
{
++feet;
}
9. Distance Distance::operator ++ ()
{
int f = ++feet;
float i = inches;
return Distance(f, i);
}
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
15. d
17. b
18. true
19. constructor
21. d
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)
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
3. // ex8_3.cpp
// overloaded '+' operator adds two times
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class time
{
private:
int hrs, mins, secs;
public:
time() : hrs(0), mins(0), secs(0) //no-arg constructor
{ } //3-arg constructor
time(int h, int m, int s) : hrs(h), mins(m), secs(s)
{ }
void display() //format 11:59:59
{ cout << hrs << ":" << mins << ":" << secs; }
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) ); }
delta = 2147483647;
gamma = delta + alpha; //overflow error
delta = -2147483647;
gamma = delta - alpha; //overflow error
Chapter 9
Answers to Questions
1. a, c
2. derived
3. b, c, d
5. false
6. protected
7. yes (assuming basefunc is not private)
8. BosworthObj.alfunc();
9. true
12. c, d
13. true
15. a
16. true
17. c
19. Base::func();
20. false
21. generalization
22. d
23. false
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;
public:
void getdata()
{
publication::getdata();
cout << "Enter number of pages: "; cin >> pages;
}
void putdata() const
{
publication::putdata();
cout << "\nPages: " << pages;
}
};
////////////////////////////////////////////////////////////////
class tape : private publication // derived class
{
private:
float time;
public:
void getdata()
{
publication::getdata();
cout << "Enter playing time: "; cin >> time;
}
void putdata() const
{
publication::putdata();
cout << "\nPlaying time: " << time;
}
};
////////////////////////////////////////////////////////////////
int main()
{
book book1; // define publications
tape tape1;
2. // ex9_2.cpp
//inheritance from String class
#include <iostream>
3. // ex9_3.cpp
// multiple inheritance with publication class
#include <iostream>
#include <string>
using namespace std;
////////////////////////////////////////////////////////////////
class publication
{
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 << "\n Price: " << price;
}
};
////////////////////////////////////////////////////////////////
class sales
{
private:
enum { MONTHS = 3 };
float salesArr[MONTHS];
public:
void getdata();
void putdata() const;
};
//--------------------------------------------------------------
void sales::getdata()
{
cout << " Enter sales for 3 months\n";
for(int j=0; j<MONTHS; j++)
{
cout << " Month " << j+1 << ": ";
cin >> salesArr[j];
}
}
//--------------------------------------------------------------
void sales::putdata() const
{
for(int j=0; j<MONTHS; j++)
{
cout << "\n Sales for month " << j+1 << ": ";
cout << salesArr[j];
}
}
////////////////////////////////////////////////////////////////
class book : private publication, private sales
{
private:
int pages;
public:
void getdata()
{
publication::getdata();
cout << " Enter number of pages: "; cin >> pages;
sales::getdata();
}
void putdata() const
{
publication::putdata();
cout << "\n Pages: " << pages;
sales::putdata();
}
};
////////////////////////////////////////////////////////////////
class tape : private publication, private sales
{
private:
float time;
public:
void getdata()
{
publication::getdata();
cout << " Enter playing time: "; cin >> time;
sales::getdata();
}
void putdata() const
{
publication::putdata();
cout << "\n Playing time: " << time;
sales::putdata();
}
};
////////////////////////////////////////////////////////////////
int main()
{
book book1; // define publications
tape tape1;