[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng Dept] / [R J Botting] / [CSci201] / 18
[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]
Podcasts: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12]
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Fri Mar 7 14:51:58 PST 2008

Contents


    Chapter 8: pages 253-272 More on Classes

      Reading

        Introduction

        We finish with some of the clever things that you can do with classes and objects in C++. C++ provides some the most powerful features you could wish for. And quite a few of them can be useful.

        8.1 Constant Objects

        Variable that are declared with a const can not change. So if you have a const object you can not mutate it. The compiler is very careful to make your code fulfill its promise to not change something that is supposed to be constant.

        You can declare that a member function does not change its object by using the word const in the declaration or definition:

         		type name (parameters) const { body }
         		type name (parameters) const ;
        The result is called an accessor. In the UML this is called a query.... and in DIA they use the C++ notation not the on in the UML standard.

        8.2 The pointer this

        8.3 Friends

        8.4 Operators

          Intro

          8.4.1 Binary operators

          8.4.2 Predefined Comparison operators

          Let the C++ library <utility> do the boring part of defining relations. Once you have defined operator< and operator!= this library will give you a complete list. And don't forget the magic incantation
           		using namespace std::rel_ops;
          to make all the comparison operators work.

          8.4.3 Unary Operators

          Notice that you can define two different forms of operators like '++'. I won't be asking you any questions about this in quizzes, labs, or the final.

          8.4.4 Assignment operator

          These take great care to program correctly -- leave until CSCI202!

          8.4.5 The Indexing operator

          How to make your own array work just like the real things!

          Notice -- sometimes we need two versions. One with const and one without.

        . . . . . . . . . ( end of section 8.4 Operators) <<Contents | End>>

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

      Glossary

    1. const::lexeme, indicates that something is not allowed to change.

    2. this::lexeme, a word that, in a class, is a pointer to the object that the function has been sent to.

    3. operator::lexeme, a word that is written before a symbol for an operator to make it behave like a function.

      Syntax

      Questions

        Can we practice for the last quiz

        I drew class diagram, using the UML, piece by piece, and the class translated it into C++. In the quiz the C++ code will have blanks and there will be a UML diagram designed to help you fill them in.

        Which arrow in Dia is used in a class diagram

        It should be the open arrow head: ---->

        Why are the type names in Dia backwards to C++

        UML comes from the Pascal->Ada tradition and Dia follows that tradition. C++ comes form the FORTRAN->Algol->CPL->C->C++ tradition.

        How many bits in a binary number

        It depends on the compiler and the CPU. Write a program that has
         		cout << 8*sizeof(int) <<endl;
        to find out the answer.

        What is the difference between public and private

        These are two key words used in C++ class declarations. Variables and functions declared after a private can not be accessed or used by another class, function, or the main program. Those that are public can be accessed and used.

        In the UML a private member is marked with a minus and a public one with a plus sign.

        The tradition is to make all data private and most functions public. You only allow other classes, functions, and the main program to use the private data in public ways. This makes it harder for programs to do something unexpected.

        The only way to change or get the value of a private int is to use a prewritten public operator/function to do it.

        What are the friends of a class

        A class may declare another class to be a friend. It can also declare a function to be a friend.

        What do friends do

        The have access to the private data and functions of a class.

        Avoid them.

        What does this do

        The keyword this in a member function in a class is a pointer variable with a predefined value. It points at the current object.

        It's main purpose in C++ is to let you send it to another object:

         		somebody.pleaseCallMeBack(this);
        It is like sending your phone number so people know where to contact you.

        What does the double colon do

        It separates a class name from the name of a data field or function in that class. It makes a complex name out of simple ones. You can use it to make one part of a program refer to a part of a different class -- as long as that is public.

        Give an example of a constant object

        Constant numbers like π are obviously useful. It is rare to need a constant object for a class.

        However it is very wise to pass parameters that refer to objects as constant references. That way the function can see the value of the object but not change it.

        Why do we have to define our own comparison operators for an Array

        C++ doesn't define one as standard -- so when we have a class of objects that we want to compare then we have to tell C++ how we want it done.

        What is an assignment operator

        The main assignment operator is '='. It is used like this
        		variable = expression;
        However there are half-a-dozen others: +=, -=, *=, /=, <<=, >>=, ...

        For example

         		variable += expression;
        means
         		variable = variable + expression;

        What are unary operators

        The following symbols can be used on numbers as unary operators: - ++ -- ! not * ~.

        You can make them work, with the same syntax, on any class of object, if you need to, by defining operations like: operator !.

        Details are in the text book and are not part of this class.

      .Next -- Quiz9 Lab10 Project8 Project: [ projects.html#P8 ]

      Quiz 9 will be all about the relationship between UML and C++ code.

      Lab10 TBA

    Abreviations

  1. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular C++ compiler.
  2. KDE::="Kommon Desktop Environment".
  3. OOP::="Object-Oriented Programming", Current paradigm for programming.
  4. SP::="Structured Programming", Previous paradigm for programming.
  5. TBA::="To Be Announced", something I should do.
  6. TBD::="To Be Done", something you have to do.
  7. UML::="Unified Modeling Language", [ 15.html ] (class notes on the UML and OOP).

End