[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] / [CSE201] / 12
[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]
Tue May 14 15:47:19 PDT 2013

Contents


    cs201/12

      Previous 11 Case Study

      [ 11.html ]

      Project 5 Due

      [ projects.html#P5 ]

      Study 5.1-5.3 pages 227-237

        5.1 Discovering Classes

        You notice that you are repeating yourself....

        Example Code: [ bestval.cpp ]

        Common Error 5.1 -- getline and >>

        This does not fit in this chapter. But it is vital information. Stick a notelet in this page and be ready to come back to it when your program mysteriously refuses to read in using getline just after it has done a ">>".

        5.2 Interfaces

        Notice the distinction between interface and implementation.

        Look at the walls in our lecture rooms. How many different kinds of socket can you see? Each is a different interface. An interface is a place where you can "plug things in" and so get to use things on the other side of the interface.

        In programming, an interface is a list of function prototypes. You use the interface by calling functions in the list. The functions hide the code implementing them. You use functions in the list with out worrying (too much) about what they are.

        If it quacks like a duck, and it walks like a duck; then it is a duck!

         class Duck
         { public:
         	void walk();
         	string quack()const;
         };

        Notice the phrases: member function, constructor, mutator function, accessor function.

        Example code: [ product1.cpp ]

        Syntax 5.1 Class Definition

      1. class_definition::= "class" name "{" "public:" members "private:" members "};" Memorize the syntax of the C++ class definition. Notice the semicolon at the end of the class.
         class Card
         {
         	public:
         	   void read();
         	   void print() const;
         	private:
         	   int suit;
         	   int rank;
         };
      2. members::= # member, members are always declared in a class and belong to objects of that class.
      3. member::= data_member | member_function.
         	int i;
         	void add(int n);
         	int value() const;
      4. member_function::= constructor | mutator | accessor | ... .
      5. mutator::= a member function that changes the data in an object.
         	void add(int n);
      6. accessor::=a member function that does not change the data fields in an object but outputs or returns a value computed from the data.
         	int value() const;
         	void print() const;

        Note: you can have functions that do not change data fields but don't have the const that tells the compiler that that don't keep things constant. Good programmers don't write functions like this -- ambiguous about whether they are accessors or mutators. Nether do the write functions that both change the data and return values...

        A Fun Example with toads and magic

        [ magic0.cpp ]

        (Notice the need to write a program to test the class...)

        Common Error 5.2 -- forgetting a

        Don't forget the ________________________________ at the end of a class!

        Hint -- use a fill in the blanks file for classes

        [ class.cpp ]

        5.3 Encapsulation

        Putting things inside a capsule. Protecting them from prying fingers.

        Can I borrow and open up your expensive watch?

        I only want to know what time it is!

      Review Questions

      Answer as many as you can of R5.1, R5.2, R5.4, R5.5, R5.8, R5.13 and hand in one for credit.

      Exercises

      [ 12ex0.cpp ] [ 12ex2.cpp ]

      Quiz 5 on loops

      TBA

      Lab on functions

      Next 13 Member Functions

      [ 13.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