CS117 Programming Methodology
Fall 1999

 

Code Reading in C++

Instructions:

Examine the program on the following pages and answer these questions about the different tokens and constructs in the program listing. Hand your answers to me (RTH) before, or during class on Monday (Oct 18th).

 

1.        How many classes are declared?

2.        On what line does the main function start?

3.        On what line is the name push declared?

4.        Is the first mention of iceCream a declaration or a definition?

5.        How many local declarations and/or definitions does the main function contain?

6.        What is the initial value of the variable cp in the main function?

7.        On what lines (note the plural) is there a unary operator?

8.        On what lines is there a for loop?

9.        On what lines in function pop is there a conditional statement?

10.     On what lines is there an binary arithmetic operator?

11.     On what lines is a global function defined?

12.     On what line is the scope resolution operator LAST used?

13.     How many function members does class StringStack have?

14.     What are the file names of included header files?

 

 


 

1        /****************************

2        SSTACK.CC -- Enums inside classes

3        Roger Hartley, Spring 1997

4        ****************************/

 

5        #include <string.h>

6        #include <iostream.h>

 

7        class String {

8        private:

9          char *str;

10      int nchars;

11    public:

12      String() { nchars = 0; str = 0; }

13     

14      String(const char* s) {

15        nchars = strlen(s);

16        str = new char[nchars + 1];

17        strcpy(str, s);

18      }

19     

20      String(String& copy) {

21        nchars = strlen(copy.c_str());

22        str = new char[nchars + 1];

23        strcpy(str, copy.c_str());

24      }

25     

26      int length() { return nchars; }

27        const char* c_str() const { return str; }

28        int empty() const {

29        return str[0] == '\0';;

30      }

31    };

32     

33    const String iceCream[] = {

34      "pralines & cream",

35      "fudge ripple",

36      "jamocha almond fudge",

37      "wild mountain blackberry",

38      "lemon swirl",

39      "rocky road",

40      "deep chocolcate fudge",

41      ""

42    };

43     

44    ostream& operator<<(ostream& out, const String& s) {

45      out << s.c_str();

46      return out;

47    }

 

 

48    class StringStack {

49      enum { size = 100 };

50      String stack[size];

51      int index;

52    public:

53      StringStack();

54     

55      void push(const String& s);

56     

57      String pop();

58     

59      int itemCount() { return index; }

60     

61      int empty() { return index == 0; }

62    };

63     

64    StringStack::StringStack() {

65      index = 0;

66    }

67     

68    void StringStack::push(const String& s) {

69      if (index < size)

70        stack[index++] = s;

71    }

72     

73    String StringStack::pop() {

74      if (index > 0) {

75        String rv = stack[--index];

76        stack[index] = String();

77        return rv;

78      }

79      return String();

80    }

81     

82    int main() {

83      StringStack SS;

84      int numberStrs = 0;

85    // count the strings in iceCream

86      while (!iceCream[numberStrs].empty())

87        ++numberStrs;

88    // print them out while pushing them on the stack

89      for (int i = 0; i < numberStrs; i++) {

90        cout << iceCream[i] << endl;

91        SS.push(iceCream[i]);

92      }

93      cout << "Items = " << SS.itemCount() << endl;

94    // pop the stack and print again

95      const char* cp;

96      while (!SS.empty()) {

97        cp = SS.pop().c_str();

98        cout << cp << endl;

99      }

100}