ch3-Enum (Enumerations in C/C++)
Chapter_3 Exercise_3-15 | Exercise_3-16 |
Enum TCP1, p. 193 (enum.c, Enum.cpp)
enum.c download
#include <stdio.h> // for printf()
// Keeping track of shapes
enum ShapeType // type `enum ShapeType' defined in C
{
circle, square, rectangle // 0, 1, 2
}; // Must end with a semicolon like a struct
void printShape(enum ShapeType s);
void printShape(enum ShapeType);
// void printShape(ShapeType); // warning: variable `ShapeType' has no type
// void printShape(ShapeType s); // error: unknown type name `ShapeType'
typedef enum ShapeType ShapeType; // `ShapeType' becomes a type name
void printShape(ShapeType s);
void printShape(ShapeType);
int main()
{
enum ShapeType shape = circle; // ShapeType shape = circle;
// Activities here....
// circle++; // compile error: `circle' not lvalue
// circle = rectangle; // compile error: `circle' not lvalue
printf("circle: %d, shape: %d\n", circle, shape);
shape++; // OK in C, error in C++
shape = 1; // OK in C, error in C++
shape = square; // OK in C/C++
printf("square: %d, shape: %d\n", square, shape);
shape += 2; // OK in C, error in C++
shape = rectangle+1; // OK in C, error in C++
shape = rectangle; // OK in C/C++
printf("rectangle: %d, shape: %d\n", rectangle, shape);
shape--, shape++; // OK in C, error in C++
printf("shape - circle = %d\n", shape-circle);
printf("circle - rectangle = %d\n", circle-rectangle);
printf("shape + square = %d\n", shape+square);
// Now do something based on what the shape is:
printShape(0); // circle, printShape() accepts both ShapeType, int as arg
printShape(square); // square, argument ShapeType
printShape(shape); // rectangle, argument ShapeType
printShape(shape+1); // unknown shape, argument int
return 0;
}
void printShape(ShapeType shape)
{
switch(shape)
{
case circle:
printf("circle\n");
/* circle stuff */
break;
case square:
printf("square\n");
/* square stuff */
break;
case rectangle:
printf("rectangle\n");
/* rectangle stuff */
break;
default:
printf("unknown shape\n");
break;
}
}
/*
gcc enum.c -o enum
./enum
circle: 0, shape: 0
square: 1, shape: 1
rectangle: 2, shape: 2
shape - circle = 2
circle - rectangle = -2
shape + square = 3
circle
square
rectangle
unknown shape
*/
Note: See also ch2-enum on the blog Kernighan_and_Ritchie, Chapter_2, Sec. 2.3.
Enum.cpp download
#include <iostream>
using std::cout;
using std::endl;
// Keeping track of shapes
enum ShapeType // types `ShapeType', `enum ShapeType' defined in C++
{
circle, square, rectangle // 0, 1, 2
}; // Must end with a semicolon like a struct
void printShape(enum ShapeType s);
void printShape(enum ShapeType);
void printShape(ShapeType s);
void printShape(ShapeType);
void printShape(int); // overloading printShape()
int main()
{
enum ShapeType shape = circle; // ShapeType shape = circle;
// Activities here....
// circle++; // compile error
// circle = rectangle; // compile error: `circle' not lvalue
cout << "circle: " << circle << ", shape: " << shape << endl;
// shape++; // compile error
// shape = 1; // compile error
shape = square;
cout << "square: " << square << ", shape: " << shape << endl;
// shape += 2; // compile error
// shape = rectangle+1; // compile error
shape = rectangle;
cout << "rectangle: " << rectangle << ", shape: " << shape << endl;
// shape--; // compile error
cout << "shape - circle = " << shape-circle << endl;
cout << "circle - rectangle = " << circle-rectangle << endl;
cout << "shape + square = " << shape+square << endl;
// Now do something based on what the shape is:
printShape(0); // overloaded function, prints `0: circle'
printShape(square); // square
printShape(shape); // rectangle
printShape(shape+1); // overloaded function, prints `3: unknown shape'
return 0;
}
void printShape(ShapeType shape)
{
switch(shape)
{
case circle:
cout << "circle" << endl;
/* circle stuff */
break;
case square:
cout << "square" << endl;
/* square stuff */
break;
case rectangle:
cout << "rectangle" << endl;
/* rectangle stuff */
break;
default:
cout << "unknown shape" << endl;
break;
}
}
void printShape(int i)
{
switch(i)
{
case circle:
cout << i << ": circle" << endl;
/* circle stuff */
break;
case square:
cout << i << ": square" << endl;
/* square stuff */
break;
case rectangle:
cout << i << ": rectangle" << endl;
/* rectangle stuff */
break;
default:
cout << i << ": unknown shape" << endl;
break;
}
}
/*
g++ Enum.cpp -o Enum
./Enum
circle: 0, shape: 0
square: 1, shape: 1
rectangle: 2, shape: 2
shape - circle = 2
circle - rectangle = -2
shape + square = 3
0: circle
square
rectangle
3: unknown shape
*/
Chapter_3 Exercise_3-15 | BACK_TO_TOP | Exercise_3-16 |
Comments
Post a Comment