[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] / [CSE201] / 04
[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 Apr 11 07:59:44 PDT 2013

Contents


    cs201/04 -- Objects

      Previous -- Numbers

      [ 03.html ]

      Project 1 Due at start of class

      [ projects.html#P1 ]

      Study pages 62 to 86 inclusive

        Computers handle non-numeric data.

        These are called objects in C++.

      1. object::gloss="A collection of pieces of data that has operations defined on it", all objects in C++ belong in a class that defines the type of data and what you can do to it.

        2.6 Strings

          A vital, common, useful class of objects are strings. Strings will appear in quizzes, labs, classes, projects, the final, and most real programs you write.

          2.6.1 String Variables

           	string variable;
          Sets variable to the empty string.

           	string variable = "initial value";

          Notice that the first character in a string is numbered zero

          2.6.3 Substrings

          Table 4 String Functions

          Take note of these.

          Syntax 2.9 Member function call

        1. member_function_call::= object "." function_name "(" list_of_parameters ")".
           		my_name . length()
           		my_name . substr(0,1)

          2.6.3 Concatenation

          Advanced Topic 2.6 -- Old fashioned strings and chars

          We will review this later.
        2. character_literal::="'" char "'".
           		'a'
           		'\n'

          Examples -- initials

          [ initials.cpp ]

          2.6.4 Formatted Output

          The library named iomanip is a complex and complete library of functions that change the format of input and output. I usually cover it in detail in CSCI202. But if you want a reference see [ ../cs202/iomanip.html ] for all the details in a terse format.

          Notes on strings

          [ string.html ]

        . . . . . . . . . ( end of section 2.6 Strings) <<Contents | End>>

        Why we have objects and how they work

        The watch analogy.

        Example 1 -- string


        Table
        String member functionsReturns
        s.length()int
        s.substr(start)string
        s.substr(start, number)string

        (Close Table)

        2.7 Using Objects

        Objects are an important way to make complex programs easier to understand and change. The example classes of objects: Time, Employee, Point, Line, Circle, ... are not part of standard C++. They will be used to make some labs more entertaining. The may enrich some classes.

        Objects have

        1. Constructors -- make new objects
        2. Mutator functions -- change existing objects
        3. Accessor functions -- return properties of existing objects
        4. Destructors -- behind the seen tidying up of old objects

        2.7.1 Time Objects

        Syntax 2.10 Object Construction

      2. object_construction::=class_name ( parameters )| class_name ( ).
         		Time()
         		Time(10,0,0)
         		Employer("Richard Botting", 80000)
         		Point(1,2)

        Syntax 2.11 Object Variable Definition

      3. object_variable_declaration::= class_name variable_name ( parameters ) | class_name variable_name.
         		Time now;
         		Time class(10,0,0)
         		Employer me ("Richard Botting", 80000)
         		Point origen(0,0);

        Table 5 Member Functions of the Time Class

        Take note.
        Table
        ExampleTypePurpose
        Time()constructorMakes an object for the current time
        Time(h,m,s)constructorMakes the time with hours h, minutes m, and seconds s
        t.get_seconds()accessorreturn number of seconds in t
        t.get_minutes()accessorreturn number of minutes in t
        t.get_hours()accessorreturn number of hours in t
        t.add_seconds(n)mutator changes t to n seconds later
        t.seconds_from(t2)accessor return the number of seconds between t and t2

        (Close Table)

        example on page 73 ch02/time.cpp

        [ time.cpp ]

        Example time2.cpp

        [ time2.cpp ]

        Common Error 2.7 -- Wot -- No object

        2.7.2 Employee Objects

        [ employee.cpp ]

        Table 6 Member Functions of the Employee Class


        Table
        ExampleTypePurpose
        Employee(n,s)constructormakes a new employee with name n and salary s
        e.get_name()accessor returns the name of e.
        e.get_salary()accessor returns the salary of e.
        e.set_salary(s)mutators changes the salary of e

        (Close Table)

        2.8 Displaying Graphical Shapes

        2.8.1 Graphic Objects -- Point Circle Message Window

          We have a version of Horstmann's Graphic library in
           		/share/ccc3e
          we also have to include the windowing library called "X11R6" in our code. So a typical compilation looks like
           g++ -w -I/share/ccc3e -I/usr/X11R6/include -I/usr/include -L/usr/X11R6/lib -lX11 -o lab03/message lab03/message.cpp /share/ccc3e/ccc_x11.cpp /share/ccc3e/ccc_shap.cpp

          The good news is that "Q" does this for you if you "#include" the ccc library. The above command was run when I typed

           		Q lab03/message.cpp

          Note that it is much easier to do graphics programs in C++ in our labs rather than remotely. You need a working local C++ compiler and the Horstmann CCC library installed.

          Table 7 Point Class

          Table 8 Circle Class

          Table 9 Line Class

          The default coordinate system in ccc

          [Grid showing coordinates in default ccc window]

        Random Fact 2.2 Computer Graphics

        My Ph. D. topic -- The fundamental algorithms of Computer Graphics.

        The secret of computer graphics is the Cartesian Geometry invented by Rene Descartes, where each point in the picture or on the screen is given a pair of numbers (called x and y coordinates).

        Consequence: computer graphics includes a lot of mathematics.

        2.8.2 Choosing a Coordinate System

        2.8.3 Getting Input from the graphics window

        Table 12 GraphicsWindow

        Refer to this when writing graphic code.

        For Your Information -- A Picture of the ccc Graphics Library

        [A Unified Modelling Language model of the ccc library]

      . . . . . . . . . ( end of section 2.8 Displaying Graphical Shapes) <<Contents | End>>

      Review Questions

      Try to do as many of these questions as you have time for: R2.8, R2.9, R2.10, R2.11, R2.12, R2.13, R2.14, R2.15, R2.16, R2.17, R2.18, R2.19, R2.20, R2.21.

      Write up and hand in one of these exercises and your solution, with your name, for me to grade, before the start of class.

      Exercises on data types

      [ av03b.cpp ] (review tracing), [ av03.cpp ] and [ av03a.cpp ] (String operations and tracing), [ av03c.cpp ] (substrings), [ av03d.cpp ] (concatenation), [ av03e.cpp ] (concatenation substring and length).

      Programming Problem: you need to input a phone number from the user in format "123-456-7890" and extract and output the area code("123") and the seven digit phone number("4567890").

      Quiz 1 at end of class

      Next -- if-then-else etc

      [ 05.html ]

    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 the data it knows and the functions it can execute.
  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 value".

( End of document ) <<Contents | Top