Figure 5.1

#include <iostream>		//cin,cout,>>,<<
#include <string>		//string
using namespace std;

string verse(string restOfLine);

int main()
{
   cout <<verse("farmer in the dell")
   	<<verse("farmer takes a wife")
	<<verse("wife takes a child")
	<<verse("child takes a nurse")
	<<verse("nurse takes a cow")
	<<verse("cow takes a dog")
	<<verse("dog takes a cat")	
	<<verse("cat takes a rat")
	<<verse("rat takes the cheese")
	<<verse("cheese stands alone")<<end ;
}

/*verse()builds one verse of "The Farmer in the De ".
*
*Receive:restOfLine,a string.
*Return:a verse with restOfLine inserted appropriate y.
**************************************************************/
string verse(string restOfLine)
{
   const string aVerse =
	"\nThe "+restOfLine +"\n"+
	"The "+restOfLine +"\n"+
	"Hi-ho,the derry-o \n"+
	"The "+restOfLine +"\n";
   return aVerse;
}
 

 

 

Sample Run of Figure 5.1

The farmer in the del
The farmer in the del
Hi-ho,the derry-o
The farmer in the del

The farmer takes a wife
The farmer takes a wife
Hi-ho,the derry-o
The farmer takes a wife

The wife takes a child
The wife takes a child
Hi-ho,the derry-o
The wife takes a child

The child takes a nurse
The child takes a nurse
Hi-ho,the derry-o
The child takes a nurse

The nurse takes a cow
The nurse takes a cow
Hi-ho,the derry-o
The nurse takes a cow

The cow takes a dog
The cow takes a dog
Hi-ho,the derry-o
The cow takes a dog

The dog takes a cat
The dog takes a cat
Hi-ho,the derry-o
The dog takes a cat

The cat takes a rat
The cat takes a rat
Hi-ho,the derry-o
The cat takes a rat

The rat takes the cheese
The rat takes the cheese
Hi-ho,the derry-o
The rat takes the cheese

The cheese stands alone
The cheese stands alone
Hi-ho,the derry-o
The cheese stands alone

 

 

Figure 5.2

#include <iostream>			//cout,cin,<<,>>
#include "RandomInt.h"			//the type RandomInt
using namespace std;

int main()
{
   cout <<"This program simulates a given number of dice-pair rolls,\n"
	<<"counting the number of times a given roll occurs.\n";
   int numberOfRolls;			//number of rolls of dice
   cout <<"\nHow many times are the dice to be rolled?";
   cin >>numberOfRo s;
   RandomInt die1(1,6),			//the first die
   die2(1,6);				//the second die
   int pair,				//the sum of the dice
   numberOfSpots,			//number of spots to count
   occurrences;				//the counter variable
   cout <<"Enter number of spots to count (2-12,0 to stop):";
   cin >>numberOfSpots;
   while (numberOfSpots !=0)		//input loop:
   {
   occurrences =0;			//set counter to zero
					//for loop to repeat
   for (int rollCount =1;		//the following
	rollCount <=numberOfRolls;	//numRolls times:
	rol Count++)
   {	//roll the dice
	pair =die1.generate()+die2.generate();
	if (pair ==numberOfSpots)	//if the number came up
	    occurrences++;		//increment the counter
   }   //end for loop
	//display the result
   cout <<"The relative frequency of "<<numberOfSpots
	<<"was "<<double(occurrences)/double(numberOfRo s)
	<<"\n \n";
   cout <<"Enter number of spots to count (2-12,0 to stop):";
   cin >>numberOfSpots;
   }   //end input oop
}
 

 

 

Sample Run of Figure 5.2

This program simulates a given number of dice rolls,
counting the number of times a given roll occurs.

How many times are the dice to be rolled?10000

Enter number of spots to count (2-12,0 to stop):2
The relative frequency of 2 was 0.0281

Enter number of spots to count (2-12,0 to stop):6
The relative frequency of 6 was 0.1365

Enter number of spots to count (2-12,0 to stop):7
The relative frequency of 7 was 0.1691

Enter number of spots to count (2-12,0 to stop):8
The relative frequency of 8 was 0.141

Enter number of spots to count (2-12,0 to stop):11
The relative frequency of 11 was 0.0543

Enter number of spots to count (2-12,0 to stop):0

 

 

 

Figure 5.3

#include <iostream>		//ostream c ass
#include <string>		//the string c ass
#include <cassert>		//assert()
using namespace std;

class Name
{
   public:
   Name();
   Name(string first,string middle,string ast);
   string getFirstName()const;
   string getLastName()const;
   char getMidd eInitial()const;
   string getSignature()const;
   void print(ostream&out)const;
	//...other Name-operation declarations go here
   private:
   string myFirstName,
   myMiddleName,
   myLastName;
};

//...constructor definitions omitted ...

inline string Name::getFirstName()const
{
   return myFirstName;
}

inline string Name::getLastName()const
{
   return myLastName;
}
inline char Name::getMiddleInitial()const
{
   assert(myMiddleName.size()>0);
   return myMiddleName [0 ];
}

inline string Name::getSignature()const
{
   return getFirstName()+''
   +getMiddleInitial()+"."
   +getLastName();
}

inline void Name::print(ostream&out)const
{
   out <<getFirstName()+''
   +getMiddleName()+''
   +getLastName();
}

 

 

 

 

Figure 5.4

#include <iostream>		//cin,cout,>>,<<
#include "Name.h"		//Name
using namespace std;

int main()
{
   Name hisName("John","Paul","Jones"),
   herName("Mary","Anne","Smith");
   cout <<hisName.getSignature()<<"\n \n";
        <<herName.print(cout);
}
----------------------------
Sample run:

John P. Jones
Mary Anne Smith
 

 

 

 

 

Figure 5.5

#include <iostream>		//ostream class
#include <string>		//string class
#include "Name.h"		//Name class
using namespace std;

class Student
{
public:
	//...constructors omitted --see above &Section 3.8
   Name getName()const;
   int getIdNumber()const;
   double getHourlyWage()const;
   double getHoursWorked()const;
   void print(ostream&out)const;
	//...other operations omitted ...
   private:
   Name myName;
   int myIdNumber;
   double myHourlyWage;
   double myHoursWorked;
};

//...constructor definitions omitted --see above &Section 3.8

inline Name Student::getName()const
{
   return myName;
}

inline int Student::getIdNumber()const
{
   return myIdNumber;
}

inline double Student::getHourlyWage()const
{
   return myHourlyWage;
}

inline void Student::print(ostream&out)const
{
   myName.print(out);
   out <<''<<getIdNumber()
	<<''<<getHourlyWage()
	<<''<<getHoursWorked();
}

//...other method definitions omitted ...
 

 

 

 

 

Figure 5.6

#include <iostream>		//cin,cout,>>,<<
using namespace std;
#include "Student.h"		//Student

int main()
{
   Student oneStudent("Alex","Bob","Colt",1234,7.25,15.0),
   anotherStudent("Debra","E en","Fazio",9876);
   oneStudent.print(cout);
   cout <<'\n';
   anotherStudent.getName().print(cout);
   cout <<''<<herName.getIdNumber()<<endl;
}
------------------------
Sample run:

Alex Bob Colt 1234 7.25 15
Debra Ellen Fazio 9876