[cs13001] cs1b questions

Mikhail Nesterenko mikhail at cs.kent.edu
Tue May 10 19:01:15 EDT 2016


> Slide 14 on classes member constant:
> Isn't a constant member  simillar to any other member variable in terms of
> accessibility?


A constant declared inside a class is similar to a member variable and
a member function.

What I meant is that, unlike a global constant, the scope of a
constant declared inside a class is the class itself. Thus, turning a
global constant into an in-class constant shrinks its scope. Limiting
a variable scope is good programming style since smaller scope
decreases the possibility of name collision and makes the program
easier to understand.

Consider the two battleship labs we did. In the structures lab,
FLEET_SIZE and FIELD_SIZE were global constants with global scope. In
the classes lab, the equivalent fieldSize and fleetSize constants were
moved into Location and Fleet classes respectively. This made the
classes lab code more modular and easier to program and understand.

Thanks,
--
Mikhail



More information about the cs13001 mailing list