Code Reading

Here is your first assignment in code reading. Follow the steps below to complete the assignment in good fashion. Good luck!

1. Save this message and print the file so that you can do the assignment successfully. If you do not use the lineprinter, try to print the text with a fixed-width font. This is standard practice whenever program text is printed.

2. Examine the program text in section 4 and identify the syntactic categories in section 3 by writing (or typing) the appropriate fragment against the category name. Make sure the you write the whole of the fragment, including punctuation, if necessary. Where there is more than one possibilty you may choose any one. Hand your work to me (RTH) in class on Friday, February 2nd.

3. Identify these categories:

a. a variable definition

b. a function prototype

c. a parameter list

d. a return type

e. a compound statement

f. a while loop

g. an iteration section in a for loop header

h. a class declaration

i. an access control keyword

j. a binary arithmetic operator

k. an assignment statement

l. a test expression in an iteration

m. a single-line comment

n. a directive (a pre-processor directive)

o. an argument to a function call

p. a parameter

q. a function name

r. a punctuation token

s. a for statement

t. an if statement with else

4. The text itself.

/**************************************************
Code reading example. CS177 Spring 1996
Roger Hartley
**************************************************/

#include <iostream.h>
#define print(arg) cout << arg

const int GX = 10;
int add10(int);

class XClass {
private:
	int x;
public:
		XClass() : x(0) {}
	void printX();
	int getX() { return x; }
	XClass incX();
};


void XClass::printX() {
	print(add10(x));
}

XClass XClass::incX() {
	x += 2;
	return *this;
}

int add10(int i) {
        return i + 10;
}

int main() {
/* declare an XClass object and do various
things to its internal variable */
	XClass x1;

	while (x1.getX() != GX) {
                // print the value of x inside x1
		if (x1.getX() == 0)
			print("Zero!\n");
		else {
			x1.printX();
			cout << endl;
		}
		x1.incX();
	}

	for (int i = 0; i < 10; i++)
		print(x1.incX().getX());
}