[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng Dept] / [R J Botting] / [CSci201] / 17
[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]
Wed Mar 5 18:04:38 PST 2008

Contents


    pages 243-250 Example of Object Oriented Programming

      Project 7 Due in

      We will skip 233-242

      Reading 7.5 An Object Oriented example

        Introduction

        A complete OO example -- a blackjack-like game.

        page 253 Declaring suit and Card

        You may want to review enumerations [ 5.8 Enumeration types in 14 ]

        Error in declaration of class Card

         	Card(suit = clubs, int = 1);
        should be
         	Card(suit ss = clubs, int = 1);

        page 244 -- declaration of CardStack

        page 245 -- definition of CardStack functions

        page 245 -- coding error

        In place of
         		for(suit s=clubs; s<spades; ...)
        I think we need
         		for(suit s=clubs; s<=spades; ...)
        so that the pack actually contins the suit of spades, as well as clubs, diamonds, and hearts.

        Note on shuffling cards

      1. The function Cardstack::shuffle() repeats the switching of the two cards 1000 time. I've often used an algorithm that only needs 52 at most switches!
         	void Cardstack::shuffle()
         	{
         	   srand(time(0));
         	   for (int i = 0; i<stack.size()-1; i++)
         	   { // pick the i'th card from rest of stack
         		int n1 =i+ rand() % (stack.size()-i);
         		Card temp = stack[i];
         	        stack[i] = stack[n1];
         	        stack[n1]= temp;
         	   }
         	}
        I may have the details wrong above...

        page 246 -- random numbers -- rand and srand

        page 247 -- Declaration of Player

        page 248 -- The main program

        page 248-250 -- Definition of the Player functions

        page 250 -- Example of a run

        Page 251 -- exercise 7.5 has an error

        The formula for d, the distance between two points needs fixing. See [ Distance#Geometry ] on the Wikipedia.

      Questions

        is "Player.h" in most compilers

        No. You have to prepare file with name "Player.h" and put it in the same directory and the file that #include "Player.h" is written.

        All C++ compilers handle #include "foo.bar" in the same way. The open the foo.bar file and read it -- as if you had included a copy in you code.

        What are constructors

        They are member functions of a class that have the same name and initialize the data memebers of the class. For more info follow the links constructed by [ lookup.php?search=constructor ]

        Are there any other ways to initialize data memebers other than using constructors

        Not reliably.

        What you often find is a setter memebr function that changes the value of a data memebr. For example if you have

         	int knobs;
        in a class, then you may also have
         	void setKnobs(int newvalue){ knobs = newvalue; }

        A good rule is to only add a setter function when it is going to be used by another class.

        What are some other type of prgrams in which object oriented programming can be used

        OO has been used ofr all applications, and all sizes of prgram from the smallest "Hello World" to whole operating systems, like Linux.

        The only case for not using objects is when the program is trivial in a non-OO language. Fir example I do a lot of programming using the Linux shell and tools. They are not OO but are very powerful for sorting, editting, searching, summarizing tables.

        Similarly your grades a kept in a spreadsheet these days. It is more flexible and easiy to use than the Object Based Hypercard system Used in the late 1980s and early 90s.

        Dont you need a main program to use a class

        Yes. With out a main function the program never starts running. Becasue 'main' is where it starts.

        However -- a class may be used by another class, and that class can be use by main.

        Why is the simplest solution usually the right one

        Simple solution don't have as many problems and are easier to fix when things change.

        This philosophy is also a guide for us to avoid unneeded complexity.

        When drawing is there an easy way to combine several classes into one

        What a cool idea!

        Not as far as I know. None of the tools I've used in the last 20 years had this feature. You just have copy in the attributes and operations and connections...

        It is best to only have one class in your diagram from each name. We ended up with several "Person" classes in lab08 because the book illustrated each concept in a different diagram.

        If you wanted a user to input a function with variable what type could you use

        C++ is a compiler it handles and stores functions before the program runs. After then there is no compiler running to accept and insert new code.

        More -- this is a very dangerous feature. Whenever a language has allowed users to supply code somebody has exploited to take control ofthe computer on which the program runs.

        A quick example -- the first internet worm caused utter havoc by using email to send and compile code accross the internet. It sent a copy of the attacking program.... and they spread, grew, took over more and more machines ....

        What is an objective oriented example

        Looks like a typing error to me.... but there is a langugae "Objective C".

        What is an object oriented example

        See todays reading!

        What is the defference between quotes and daimond brackets in include statements

        This
         	#include <iostream>
        starts search the compilers standard library for a file called "iostream".

        The

         	#include "myFile.cpp"
        starts the search in the same directory as the file in shich you types the #include line. Only after filing to find it, does it search the standard libraries.

        What exactly does the computer do with int counter = sqrt(n) when n is not a perfect square

        Lets try

         		int counter = sqrt(17);

        1. First the computer makes space for one int in RAM with the name "counter". Then it preceeds to find the intial value to store in it.
        2. Since 'sqrt' is a function that needs a double to work on, the computer converts 17 into a double -- 17.000000.
        3. The sqrt function works on 17.00000 and produce a good approximation to the square root of 17. Something like 4.1231056...
        4. Next the double 4.1231056... coerced to fit into an int slot. To do this it is rounded off to 14. This will be to the closest integer to the double number.
        5. The rounded value 4 is placed in the memory area called counter.

        Notice this hard-to-remember fact: the expression on the right of '=' is evaluated the same way whatever the variable is on the left of the assignment. After the value is found it is made to fit the variable.

        With nested for loops do you do the whole of the second loop inside the first one

        Yes. If you see code like this
         		for(A; B; C)
         			for(D; E; F)
         				G
        then the sequence of events is like this
         A; B;  D; (E; G; F; E; G; F; ) !E; C
            B;  D; (E; G; F; E; G; F; ) !E; C
            B;  D; (E; G; F; E; G; F; ) !E; C
           !B;

        How do you get a program to pick a card at random

        The function 'rand' is used in C++. All other languages have a similar function. They work as random as it gets. The book has a very good example of rand.

        What are enumeration types

        [ 14.html#5.8 Enumeration types ]

        In what situations does having constant object benefit a programmer

        When you have an object that must not, even by accident, change or mutate.

        I didn't understand friends

        They are for Thursday. [ 18.html#8.3 Friends ]

        What is the inheritance concept

        This is part of CS202: [ ../cs202/inheritance.html ]

        How do objects oriented programming relate to data mapping

        Interesting question.

        First both data bases and OO are inspired by making the software reflect the real world. You can even use UML diagrams (with no operations) to describe a data base.

        Second they have totally different theories. Most databases these days are "Relational" -- organized in tables connected by keys (values of attributes). In OOP we use the addresses (pointers) of data to connect and relate the objects.

        There are some data bases that over some object-oriented features and so we may end up with a consistency between the two approaches.

        Rich now, however, most OO prgrams has a special set of classes -- the persistance layer -- that know all about the data base the program uses but stops the other objects needing to know about the data base. This is deep stuff and we have several classes that touch on it.

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

      Quiz 8 -- Program with a class and main

      Lab 09 -- UML diagrams of Clock, Flight, Card, Cardstack, ...

    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