[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] / [CSE201] / 13
[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:51:51 PDT 2013

Contents


    cs201/13

      Previous 12 Declaring classes

      [ 12.html ]

      Study 5.4-5.6 pages 237-249

        5.4 Member Functions

        Functions are like buttons that let you do things to objects. Think of the buttons on a watch. Some give you data, and some change the behavior or state of the watch.

        Types of member functions

        There three kinds of member functions in a class. First there are constructors that build new objects when called. Then there are accessors or queries that get information about the object without changing it. They normally return a value calculated from the internal state of the object. And finally mutators change the data members or variables in the class. You can write mutators that return data but this is thought to be a bad idea.

        Accessors are defined to be "const" functions. This means that the compiler stops them from changing the private data in the object. They usually return data and so are usually not void.

        Mutators must not be "const" and should be "void" functions.

        Both mutators and accessors can have parameters if needed.

        You may meet functions that don't have the const but do not change the data in the class. THese arenether accessors or mutators. These are a source of sneaky bugs and should be avoided and if possible refactored.

        A getter is a special kind of accessor that "gets" the value of a private variable. Its name should start "get". These are very common.

         	double getSize()const{return size;}

        A setter is a special kind of mutator that "sets" the value of a private variable. Its name should start "set". These are quite common.

         	void setSize(double s)const{size = s;}

        Syntax 5.2 Member Function Definition

         		type  class::name( arguments )        { body }
        For example
         		void  Snake::slither( double meters ) { .... }
         		Sound Snake::sound(  )const           { return "hiss" }
         		void  Snake::swallow( Animal prey )   { .... }
        Member functions are either constructors, mutators, or accessors.

        Extra -- Inline member functions

        There is a short way to include a complete function defion inside the class defintion. This means you do not need the double-colon prefix to resolve the scope of the function. This should only be done with functions that are simple, short, and obvious. It changes the way that function calls are compiled so that the resulting code is larger but runs faster -- because a copy of the function is put where the call appears!

        Here is a sample: [ test.circle.cpp ]

         class Circle
         {
           private:
         	double radius;
           public:
         	       Circle()               {radius=0;}
         	       Circle(double r)       {radius=r;}
         	double getRadius()const       { return radius; }
         	void   setRadius(double v)    { radius=v; }
         	double getCircumference()const{ return PI*radius; }
         }; //don't loose the semicolon.

        Inline member functions are very useful for getters, setters, and constructors.

        I use this in quizzes and class work because I need to get the whole thing into a single slide or page.

        Common Error 5.3 - the missing const

        The description of the function in the class must match the form of the function definition. There must be no way that the function can change a member variable.

        5.5 Default Constructor

        What happens when you declare an object?
         		Class object;
        Answer: The Class::Class constructor is called to make sure that 'object' is ready for use.

        The default constructor constructs default objects!

        Example code [ product2.cpp ]

        Random Fact 5.1 Programmer Productivity

        5.6 Constructors with Parameters

        What do I do if I don't like the default object? Answer: Include some parameters (and hope the class has defined them!)
         		Class object(parameters);

        Syntax 5.3 Constructor Definition

         		className::className(parameters)
         		{
         			code
         		}

        Common Error 5.4 forgetting to initialize things

        Duh!

        Common Error 5.5 abusing the constructor

        Constructors do not change variables. They are used (implicitly) to declare variables. They can also be used rather like constants in expressions:
         		object = Class();
         		object = Class(parameters);
        Notice that the new value of the object overwrites the values in the old object.

        Advanced Topic 5.1 -- initializing fields

        Come back to this next time.

        Advanced Topic 5.2 -- overloading

        C++ lets you declare your own versions of any operator and function you like. It gives you the one it thinks you need. This works better than you might expect as long as you use '+' to mean something like addition!

        But follow the Golden Rule: Write code as if you are going to be reading and changing it.

      Project 6 Due Next time

      [ projects.html#P6 ]

      You must tackle one of the programming projects at the end of Chapter 5 -- create a class and test it.

      Error in Programming Project P5.1

      Review Questions

      Do as many of R5.6, R5.7, R5.9, R5.10, R5.11, R5.12 as you can and hand in one for grading.

      Exercises

      What functions should a Watch have? How might you code them as a class Watch?

      Members of a class: [ 12ex.cpp ]

      Here is a Microwave class ... [ 13ex2.cpp ] What can you add to it?

      Quiz 6 next time

      Classes Member Functions data objects etc

      Lab 7 Classes

      [ lab07/ ]

      Next 14 More on Classes

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