#include <string>//string
#include <cctype>//islower(),toupper()
using namespace std;
//-------Input Method ----------------------------------
void Color::read(istream&in)
{
string colorString;//read string
in >>colorString;
for (int i =0;i <colorString.size();i++)//convert case
if (islower(colorString [i ]))//if needed
colorString [i ] =toupper(colorString [i ]);
if (colorString =="RED")//map to ColorValue
myColorValue =RED;
else if (colorString =="ORANGE")
myColorValue =ORANGE;
else if (colorString =="YELLOW")
myColorValue =YELLOW;
else if (colorString =="GREEN")
myColorValue =GREEN;
else if (colorString =="BLUE")
myColorValue =BLUE;
else if (colorString =="INDIGO")
myColorValue =INDIGO;
else if (colorString =="VIOLET")
myColorValue =VIOLET;
else
{
cerr <<"\n***read():invalid color-value received!\n ;
in.setstate(ios::failbit);
}
}
|
double wavelength(Color aColor)
{
switch(aColor)
{
case RED:
return 6.5E-7;
case ORANGE:
return 6.0E-7;
case YELLOW:
return 5.8E-7;
case GREEN:
return 5.2E-7;
case BLUE:
return 4.7E-7;
case INDIGO:
return 4.4E-7;
case VIOLET:
return 4.1E-7;
default:
cerr <<"\n***wavelength:invalid color received!\n";
return 0.0;
}
}
|
#include <string>//string #include <cctype>//islower(),toupper() using namespace std; istream&operator>>(istream&in,Color&aColor)
{
string colorString;
in >>colorString;
for (int i =0;i <colorString.size();i++)
if (islower(colorString [i ]))
colorString [i ] =toupper(colorString [i ]);
if (colorString =="RED")
aColor =RED;
else if (colorString =="ORANGE")
aColor =ORANGE;
else if (colorString =="YELLOW")
aColor =YELLOW;
else if (colorString =="GREEN")
aColor =GREEN;
else if (colorString =="BLUE")
aColor =BLUE;
else if (colorString =="INDIGO")
aColor =INDIGO;
else if (colorString =="VIOLET")
aColor =VIOLET;
else
in.setstate(ios::failbit);
return in;
}
|
ostream&operator<<(ostream&out,Color aColor)
{
switch(aColor)
{
case RED:
out <<"RED";
break;
case ORANGE:
out <<"ORANGE";
break;
case YELLOW:
out <<"YELLOW";
break;
case GREEN:
out <<"GREEN";
break;
case BLUE:
out <<"BLUE";
break;
case INDIGO:
out <<"INDIGO";
break;
case VIOLET:
out <<"VIOLET";
break;
default:
cerr <<"\n***operator<<:invalid color received!\n";
}
return out;
}
|
Color next(Color aColor)
{
switch (aColor)
{
case RED:
return ORANGE;
case ORANGE:
return YELLOW;
case YELLOW:
return GREEN;
case GREEN:
return BLUE;
case BLUE:
return INDIGO;
case INDIGO:
return VIOLET;
case VIOLET:
return COLOR_OVERFLOW;
default:
cerr << "\n*** next(): invalid color received!\n";
return COLOR_OVERFLOW;
}
}
|
#include <iostream> // istream, ostream
using namespace std;
// --- The type
enum Color {COLOR_UNDERFLOW = -1, // too-low error
RED, ORANGE, YELLOW, GREEN, // 0-3
BLUE, INDIGO, VIOLET, // 4-6
COLOR_OVERFLOW, // too-high error
NUMBER_OF_COLORS= 7};
// --- Its interface istream& operator>>(istream& in, Color& aColor); ostream& operator<<(ostream& out, Color aColor); Color next(Color aColor); Color previous(Color aColor); double frequency(Color aColor); double wavelength(Color aColor); // ... additional Color operations ... |
#include <cassert> // assert()
using namespace std;
inline Color::Color(ColorValue initialColorValue)
{
assert(RED <= initialColorValue && initialColorValue <= VIOLET);
myColorValue = initialColorValue;
}
|
#include "Color.h" // class Color
// ------- wavelength --------------------------------------------
double Color::wavelength() const
{
switch (myColorValue)
{
case RED:
return 6.5E-7;
case ORANGE:
return 6.0E-7;
case YELLOW:
return 5.8E-7;
case GREEN:
return 5.2E-7;
case BLUE:
return 4.7E-7;
case INDIGO:
return 4.4E-7;
case VIOLET:
return 4.1E-7;
}
}
|
#include <string> // string
#include <cctype> // islower(), toupper()
using namespace std;
// ------- Input Method ----------------------------------
void Color::read(istream& in)
{
string colorString; // read string
in >> colorString;
for (int i = 0; i < colorString.size(); i++) // convert case
if (islower(colorString[i])) // if needed
colorString[i] = toupper(colorString[i]);
if (colorString == "RED") // map to ColorValue
myColorValue = RED;
else if (colorString == "ORANGE")
myColorValue = ORANGE;
else if (colorString == "YELLOW")
myColorValue = YELLOW;
else if (colorString == "GREEN")
myColorValue = GREEN;
else if (colorString == "BLUE")
myColorValue = BLUE;
else if (colorString == "INDIGO")
myColorValue = INDIGO;
else if (colorString == "VIOLET")
myColorValue = VIOLET;
else
{
cerr << "\n*** read(): invalid color-value received!\n";
in.setstate(ios::failbit);
}
}
|
// -------- Equality ---------------------------------------------
inline bool operator==(const Color& left, const Color& right)
{
return left.myColorValue == right.myColorValue;
}
// -------- Less-than --------------------------------------------
inline bool operator<(const Color& left, const Color& right)
{
return left.myColorValue < right.myColorValue;
}
// -------- Less-than-or-equal -----------------------------------
inline bool operator<=(const Color& left, const Color& right)
{
return left.myColorValue <= right.myColorValue;
}
// ... remaining relational operators omitted
|
// -------- Prefix Increment --------------------------------------
Color Color::operator++()
{
switch(myColorValue)
{
case RED:
myColorValue = ORANGE;
break;
case ORANGE:
myColorValue = YELLOW;
break;
case YELLOW:
myColorValue = GREEN;
break;
case GREEN:
myColorValue = BLUE;
break;
case BLUE:
myColorValue = INDIGO;
break;
case VIOLET:
myColorValue = COLOR_OVERFLOW;
break;
}
return Color(myColorValue);
}
// -------- Postfix Increment -------------------------------------
Color Color::operator++(int)
{
Color myOriginalColor(myColorValue);
switch(myColorValue)
{
case RED:
myColorValue = ORANGE;
break;
case ORANGE:
myColorValue = YELLOW;
break;
case YELLOW:
myColorValue = GREEN;
break;
case GREEN:
myColorValue = BLUE;
break;
case BLUE:
myColorValue = INDIGO;
break;
case INDIGO:
myColorValue = VIOLET;
break;
case VIOLET:
myColorValue = COLOR_OVERFLOW;
break;
}
return myOriginalColor;
}
|
#ifndef COLOR
#define COLOR
#include <iostream> // istream, ostream
using namespace std;
enum ColorValue {COLOR_UNDERFLOW = -1,
RED, ORANGE, YELLOW, GREEN,
BLUE, INDIGO, VIOLET,
COLOR_OVERFLOW,
NUMBER_OF_COLORS = 7};
class Color
{
public:
Color(); // constructors
Color(ColorValue initialColorValue);
double wavelength() const; // properties
double frequency() const;
void read(istream & in); // I/O
void print(ostream & out) const;
// relational ops
friend bool operator==(const Color & left, const Color & right);
friend bool operator!=(const Color & left, const Color & right);
friend bool operator<(const Color & left, const Color & right);
friend bool operator>(const Color & left, const Color & right);
friend bool operator<=(const Color & left, const Color & right);
friend bool operator>=(const Color & left, const Color & right);
Color operator++(); // prefix ++
Color operator++(int); // postfix ++
Color operator--(); // prefix --
Color operator--(int); // postfix --
private:
ColorValue myColorValue;
};
// ... inline definitions go here ...
#endif
|
#include <iostream> // cin, cout
using namespace std;
#include "Color.h" // Color
int main()
{
cout << "\nTo compute the wavelength of a given "
<< "color,\nenter a color (e.g., RED): ";
Color theColor;
cin >> theColor;
double lambda = theColor.wavelength();
cout << "\nThe corresponding wavelength is " << lambda << endl;
}
|
#include <iostream> // cin, cout
using namespace std;
#include "Rock.h" // class Rock
int main()
{
cout << "This program provides information about "
<< "specific rocks.\n";
Rock aRock; // input variable
char response; // query response
do
{
cout << "\nEnter the name of a rock: ";
cin >> aRock;
if ( cin.good() )
cout << endl << aRock
<< " is classified as a(n) " << aRock.kind()
<< " rock, and\n its texture is " << aRock.texture()
<< endl;
else
{
cerr << "That kind of rock is not supported." << endl;
cin.clear();
}
cout << "\nEnter 'c' to continue, anything else to quit: ";
cin >> response;
}
while (response == 'c');
}
----------------------------
Sample run:
This program provides information about specific rocks.
Enter the name of a rock: sandstone
Sandstone is classified as a(n) SEDIMENTARY rock, and
its texture is COARSE.
Enter 'c' to continue, anything else to quit: c
Enter the name of a rock: obsidian
Obsidian is classified as a(n) IGNEOUS rock, and
its texture is FINE.
Enter 'c' to continue, anything else to quit: q
|
#ifndef ROCK
#define ROCK
#include <iostream> // istream, ostream
using namespace std;
#include "RockKind.h" // class RockKind
#include "RockTexture.h" // class RockTexture
// Add a declaration of an enumeration RockName here; see the
// opening documentation for the rock names to use.
class Rock
{
public: // constructors:
Rock(); // default value
Rock(RockName initialRock); // explicit value
void read(istream & in); // input
void print(ostream & out) const; // output
RockKind kind() const; // igneous, ...
RockTexture texture() const; // coarse, ...
// Add prototypes for both forms of ++ and -- as well as for
// the six relational operators: <, >, ==, <=, >=, !=
private:
RockName myRockName;
};
// ---- Put definitions of the constructors,
// ---- the operators <<, >>,
// ---- and the relational operators here.
#endif
|
#ifndef ROCK_KIND
#define ROCK_KIND
enum RockKindName {KIND_UNDERFLOW = -1,
IGNEOUS, METAMORPHIC, SEDIMENTARY,
NUMBER_OF_KINDS,
KIND_OVERFLOW = NUMBER_OF_KINDS};
#include <iostream> // istream, ostream
using namespace std;
class RockKind
{
public: // constructors:
RockKind(); // default value
RockKind(RockKindName initialRock); // explicit value
// --- Add prototypes for print(), read(), ++ and -- (both forms),
// --- and the six relational operators here.
private:
RockKindName myRockKindName;
};
// ---- Put definitions of the constructors,
// ---- the operators <<, >>,
// ---- and the relational operators here.
#endif
|
#include <iostream> // cin, cout
#include <iomanip> // setw()
using namespace std;
#include "Color.h" // Color hierarchy
int main()
{
cout << "\nColor Wavelength Frequency\n"
<< "========================================\n";
Color* colorHandle = new Red();
Color* saveHandle = 0;
while (colorHandle != 0)
{
cout << left << setw(10) << colorHandle
<< right << setw(10) << colorHandle->wavelength()
<< setw(20) << colorHandle->frequency() << endl;
saveHandle = colorHandle;
colorHandle = colorHandle->next();
delete saveHandle;
}
}
--------------------------------
Sample Run:
Color Wavelength Frequency
========================================
Red 6.5e-07 4.61231e+14
Orange 6e-07 4.99667e+14
Yellow 5.8e-07 5.16897e+14
Green 5.2e-07 5.76538e+14
Blue 4.7e-07 6.37872e+14
Indigo 4.4e-07 6.81364e+14
Violet 4.1e-07 7.3122e +14
|
#ifndef COLORS
#define COLORS
#include <iostream> // cin, cout
#include <string>
using namespace std;
class Color
{
public:
Color(const string& colorName);
string asString() const;
static Color* fromString(const string& name);
virtual double wavelength() const = 0;
double frequency() const;
virtual Color* next() const = 0;
// previous() left as an exercise
friend bool operator==(const Color& left, const Color& right);
friend bool operator!=(const Color& left, const Color& right);
friend bool operator<=(const Color& left, const Color& right);
friend bool operator>=(const Color& left, const Color& right);
friend bool operator< (const Color& left, const Color& right);
friend bool operator> (const Color& left, const Color& right);
private:
string myName;
};
class Red : public Color
{
public:
Red();
virtual double wavelength() const;
virtual Color* next() const;
// previous() left as an exercise
};
class Orange : public Color
{
public:
Orange();
virtual double wavelength() const;
virtual Color* next() const;
// previous() left as an exercise
};
// ... Classes Yellow, Green, Blue, & Indigo omitted ...
class Violet : public Color
{
public:
Violet();
virtual double wavelength() const;
virtual Color* next() const;
// previous() left as an exercise
};
//... inline function definitions for Color and subclasses go here
#endif
|
#ifndef COLORS #define COLORS // ... Declarations of the Color class and subclasses go here ... // ***** Handle operations ************************************* istream& operator>>(istream& in, Color*& colorPtr); ostream& operator<<(ostream& out, const Color*& colorPtr); // ***** Color operations *************************************
inline Color::Color(const string& name) { myName = name; }
inline string Color::asString() const { return myName; }
const SPEED_OF_LIGHT = 2.998e8;
inline double Color::frequency() const
{ return SPEED_OF_LIGHT / wavelength(); }
inline bool operator==(const Color& left, const Color& right)
{ return left.asString() == right.asString(); }
// ... other inline Color methods go here ...
// ***** Red operations *************************************
inline Red::Red() : Color("Red") {}
inline double Red::wavelength() const { return 6.5e-7; }
inline Color* Red::next() const { return new Orange(); }
// ***** Orange operations *************************************
inline Orange::Orange() : Color("Orange") {}
inline double Orange::wavelength() const { return 6.0e-7; }
inline Color* Orange::next() const { return new Yellow(); }
// ... Yellow, Green, Blue operations go here ...
// ***** Indigo operations *************************************
inline Indigo::Indigo() : Color("Indigo") {}
inline double Indigo::wavelength() const { return 4.4e-7; }
inline Color* Indigo::next() const { return new Violet(); }
// ***** Violet operations *************************************
inline Violet::Violet() : Color("Violet") {}
inline double Violet::wavelength() const { return 4.1e-7; }
inline Color* Violet::next() const { return 0; }
#endif
|