Understanding the concept of objects
#include
#include
#include
enum color {red, blue, white, green, yellow};
const double dummy = 0.0;
const color dummyc = white;
void show (color);
void point (char, double);
void colorpoint (char, double, color);
void main ()
{
point('O', dummy);
point('p', dummy);
point('x', 8.0);
point('p', dummy);
point('y', 7.0);
point('p', dummy);
point('x', 2.0);
point('p', dummy);
point('x', 3.0);
point('p', dummy);
point('o', dummy);
point('p', dummy);
colorpoint('o',dummy,dummyc);
colorpoint('p',dummy,dummyc);
colorpoint('c',dummy,blue);
colorpoint('p',dummy,dummyc);
colorpoint('x',2.0,dummyc);
colorpoint('p',dummy,dummyc);
}
void point (char message, double data)
{
static double x_coord = 0.0;
static double y_coord = 0.0;
static double distance = 0.0;
switch (message)
{
case 'X':
x_coord = x_coord + data;
break;
case 'Y':
y_coord = y_coord + data;
break;
case 'O':
x_coord = 0.0;
y_coord = 0.0;
break;
case 'P':
cout << "I am currently at x = " << x_coord << ", y = " << y_coord << endl;
cout << "My distance from the origin is " << distance << endl;
break;
default:
cout << "Unknown message to point" << endl;
exit(-1);
}
distance = sqrt (x_coord * x_coord + y_coord * y_coord);
return;
}
void colorpoint (char message, double data, color color)
{
static color col = white;
switch (message)
{
case 'x':
case 'y':
case 'o':
point(message,data);
break;
case 'c':
col = color;
break;
case 'p':
point('p',dummy);
cout << "Also my color is ";
show(col);
cout << endl;
break;
default:
cout << "Unknown message to colorPoint" << endl;
exit(-1);
}
return;
}
void show (Color col)
{
switch (col)
{
case red:
cout << "Red";
break;
case blue:
cout << "Blue";
break;
case white:
cout << "White";
break;
case green:
cout << "Green";
break;
case yellow:
cout << "Yellow";
break;
}
}
Telephone
#include
#include
#include
#include
typedef int bool;
const bool true = 1;
const bool false = 0;
void readLetter(char&);
int computecorrespondingdigit(char);
bool dooneletter();
void main ()
{
bool flag = false;
while (!flag)
{
flag = dooneletter();
}
}
int exitLetter (char c)
{
return ((c == 'Q') || (c == 'Z'));
}
bool dooneletter()
{
char l;
int i;
readLetter(l);
if (exitLetter(l))
{
cout << "Quit." << endl;
return(true);
}
else
{
i = computeCorrespondingDigit(l);
cout << "The letter " << l << " corresponds to " << i
<< " on the telephone" << endl;
return(false);
}
}
int computecorrespondingdigit (char l)
{
switch (l)
{
case 'a' :
case 'b' :
case 'c' :
return(2);
case 'd' :
case 'e' :
case 'f' :
return(3);
case 'g' :
case 'h' :
case 'i' :
return(4);
case 'j' :
case 'k' :
case 'l' :
return(5);
case 'm' :
case 'n' :
case 'o' :
return(6);
case 'p' :
case 'r' :
case 's' :
return(7);
case 't' :
case 'u' :
case 'v' :
return(8);
case 'w' :
case 'x' :
case 'y' :
return(9);
}
}
void readletter(char& c)
{
cout << "Enter a letter: ";
cin >> c;
while (cin && !isalpha(c))
{
cout << "You have not entered a letter. Try again: ";
cin >> c;
}
if (!cin)
{
cout << "Unrecoverable error... Exiting" << endl;
exit(-1);
}
c = toupper(c);
}
No comments:
Post a Comment