Chapter 2 - Programming exercise 1
File format : CH02_1.CPP,Save That.
#include <iostream.h>
enum game_result {WIN, LOSE, TIE, CANCEL, FORFEIT};
void main()
{
game_result result;
enum game_result omit = CANCEL;
int index;
for (index = WIN ; index <= FORFEIT ; index++) {
result = (game_result)index;
if (result == omit)
cout << "The game was cancelled\n";
else {
cout << "The game was played ";
if (result == WIN)
cout << "and we won!";
if (result == LOSE)
cout << "and we lost.";
if (result == FORFEIT)
cout << "and we won by forfeit.";
cout << "\n";
}
}
}
// Result of execution
//
// The game was played and we won!
// The game was played and we lost.
// The game was played
// The game was cancelled
// The game was played and we won by forfeit.
enum game_result {WIN, LOSE, TIE, CANCEL, FORFEIT};
void main()
{
game_result result;
enum game_result omit = CANCEL;
int index;
for (index = WIN ; index <= FORFEIT ; index++) {
result = (game_result)index;
if (result == omit)
cout << "The game was cancelled\n";
else {
cout << "The game was played ";
if (result == WIN)
cout << "and we won!";
if (result == LOSE)
cout << "and we lost.";
if (result == FORFEIT)
cout << "and we won by forfeit.";
cout << "\n";
}
}
}
// Result of execution
//
// The game was played and we won!
// The game was played and we lost.
// The game was played
// The game was cancelled
// The game was played and we won by forfeit.
Chapter 2 - Programming exercise 2
File format : CH02_A.CPP,Save That.
#include <iostream.h>
class animal {
public:
int weight;
int feet;
float height;
};
main()
{
animal dog1, dog2, chicken;
animal cat1;
class animal cat2;
dog1.weight = 15;
dog2.weight = 37;
chicken.weight = 3;
dog1.feet = 4;
dog2.feet = 4;
chicken.feet = 2;
dog1.height = 17.4;
dog2.height = 2.221;
chicken.height = 7.123;
cout << "The weight of dog1 is "<< dog1.weight << "\n";
cout << "The weight of dog2 is "<< dog2.weight << "\n";
cout << "The weight of chicken is "<< chicken.weight << "\n";
cout << "Dog1 height is "<< dog1.height << " inches.\n";
cout << "The chicken's height is "<< chicken.height
<< " inches.\n";
}
// Result of execution
//
// The weight of dog1 is 15
// The weight of dog2 is 37
// The weight of chicken is 3
// Dog1 height is 17.4 inches.
// The chicken's height is 7.123 inches.
class animal {
public:
int weight;
int feet;
float height;
};
main()
{
animal dog1, dog2, chicken;
animal cat1;
class animal cat2;
dog1.weight = 15;
dog2.weight = 37;
chicken.weight = 3;
dog1.feet = 4;
dog2.feet = 4;
chicken.feet = 2;
dog1.height = 17.4;
dog2.height = 2.221;
chicken.height = 7.123;
cout << "The weight of dog1 is "<< dog1.weight << "\n";
cout << "The weight of dog2 is "<< dog2.weight << "\n";
cout << "The weight of chicken is "<< chicken.weight << "\n";
cout << "Dog1 height is "<< dog1.height << " inches.\n";
cout << "The chicken's height is "<< chicken.height
<< " inches.\n";
}
// Result of execution
//
// The weight of dog1 is 15
// The weight of dog2 is 37
// The weight of chicken is 3
// Dog1 height is 17.4 inches.
// The chicken's height is 7.123 inches.
Chapter 2 - Programming exercise 2
File format : CH02_B.CPP,Save That.
#include <iostream.h>
class animal {
float height;
public:
int weight;
int feet;
};
main()
{
animal dog1, dog2, chicken;
animal cat1;
class animal cat2;
dog1.weight = 15;
dog2.weight = 37;
chicken.weight = 3;
dog1.feet = 4;
dog2.feet = 4;
chicken.feet = 2;
dog1.height = 17.4;
dog2.height = 2.221;
chicken.height = 7.123;
cout << "The weight of dog1 is "<< dog1.weight << "\n";
cout << "The weight of dog2 is "<< dog2.weight << "\n";
cout << "The weight of chicken is "<< chicken.weight << "\n";
cout << "Dog1 height is "<< dog1.height << " inches.\n";
cout << "The chicken's height is "<< chicken.height
<< " inches.\n";
}
class animal {
float height;
public:
int weight;
int feet;
};
main()
{
animal dog1, dog2, chicken;
animal cat1;
class animal cat2;
dog1.weight = 15;
dog2.weight = 37;
chicken.weight = 3;
dog1.feet = 4;
dog2.feet = 4;
chicken.feet = 2;
dog1.height = 17.4;
dog2.height = 2.221;
chicken.height = 7.123;
cout << "The weight of dog1 is "<< dog1.weight << "\n";
cout << "The weight of dog2 is "<< dog2.weight << "\n";
cout << "The weight of chicken is "<< chicken.weight << "\n";
cout << "Dog1 height is "<< dog1.height << " inches.\n";
cout << "The chicken's height is "<< chicken.height
<< " inches.\n";
}