
lafore_robert_objectoriented_programming_in_c
.pdf

Appendix G
926
int main()
{ |
|
|
double number, answer; |
|
|
int pow; |
|
|
char |
yeserno; |
|
cout |
<< “\nEnter number: “; |
//get number |
cin >> number; |
|
|
cout |
<< “Want to enter a power (y/n)? “; |
|
cin >> yeserno; |
|
|
if( yeserno == ‘y’ ) |
//user wants a non-2 power? |
|
{ |
|
|
cout << “Enter power: “; |
|
|
cin >> pow; |
|
|
answer = power(number, pow); |
//raise number to pow |
|
} |
|
|
else |
|
|
answer = power(number); |
//square the number |
|
cout |
<< “Answer is “ << answer << endl; |
return 0;
}
//--------------------------------------------------------------
//power()
//returns number n raised to a power p double power( double n, int p )
{
double result = 1.0; |
//start with 1 |
|
for(int j=0; |
j<p; j++) |
//multiply by n |
result *= |
n; |
//p times |
return result;
}
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);


Appendix G
928
//--------------------------------------------------------------
//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();
};
5.false; both data and functions can be private or public
6.leverage lever1;
7.d
8.lever1.pry();
9.inline (also private)


930 |
Appendix G |
|
Int(int ii) |
|
//create and initialize an Int |
|
{ i = ii; |
} |
|
|
void |
add(Int |
i2, Int i3) //add two Ints |
|
{ |
i = i2.i + i3.i; } |
|
|
void |
display() |
//display an Int |
|
{ |
cout << |
i; } |
|
};
////////////////////////////////////////////////////////////////
int main() |
|
|
{ |
|
|
Int Int1(7); |
//create |
and initialize an Int |
Int Int2(11); |
//create |
and initialize an Int |
Int Int3; |
//create |
an Int |
Int3.add(Int1, Int2); |
|
//add two Ints |
cout << “\nInt3 = “; Int3.display(); |
//display result |
|
cout << endl; |
|
|
return 0; |
|
|
} |
|
|
2.
//ex6_2.cpp
//uses class to model toll booth #include <iostream>
using namespace std; #include <conio.h>
const |
char ESC = 27; |
//escape key |
ASCII code |
const |
double TOLL = 0.5; |
//toll is 50 |
cents |
////////////////////////////////////////////////////////////////
class tollBooth |
|
{ |
|
private: |
|
unsigned int totalCars; |
//total cars passed today |
double totalCash; |
//total money collected today |
public: |
//constructor |
tollBooth() : totalCars(0), totalCash(0.0)
{}
void |
payingCar() |
//a |
car |
paid |
{ |
totalCars++; totalCash += TOLL; } |
|
|
|
void |
nopayCar() |
//a |
car |
didn’t pay |
{ |
totalCars++; } |
|
|
|
void |
display() const |
//display totals |
{cout << “\nCars=” << totalCars
<<“, cash=” << totalCash
<<endl; }
};


932 |
Appendix G |
|
if( mins > 59 ) |
//if |
overflow, |
{ mins -= 60; hrs++; } |
// |
carry an hour |
hrs += t1.hrs + t2.hrs; |
//add hours |
|
} |
|
|
};
////////////////////////////////////////////////////////////////
int main() |
|
{ |
|
const time time1(5, 59, 59); |
//creates and initialze |
const time time2(4, 30, 30); |
// two times |
time time3; |
//create another time |
time3.add_time(time1, time2); |
//add two times |
cout << “time3 = “; time3.display(); |
//display result |
cout << endl; |
|
return 0; |
|
} |
|
Chapter 7
Answers to Questions
1.d
2.same
3.double doubleArray[100];
4.0, 9
5.cout << doubleArray[j];
6.c
7.int coins[] = { 1, 5, 10, 25, 50, 100 };
8.d
9.twoD[2][4]
10.true
11.float flarr[3][3] = { {52,27,83}, {94,73,49}, {3,6,1} };
12.memory address
13.a, d
14.an array with 1000 elements of structure or class employee
15.emplist[16].salary
16.d


934 |
Appendix G |
|
reversit(str); |
//reverse the string |
||
cout << |
“Reversed string is: “; |
//display it |
|
cout << |
str << endl; |
|
|
return 0; |
|
|
|
} |
|
|
|
//-------------------------------------------------------------- |
|
|
|
//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;
}
}
//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;