[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng Dept] / [R J Botting] / [CSci201] / 05
[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]
Wed Jan 28 14:34:44 PST 2009

Contents


    05 Objects and Classes

    Previous -- elements of C++

    [ 04.html ]

    Prepare

      3.1 Introduction

      For the last 10 years objects have been the preferred way to think about and write programs. An object is a piece of memory plus some operations to work on the data in that memory. An object "encapsulates knowledge" in two forms. First, it knows about its data. Second an object knows how to do things. For example we can create an object that counts for us. It will hold a single integer. we can program it so that it knows that the initial value is zero, and it knows how to add 1 to the count and how to tell us what the current count is.

      If we know how to use an object, we don't need to know how it does it. It is like a watch. We know how to read and set the time. We don't need to know how it works. We don't have to think about cogs and spring (or chips and batteries) to tell the time.

      The properties of an object (what data it will hold for instance) is defined by its class. So my wrist watch is in class "Watch". There can be any number of objects for any given class.

      As a rule in C++ you can say that if something is not a number then it is an object.

      Strings -- an example standard class of objects

      The book uses string in this chapter (Section 3.5 below). It is a very useful standard class. I will show you some uses in the laboratories. Each string contains a sequence of zero or more characters. Examples: words, names, dates, sentences, formulas.... They are numbered 0,1,2,3,4,....

      If you want to use strings in your program you need these two lines:

       #include <string>
       using std::string;

      You can then do the following things with a string objects called s, t, etc:
      Table
      ExampleEffect
      Commands
      string s = "Hello";Declare and initialize s
      cin >> s;Read the next word from input into s
      getline(cin,s);read the whole next line into s
      s="World of Pain";copy in a new value replacing the old s.
      s=t;copy t into s replacing the old s.
      Expressions
      string("example")Construct a new string containing the characters.
      s + tConcatenate s with string t
      s.length()find out how many characters in s
      s.substr(f, l)part of s starting at f and of length l.
      s.find(t)Place in s with the first t occurs.
      s == tTrue is s and t have the same characters in the same order.
      ...and many more TBA

      (Close Table)

      Can strings be used for arithmetic

      Not easily. You have to use some advance techniques to convert the characters into numbers beforehand.

      How many strings can you have

      As many as you want.

      3.2 Classes, Objects, Member Functions and Data Members

      Short definitions:
    1. class::="a description of the properties of a set of similar objects".
    2. object::="a collection of data with operations to change that data".
    3. member::="things described in a class: data, functions, ...".
    4. function::="a named description of an operation that may or may not return a value".

      Classes are static -- handled by the compiler. They can't do much until the program runs and they constructs objects.

      3.3 Overview of the Chapter Examples

      Note. The book grows the GradeBook class from a useless beginning.... but by the end of this course a GradeBook object is starting to look very useful.

      Also notice the style used for naming classes, functions, objects etc. The book does it right.

      Can we use a different style of name

      Not in this course! For example: please don't abbreviate names of functions or classes without a strong reason for doing so.

      When you move on to other things -- find out the style of the people you work with and fit in.

      What is a grade book

      It is a book that records all the grades in a course.

      3.4 Defining a Class with a Member Function

      Take note of the syntax for declaring classes:
       class Name
       {
       		...
       };
      Do not forget the semicolon above!!!

      A class says what is possible. Calls to its functions make possible things happen... to that particular object.

      Why is data private and functions public

      Becuase a class has to have public functions for it to be used, and the hidden data is then protected from abuse.

      Why must we create objects before we can call member functions

      Becuase member functions operate on the data inside an object. ANd if there is no object, then there is no data to work with.

      Drawing UML Diagrams

      We have a tool called Dia in our lab machines that is quite good at doing diagrams, including the UML. It is free software and works on MS Windows as well. In the lab, in a terminal window, just type:
       	dia &
      (don't forget the "&"!) and a working window and a tool box will pop up. Have fun....

      However -- you can draw very useful diagrams by hand. I will give examples in class of the kind of rough diagrams I use. I'll expect you to draw UML diagram in quizzes and exams.

      Do you have a handout on the UML

      [ uml.html ]

      Will we use the UML

      Yes. In labs, quizzes, and the final.

      They may also help you in your project.

      3.5 Defining a Member Function with a Parameter

      Notice the slightly different notation for declaring a member function
       			void name (parameters)
       			{
       				...
       			}
      Exercise: List all the differences between the syntax for declaring a function and the syntax for declaring a class above.

      3.6 Data Members, set Functions and get Functions

      All vital information...

      Notice that you always need to test a class by using a main function.

      Hint: it is not necessary to have a "set" and "get" for every attribute (data member). True objects don't expose their secrets easily.

      How does a set function differ from a get function

      A set function or setter
    5. uses its given parameters to compute new values for the data in an object. A get function or getter takes the data inside the object and computers a value that is returned to the statement that calls it.

      Note: older functions tend to not use the words "set" and "get" so you have to look at the definitions...

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

    Deliver -- a question

    Exercises

    Next

    Bring a calculator! [ 06.html ] [ lab03/ ]

    Abbreviations

  1. Algorithm::=A precise description of a series of steps to attain a goal, [ Algorithm ] (Wikipedia).
  2. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular and free C++ compiler.
  3. KDE::="Kommon Desktop Environment".
  4. OOP::="Object-Oriented Programming", Current paradigm for programming.
  5. Semantics::=Rules determining the meaning of correct statements in a language.
  6. SP::="Structured Programming", a previous paradigm for programming.
  7. Syntax::=The rules determining the correctness and structure of statements in a language, grammar.
  8. Q::software="A program I wrote to make software easier to develop",
  9. TBA::="To Be Announced", something I should do.
  10. TBD::="To Be Done", something you have to do.
  11. UML::="Unified Modeling Language".

End