Skip to main contentCal State San Bernardino / [CNS] / [Comp Sci Dept] / [R J Botting] >> [CSci202] >> 06
[Index] [Schedule] [Syllabi] [Text] [Labs] [Projects] [Resources] [Search] [Contact] [Grading]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Mon Apr 30 17:00:40 PDT 2007

Contents


    CSci202 Computer Science II, Session 06, More about C++ classes


      (Previous): C++ Classes [ 05.html ]

      Preparation

      Study this page and Chapter 8 but not 8.6 (pointers to member functions) and write down the questions, doubts, and surprises that you have on a piece of paper. Include your printed name!

      Assigned Work Due

      Hand in your questions, doubts and surprises.

      Project 2 is due in.

      You can resubmit Project 1 if the previous grade was B or less.

      Topics

        const Objects and member functions

      1. In UML mark the operation with the stereotype "<<query>>" or (Dia) "const"

        this

        Friend functions and classes

      2. In UML mark the friend functions with stereotype "<<friend>>"

        Do it yourself operators

          Binary operators

          Member functions

          Assignment

          Friend functions

          Comparison operators and <utility>

          Unary operators

          Simple

          Prefix

          Postfix

          Indexing[]

          Function call()

          Type Conversion operations

        Classwide or static members

      TBA

      Demo a class with const, friend, operators

      Complete a given simple efficient 8 character buffer class for secure input/output.

      Previously you met a very insecure 8 character buffer in [ lab01.html ] because it allowed stupid programmers to do stupid things ... like over-running the end of the buffer.

      One way to fix this is to hide the dangerous data in a class and provide just those functions that do what is needed and can't be abused.

      UML: class Buffy in UML

      Here is the test program: [ testBuffy.cpp ]

      Here is the "Header file": [ Buffy.h ]

      Demo Splitting a header file from a body file and using make

      The above was done is the simplest way -- very amateurish. Now to redo it using professional techniques: we separate the detailed code of the functions from the header file and precompile them. Then we use a program called make to assemble the program for use.

      [ testBuff2.cpp ] [ Buff2.h ] [ Buff2.cpp ] [ Makefile ] [ Make in resources ]

      Glossary

    1. stereotype::=`a way to attach a specialized meaning to something in the UML by using guillemots or French quotation marks".

    2. guillemots::ASCII= "<<" | ">>", a good UML tool will insert the real symbols for you.

      Questions

        Can you explain type_id and dynamic_cast

        We will ignore type_id in CS202 except as a way of recognizing exceptions in meeting 09 and lab/04. It turns out that our C has a pretty weird conception of what the name of a type is. Try [ type_ids.cpp ] to see how it works.

        We will do a little dynamic_cases later.

        How does & work in function headers

        It is placed before the name of a parameter to set up call by reference. Without it the actual parameter is copied into the function as the initial value of the parameter -- and never comes out again!

        Call by reference means that the address of the variable in the call is passed to the function and all actions done to the parameter actually are done to the object. This is mainly used to allow data to flow in and out of the function through a parameter.

        Notice it is faster to use call be reference (&) with objects -- the object will have more bytes in it than an address. Copying bytes takes time.

        Also -- we used to get the same effect by using '*' and '&' but this needs a great deal of knowledge and care to work without bugs.

        When to use friends

        Any class can declare friend classes and friend functions.

        Use them as little as you can. They expose the object to abuse by the friends.

        The input and output functions (operator<< and operator>>) are the main functions that you MUST define as friends in a class in the course.

        Can you point to a data member in an object

        If the data is public you can. But it is wiser not too.

        What is * this

        When you apply a function to an object:
         		object.example(data)
        then, invisibly, the program does this
         		this = & object;
        before the function starts to execute. Thus the function knows where the object is.... and can use the information in several ways.

        A common way is to tell another object about 'this' object:

         			user_interface.addButtonListener(this);
        which means that when the user clicks a button 'this' will be called with a description of the event.

        There are other uses for this to be covered later. Most of these use

         		*this
        to refer to the current object -- say to send a message to itself!

        Can you have many classes in one program or file

        Yes. AN lots of functions can be used in one program.

        Examples of static data members

        First -- static members can change. A static variable can change its value at any time. The word "static" in programming is overloaded and abused. Variables that don't vary are said to be "const".

        Static data belongs to its class, not to the individual objects. It is shared by all the objects. Its value doesn't change when you change objects.

        Use it when the data is about the whole class not about one instance/object of the class.

        The Wikipedia [ Static_variable#For_class_variables ] has a couple of examples -- one in C# and a simpler one in C++ They define a class of Requests. Each Request is for a particular URL (place on the web), but the class, itself maintains a count of created Requests. This would be used to keep track of how many outstanding requests for web pages have been made, and whether there is a problem or not.

        Other examples is a class recording the enrollment in a section of a course. The size of the enrollment is a shard "static" number, each enrollment is for a specific student who is enrolled in a particular section.

        In fact, most academic examples of static class members are of counters!

        What is a binary operator

        A binary operator is something like "+" and "/" which is written between two operands:
         		1 + 2

        How to create random letters

         		int(rand()*26)+'a'

        When does const really matter

        when it stops a program changing my wages.

        Seriously: const protects data for abuse -- use it to control bugs.

      Exercises

      Based on chapter 7 and the Input above. Examples: take UML and code in C++, take given C++ and draw UML, ...

      Lab

      Same as Monday: [ lab03.html ] on Dia and the UML.

    . . . . . . . . . ( end of section CSci202 Computer Science II, Session 06, More about C++ classes) <<Contents | End>>
    (Next): Deriving classes and Inheritance [ 07.html ]

    Abreviations

  1. TBA::="To Be Announced", something I have to do.
  2. TBD::="To Be Done", something you have to do.
  3. Dia::="A free Open Source Diagramming tool for Linux, Windoze, etc. ".

End