Figure 14.1

#include <iostream>
using namespace std;
int main()
{
   int i = 11,
   j = 22,
   k = 33;
   int* iPtr = &i;
    nt* jPtr = &j;
   int* kPtr = &k;
   cout << "\nAt address " << iPtr
   << ", the value " << *iPtr << " is stored.\n"
   << "\nAt address " << jPtr
   << ", the value " << *jPtr << " is stored.\n"
   << "\nAt address " << kPtr
   << ", the value " << *kPtr << " is stored.\n";
}
------
Sample run:

At address 0x0053AD78, the value 11 is stored.

At address 0x0053AD7C, the value 22 is stored.

At address 0x0053AD80, the value 33 is stored.

 

LinkedList Class

#ifndef LINKEDLIST
#define LINKEDLIST

template <typename DataType>
class LinkedList
{
   private:
	/*** Node class ***/
	class Node
	{
	  public:
		// Node’s operations
		// Node’s instance variables
	   DataType data;
	   Node * next;
	};

   typedef Node* NodePointer;
   public:
	// LinkedList’s methods
	. . .
   private:
	// LinkedList’s instance variables
  	. . .
};
. . .
#endif

 

Figure 14.2

#include <cassert>//assert
#include <string>//string
#include <iostream>//cin,cout,>>,<<
#include <iomanip>//setw()
#include <fstream>//ifstream,is_open()
#include <list>//list<T>
#include <algorithm>//find
using namespace std;

//---------------Begin class AddressItem -------------------------
class AddressCounter
{
   public:
   void read(istream &in){in >>address;count =1;}
   void print(ostream &out)const
 	{out <<setw(15)<<left <<address
	     <<"\ t occurs "<<count <<"times \n";}
   void tally(){count++;}
   friend bool operator==(const AddressCounter&addr1,
   const AddressCounter&addr2);

   private:
   string address;
   int count;
};

inline bool operator==(const AddressCounter&addr1,
		       const AddressCounter&addr2)
{return addr1.address ==addr2.address;}

//-----------------End class AddressCounter --------------------
typedef list<AddressCounter>TCP_IP_List;

int main()
{
   string fileName;				//file of TCP/IP addresses
   TCP_IP_List addrCntList;			//list of addresses
   ifstream inStream;				//open file of addresses
   cout <<"Enter name of file containing TCP/IP addresses:";
   cin >>fileName;
   inStream.open(fileName.data());
   assert(inStream.is_open());
   AddressCounter item;				//one address &its count
   for (;;)//input loop:
	{
	item.read(inStream);			//read an address
	if (inStream.eof())break;		//if eof,quit
	TCP_IP_List::iterator it =		//is item in list?
	find(addrCntList.begin(),addrCntList.end(),item);
	if (it !=addrCntList.end())		//if so:
	   (*it).tally();			//++its count
	else 					//otherwise
	   addrCntList.push_back(item);		//add it to the list
   }//end loop
   cout <<"\nAddresses and Counts:\n \n";		//output the list
   for (TCP_IP_List::iterator it =addrCntList.begin();
                                it !=addrCntList.end();it++)
	(*it).print(cout);
}
--------------------------
Listing of file ipAddresses.dat used in sample run:

128.159.4.20
123.111.222.333
100.1.4.31
34.56.78.90
120.120.120.120
128.159.4.20
123.111.222.333
123.111.222.333
77.66.55.44
100.1.4.31
123.111.222.333
128.159.4.20
--------------------------
Sample run:

Enter name of file containing TCP/IP addresses:ipAddresses.dat
Addresses and Counts:
128.159.4.20 occurs 2 times
123.111.222.333 occurs 3 times
100.1.4.31 occurs 1 times
34.56.78.90 occurs 0 times
120.120.120.120 occurs 0 times
77.66.55.44 occurs 0 times
 

 

 

Figure 14.3

#include <iostream>
using namespace std;

int main(int argc,char*argv [])
{
	cout <<"\nThere are "<<argc
	     <<"strings on the command line:\n";
	for (int i =0;i <argc;i++)
		cout <<'\t'<<"argv ["<<i <<"] contains:"
		     <<argv [i ] <<endl;
}
--------------------

Sample Run

If we execute commandline by entering the command

$commandLine I want an argument   then the output will be

There are 5 strings on the command line:
argv [0 ]contains:commandLine
argv [1 ]contains:I
argv [2 ]contains:want
argv [3 ]contains:an
argv [4 ]contains:argument

 

 

Figure 14.4

#include <iostream>//cin,cout,<<,>>
#include <cmath>//sqrt()
#include <cstdlib>//strtod()
using namespace std;
int main(int argc,char*argv [])
{
   if (argc <2)
   {
	cout <<"\n***Usage:sroot List-of-Positive-Numbers \n \n";
	return 1;
   }
   double inValue;		//double equivalent of an argument
   for (int i =1;i <argc;i++)
   {
	inValue =strtod(argv [i ],0);
	if (inValue >=0)
	   cout <<"\n-->The square root of "<<inValue
		<<"is "<<sqrt(inValue)<<endl;
	else
	   cout <<"\n***"<<argv [i ] <<"is not a valid data item;"
		<<"\n***must be numeric and positive.\n"
		<<endl;
   }
}
----------------------------
Sample runs:

$sroot
***Usage:sroot List-of-Positive-Numbers

$sroot 4
-->The square root of 4 is 2

$sroot 4 ABC 7 9
-->The square root of 4 is 2

***ABC is not a valid data item;
***must be numeric and positive.
-->The square root of 7 is 2.64575
-->The square root of 9 is 3
 

 

 

Figure 14.5

#ifndef BIRD
#define BIRD
#include <string>
using namespace std;

class Bird
{
   public:
   Bird(const string&kind);
   string getKind()const;
   virtual string getMovement()const =0;
   virtual string getCall()const =0;

   private:
   string myKind;
};

inline Bird::Bird(const string&kind){myKind =ind;}
inline string Bird::getKind()const {return myKind;}

#endif

 

 

Figure 14.6

#ifndef FLYING_BIRD
#define FLYING_BIRD
#include "Bird.h"

class FlyingBird :public Bird
{
   public:
   FlyingBird(const string&kind);
   virtual string getMovement()const;
};

inline FlyingBird::FlyingBird(const string&ind):Bird(ind){}

inline string FlyingBird::getMovement()const {return "flew";}

#endif

 

 

Figure 14.7

#ifndef WALKING_BIRD
#define WALKING_BIRD
#include "Bird.h"

class WalkingBird :public Bird
{
   public:
   WalkingBird(const string&kind);
   virtual string getMovement()const;
};

inline WalkingBird::WalkingBird(const string&ind):Bird(kind){}

inline string WalkingBird::getMovement()const {return "walked";}

#endif

 

 

Figure 14.8

#ifndef HAWK
#define HAWK
#include "FlyingBird.h"

class Hawk :public FlyingBird
{
   public:
   Hawk();
   virtual string getCall()const;
};
inline Hawk::Hawk():FlyingBird("hawk"){}

inline string Hawk::getCall()const {return "ir-reeeee";}

#endif

 

 

Figure 14.9

#ifndef CHICKEN
#define CHICKEN
#include "WalkingBird.h"

class Chicken :public WalkingBird
{
   public:
   Chicken();
   virtual string getCall()const;
};

inline Chicken::Chicken():WalkingBird("chic en"){}

inline string Chicken::getCall()const {return "cluc -cluck";}

#endif

 

 

Figure 14.10

#ifndef DUCK
#define DUCK
#include "FlyingBird.h"

class Duck :public FlyingBird
{
   public:
   Duck();
   virtual string getCall()const;
};

inline Duck::Duck():FlyingBird("duck"){}

inline string Duck::getCall()const {return "quac -quac ";}

inline string Duck::getMovement()const
{
   RandomInt coin(0,1);
   return (coin ==0)?FlyingBird::getMovement():string("waddled");
}
#endif

 

 

Figure 14.11

#include "Chicken.h"
#include "Duck.h"
#include "GreenParrot.h"
#include "Hawk.h"
#include "Jabberwock.h"
#include "Peacock.h"
#include "RedParrot.h"
#include "RandomInt.h"
#include <iostream>
#include <vector>
using namespace std;
int main()
   {
   const int NUMBER_OF_BIRDS =7;
   vector<Bird*>birdVec(NUMBER_OF_BIRDS);
   birdVec [0 ] =new Chicken();
   birdVec [1 ] =new Duck()
   birdVec [2 ] =new GreenParrot();
   birdVec [3 ] =new Hawk();
   birdVec [4 ] =new Jabberwock();
   birdVec [5 ] =new Peacock();
   birdVec [6 ] =new RedParrot();
   cout <<"\nWelcome to the Bird Aviary!\n"
	<<"\nTo remain in the aviary,keep pressing 'Enter';\n"
	<<"enter any other character to leave:\n \n";
   int i;
   Bird*aBird;
   char response;
   RandomInt d7(0,NUMBER_OF_BIRDS-1);
   for (;;)
   {
	cin.get(response);
	if (response !='\n')break;
	i =d7.generate();
	aBird =birdVec [i ];
	cout <<"A "<<aBird->getKind()
	     <<"just "<<aBird->getMovement()
	     <<"by,calling \""<<aBird->getCall()
	     <<"\"...\n"<<endl;
   }
   for (i =0;i <NUMBER_OF_BIRDS;i++)
   delete birdVec [i ];
   cout <<"\nGood-bye,and come again!"<<endl;
}
 

 

 

Sample Run: Figure 14.12



Welcome to the Bird Aviary!
To remain in the aviary,keep pressing 'Enter';
enter any other character to leave:
.
A jabberwock just whiffled by,calling "burble-burble"...
.
A hawk just flew by,calling "kir-reeeee"...
.
A red parrot just flew by,calling "squawwwwwwww "...
.
A duck just waddled by,calling "quack-quac "...
.
A chicken just walked by,calling "cluck-cluc "...
.
A duck just flew by,calling "quack-quack"...
q .
Goodbye,and come again!