
lafore_robert_objectoriented_programming_in_c
.pdf

Appendix G
946
#include <cstring> |
//for strcpy(), etc. |
using namespace std; |
|
////////////////////////////////////////////////////////////////
class |
String |
//base class |
|
{ |
|
|
|
protected: |
//Note: can’t be private |
||
|
enum |
{ SZ = 80 }; |
//size of all String objects |
|
char |
str[SZ]; |
//holds a C-string |
public: |
|
|
|
|
String() |
//constructor 0, no args |
|
|
{ str[0] = ‘\0’; } |
|
|
|
String( char s[] ) |
//constructor 1, one arg |
|
|
{ strcpy(str, s); } |
// convert string to String |
|
|
void display() const |
//display the String |
|
|
{ cout << str; } |
|
|
|
operator char*() |
//conversion function |
|
|
{ return str; } |
//convert String to C-string |
|
}; |
|
|
|
////////////////////////////////////////////////////////////////
class |
Pstring : public String |
//derived class |
{ |
|
|
public: |
|
|
|
Pstring( char s[] ); |
//constructor |
}; |
|
|
//-------------------------------------------------------------- |
|
|
Pstring::Pstring( char s[] ) |
//constructor for Pstring |
|
{ |
|
|
if(strlen(s) > SZ-1) |
//if too long, |
|
|
{ |
|
|
for(int j=0; j<SZ-1; j++) |
//copy the first SZ-1 |
|
str[j] = s[j]; |
//characters “by hand” |
|
str[j] = ‘\0’; |
//add the null character |
|
} |
|
else |
//not too long, |
|
|
String(s); |
//so construct normally |
} |
|
|
////////////////////////////////////////////////////////////////
int main() |
|
|
|
{ |
|
//define |
String |
Pstring |
s1 = “This is a very long string |
which is |
probably “ |
|
“no, certainly--going to exceed the limit set by SZ.”; |
||
cout << |
“\ns1=”; s1.display(); |
//display String |
|
Pstring |
s2 = “This is a short string.”; |
//define |
String |
cout << |
“\ns2=”; s2.display(); |
//display String |
|
cout << |
endl; |
|
|
return 0;
}


Appendix G
948
//--------------------------------------------------------------
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();
}
};


Appendix G
950
17.a, d
18.void func(char*);
19.
for(int j=0; j<80; j++) *s2++ = *s1++;
20.b
21.char* revstr(char*);
22.char* numptrs[] = { “One”, “Two”, “Three” };
23.a, c
24.wasted
25.memory that is no longer needed
26.p->exclu();
27.objarr[7].exclu();
28.a, c
29.float* arr[8];
30.b
31.0..9 at one end; 3..* at the other
32.b
33.false
34.a
Solutions to Exercises
1.
//ex10_1.cpp
//finds average of numbers typed by user #include <iostream>
using namespace std;
int main() |
|
{ |
|
float flarr[100]; |
//array for numbers |
char ch; |
//user decision |
int num = 0; |
//counts numbers input |
do |
|
{ |
|


Appendix G
952
ptrch++; |
//move to next character |
} |
|
}
////////////////////////////////////////////////////////////////
int main()
{
String s1 = “He who laughs last laughs best.”;
cout << “\ns1=”; |
//display string |
s1.display(); |
|
s1.upit(); |
//uppercase string |
cout << “\ns1=”; |
//display string |
s1.display(); |
|
cout << endl; |
|
return 0; |
|
} |
|
3.
//ex10_3.cpp
//sort an array of pointers to strings #include <iostream>
#include <cstring> |
//for strcmp(), etc. |
|
using |
namespace std; |
|
const |
int DAYS = 7; |
//number of pointers in array |
//prototype
//array of pointers to char char* arrptrs[DAYS] = { “Sunday”, “Monday”, “Tuesday”,
“Wednesday”, “Thursday”, “Friday”, “Saturday” };
cout << |
“\nUnsorted:\n”; |
|
for(int |
j=0; j<DAYS; j++) |
//display unsorted strings |
cout |
<< *(arrptrs+j) << endl; |
|
bsort(arrptrs, DAYS); |
//sort the strings |
|
cout << |
“\nSorted:\n”; |
|
for(j=0; j<DAYS; j++) |
//display sorted strings |
|
cout |
<< *(arrptrs+j) << endl; |
|
return 0; |
|
|
} |
|
|
//-------------------------------------------------------------- |
|
|
void bsort(char** pp, int n) |
//sort pointers to strings |
|
{ |
|
|


954 |
Appendix G |
|
newlink->next = first; |
//it points to next link |
first = newlink; |
//now first points to this |
} |
|
//-------------------------------------------------------------- |
|
void linklist::display() |
//display all links |
{ |
|
link* current = first; |
//set ptr to first link |
while( current != NULL ) |
//quit on last link |
{ |
|
cout << endl << current->data; |
//print data |
current = current->next; |
//move to next link |
} |
|
} |
|
//-------------------------------------------------------------- |
|
linklist::~linklist() |
//destructor |
{ |
|
link* current = first; |
//set ptr to first link |
while( current != NULL ) |
//quit on last link |
{ |
|
link* temp = current; |
//save ptr to this link |
current = current->next; |
//get ptr to next link |
delete temp; |
//delete this link |
} |
|
} |
|
////////////////////////////////////////////////////////////////
int main() |
|
{ |
|
linklist li; |
//make linked list |
li.additem(25); |
//add four items to list |
li.additem(36); |
|
li.additem(49); |
|
li.additem(64); |
|
li.display(); |
//display entire list |
cout << endl; |
|
return 0; |
|
} |
|
Chapter 11
Answers to Questions
1.d
2.true