[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng] / [R J Botting] / [CSci201] / classes
[Text Version] [Syllabus] [Schedule] [Glossary] [Labs] [Projects] [Resources] [Grading] [Contact] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Thu Mar 18 15:16:42 PDT 2010

Contents


    Classes

      Introduction

      Here are the things that you need to be able to do with classes:
      1. Know the words: class, object, class_name, constructor, member_function, data_field, UML, accessor, mutator, getter, setter. On quizzes and the CS201 final you will have to label code correctly with the above words.
      2. Work out what a simple program with Classes does.
      3. Imagine new classes of objects.
      4. Draw a picture of your classes using the Unified Modeling Language or UML. I will be handing out an introduction to the UML that has more than you will need in CS201.
      5. Write a class in C++ that does what you want.
      6. Read a diagram the uses the Unified Modeling Language to document some classes. I'll hand out an example of UML documenting Horstmann's CCC library.
      7. Given the UML for a class, write the C++ code.
      8. Given the C++ code, draw a rough UML diagram of it.

      A class is a description of a set of similar objects. Once a class is defined you can declare new objects that belong in that class. The class describes how the objects function. A class lists functions that can be applied to objects in that class. It lists the ways you construct new objects. It can even give special meanings for operators like + and -.

      Exercise on Real Objects and Real Classes

      In groups of 2 and then 4 write down the common or shared features of all your watches. The common features will be used to define the class Watch.

      Repeat for the class: Calculator.

      C++ Classes

      Programs contain objects, Objects contain data. Objects can do things. Each object's class describes what the object can do and the type of data stored inside the object. An object in C++ has an internal structure. An object is a piece of storage divided into sub-boxes. The class lists the sub-boxes (data fields) and gives them names. Each object can have different data values for its data_fields. All have the same data fields however.

      The functions listed in a class are allowed to manipulate the data inside the objects. Normally other functions are stopped from doing this. The member functions (those listed in a class) work with the data fields exactly as if they were variables. The class therefore declares data fields exactly as if they where variables.

      Template for a class:

       		class put_name_here
       		{
       		  public:
       			list member function prototypes here
       		  private:
       			declare data fields here
       		};
      Note: Do not forget the semicolon at the end of the class.

      Note: Do not forget the semicolon at the end of the class.

      Note: Do not forget the semicolon at the end of the class.

      Template for a member function:

       	type class_name::function_name( arguments )
        {
       		put behavior of function here
        }
      Note: Do not put semicolon at the end of a function.

      Note: Do not put semicolon at the end of a function.

      Glossary

    1. classes::=plural of class.
    2. class::=a collection of objects with similar behavior(member_functions, constructors) and internal data(data_field) with a class_name.
    3. class_name::=a singular noun phrase attached to a class or similar objects. For example in C++:
       		class Counter /*class name*/
       		{ public:
       		    Counter();/*constructor*/
       		    void count();/*mutator member function*/
       		    int value()const;/*accessor member function*/
       		  private:
       		    int state;/*data field*/
       		};
    4. UML::="Unified Modeling Language". The UML is used to draw pictures of classes:

      Counter class in UML

      The name is in the first compartment, the data fields are in the second compartment, and the operations/functions are put in the third part of the box. You can omit the 2nd and 3rd compartments if you want.

    5. objects::= plural of object.
    6. object::=an instance of a class which hides certain data inside it and has its functionality defined by its class. An object can be created by using a class_name as a type in a C++ declaration:
       		Counter the_count; /* the_count is a new object in class Counter*/
      The UML can also be used to draw a picture of an object. The name of the object and its class are in the top part of the box and are underlined. The values of the data in object are put in the second compartment. For example:

      Counter the_count in the UML

    7. constructors::=plural of constructor.
    8. constructor::=a way to create a new object that is an instance of a class with the same name as the class's name. A constructor with no arguments is called when objects are declared. It is called the default constructor:
       	Counter::Counter() { state=0; }

    9. member_functions::= plural of member_function.
    10. member_function::=A function listed inside a class that has access and change the private data inside an object of that class. A member_function is often listed in the definition of the class but fully defined separately using a double-colon symbol:
       void Counter::count() { state = state + 1; }
       int Counter::value()const { return state; }
      A member function is nearly always called by first naming an object and then the function. For example we can ask the_count to record a count:
       		the_count.count(); /* ask the_count to count something*/
      It executes the following (found in the class Counter):
       { the_count.state = the_count.state + 1; }
      And now it looks like this:

      Counter the_count after the_count.count()

      We can ask the_count what it's value is and output the result:

       		cout << the_count.value(); /* apply value() to the_count*/
      Object the_count is in class Counter so it executes this code:
       { return the_count.state; }
      Inside a member_functions definition, all calls to functions that are members of the same class refer to the same implicit object.

    11. const::=in a member function the reserved word "const" indicates that the function does not change the data fields in the object. In the class Counter the function value was like this. We therefore know that
       		the_count.value()
      does not change the count in any visible way. This means that it is an accessor function. If there is no const the function can change the object. It is a mutator.

    12. accessors::=plural of accessor.
    13. accessor::=a member function that does not change the object to which it is applied, see const.
    14. getter::=an accessor that returns the value of a private data field.
    15. mutators::=plural of mutator.
    16. mutator::=a member function that may change the object to which it is applied. Mutators are often void member functions, but are never const.
    17. setter::=a mutator that seems to change a single hidden data item. Also: see constructors above.

    18. data_fields::=plural of data_field.
    19. data_field::=A variable that is hidden inside an object, it must have the name and type declared in the object's class.
       		    int state;/*data field*/

      As a rule: keep all data private and minimize the number of mutators and accessors. Never forget:

    20. YAGNI::principle="You Ain't Gonna Need It". This principle suggests you don't add a function because you might need it, you only add it when you have a real immediate need for it. Need here means: you can give a client some value by using it.

      Example program testing a class

       		int main()
       		{
       		   for( Counter i; i.value() < 10; i.count() )
       		   {
       		      cout << i.value() << " " << i.value() * i.value() << "\n";
       		   }
       		}//end main

      Note. Shift click the following to download a program made up of the Counter classes, its functions and the main program: [ ex08b.cpp ]

      Can you predict what it will do before you compile and run it?

      Now download and try to predict this program -- that uses the same class but has two objects: [ ex08c.cpp ]

      Now download and try to predict this program -- that uses the same class but has two objects in a different way: [ ex08d.cpp ]

      If you want to see what this process looks like download and run the following Graphic CCC version: [ ex08e.cpp ]

    . . . . . . . . . ( end of section Classes) <<Contents | End>>

    Abbreviations

  1. Algorithm::=A precise description of a series of steps to attain a goal, [ Algorithm ] (Wikipedia).
  2. Class::=A description of a type of object that includes data it knows and the functions that can change it.
  3. Function::programming=A selfcontained and named piece of program that knows how to do something.
  4. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular and free C++ compiler.
  5. OOP::="Object-Oriented Programming", Current paradigm for programming.
  6. Semantics::=Rules determining the meaning of correct statements in a language.
  7. SP::="Structured Programming", a previous paradigm for programming.
  8. Syntax::=The rules determining the correctness and structure of statements in a language, grammar.
  9. Q::software="A program I wrote to make software easier to develop",
  10. TBA::="To Be Announced", something I should do.
  11. TBD::="To Be Done", something you have to do.
  12. UML::="Unified Modeling Language", industry standard design and documentation diagrams.
  13. void::C++Keyword="Indicates a function that has no return".

End