.Open CSci202 Computer Science II, Session 05, Classes (Previous): Char* .See http://www/dick/cs202/04.html . Preparation Study this page and Chapter 9 on C++ classes and write down the questions, doubts, and surprises that you have on a piece of paper. Send one, thru my cs202 web site before 10am before class. .Open Input You may have to go back to Chapter 3. 9 Classes: A Deeper Look, Part1 487 9.1 Introduction 488 9.2 Time Class Case Study 489 9.3 Class Scope and Accessing Class Members 494 9.4 Separating Interface from Implementation 496 9.5 Access Functions and Utility Functions 498 9.6 Time Class Case Study: Constructors with Default Arguments 500 9.7 Destructors 506 9.8 When Constructors and Destructors Are Called 507 9.9 Time Class Case Study: A Subtle Returning a Reference to a private Data Member 510 9.10 Default Memberwise Assignment 513 9.11 (Optional) Software Engineering Case Study: Starting to Program the Classes of the ATM System 515 9.12 Wrap-Up .Close . Sample class Point in class .Image 05.png [Point in UML] Here is some possible code .See ./05Point.h and .See ./05testPoint.cpp for the above diagram. . Chapter 9 pages 489 -- TIME_H Is TIME_H something that is being created in Fig. 9.1 or is it something pre-rendered in the C++ Library? In Fig 9.1 TIME_H is a symbol created, if it does not already exist, when the file is included in other files. . Chapter 9 pages 489-494 -- Time Class Case Study example In the first example figure 9.1, what does #ifndef, #define, and #endif mean? ifndef::="a preprocessor directive that tells the compiler to only compile code if an identifier has not been defined". ifdef::="a preprocessor directive that tells the compiler to only compile code if an identifier has been defined". define::="a preprocessor directive that tells the compile to define a term". endif::="A preprocessor directuve that matches an $ifndef and terminates a conditional piece of code". . Chapter 9 pages 489-489 -- time class case study an example in the book shows #ifndef Time_h and #define Time_h, what does it do and in what other way can we implement them in a program First -- the correct identifier is "TIME_H" not "Time_h". The best practice of using all capitals for this purpose is in the book and has been used for 20 years. The "#ifndef" stands for "If not defined" and it tests to see if a "#define" has been compiled for the metioned variable. So the piece of code between the "#ifndef TIME_H" and the "#endif" will be ignored if TIME_H has been defined already. The compiled only compiles the code between them if "TIME_H" has not been defined.... and the first thing it does is define "TIME_H" -- with an empty syring as value.... The effect is to stop a file being included twice in one compilation. I think it is a bit of a kludge..... but it works and is tradtional. Copy it from header file to header file.... changing the TIME_H symbol as needed. Story -- a couple of years ago, I omitted the incantation on a simple prgram with an included file ad watched it go wrong.... I had to fix it to get the lab to work. . Chapter 09 pages 494-496 -- class scope and accessing class members what is the purpose of the scope resolution operator? The scope resolution operators (two colons) .As_is Scope::identifier tells the compiler which scope (class) it needs to look in to find an identifier. It is commonly used when defining member functions for a class... for example .As_is void Counter::click1() { ..... } would refer the compiler back to .As_is void click1(); inside the "class Counter{ ..... };" It can also be used to refer to public "static" parts of a class. For example, if we had a class of mathematical constants: .As_is class Math{ public: const double PI = 4.0*atan(1.0); ... }; then .As_is Math::PI * d would be the perimeter of a circle with diameter `d`. . Chapter 9 -- class scope Can you elaborate on class "scope" ? Normally the members of a class (variables, constants, functions, ...) are attached to objects. If they are declared with the magic word "static" then they belong to the whole class. For example, if we had a class called CS201Students modeling "CSci201 01" then you might be an instance of the class. You have your own name, id, grades, ... and so on. But the number of people in the class is not a property of any one person in the section. it is a prpoerty of the whole section.... we would declare it as a "static int" and it would have "class scope". Chapter 9 -- Multifile programming are we going to start separating programs into multiple files in this class? Yes. When necessary. . Chapter 9 pages 489 -- .h files can you please elaborate on how to separate function prototypes and function definitions in separate files, like a .h file and how would one go about compiling both .cpp and .h files together. MiniLecture .See ./resources.html#Complex Projects with possible demo..... more in labs to come. Using `make` with multifile programs: .See ./make.html . Chapter 9 pages 490 -- Classes What does it mean to inline a function defined outside of a class? Also, why would you define a member function outside of a class and then tie it to the class? Wouldn't it be better to just include it in the class? A normal function is handled by the compiler by planting a single copy of the compiled code plus jumps that transfer control to the function from it's calls. An .Key inline function is one that the compiler copies the body into the code where the call is. The call vanishes and is replaced by a copy of the body of the function. Inline functions run a bit faster than normal functions, they can also take up more space. As a rule inline functions should be small -- to save space. The result of executing a function call -- inline or out-of-line -- is the same. There are two ways to specify that a function is `inline`. First you just add "inline" to the function definition. This works with free-standing functions that are not members of a class. There is a special rule in C++ for member functions. If a function is completely declared inside the class itself then it is assumed to be an .See inline function but if the body is declared aftern the end of the class ... using .As_is type Class::Function(....){.....} then it is assumed to be not an inline function. . Chapter 9 -- scope what is a block scope? A block is a piece of C/C++ code that starts with a "{" and ends with a matched "}". Variables and constants etc defined inside a block, normally only have meaning inside that block and so are said to have "block scope". The scope resolution operator, the dot operator, and (later ) the arrow can allow identifiers, if public, to be refered to outside their block. . Chapter 9 pages 491 -- Time Class Case Study Can you explain the code on Figure 9.2 on page 491? What are the "? h" "? m" "? s" for in the validation statements? I do not see them in the class "Time.h" on the previous page either. The syntax .As_is condition ? expression1 : expression2 is a .Key conditional expression that (1) evaluates the condition and then (2) picks either expression1 or expression2 to evaluate, and (3) returns the value of the selected expression. The "?:" is the only ternary operator in C++ -- each "?" must have a ":". The idea dates back to the LISP language in the 1960's. You will find them on pages 140 and 141 in out text. Example: .As_is i<60? i+1: 0 (if i is less than 60 then return i+1 else return 0). Quick example anybody? . Chapter 09 pages 491 -- Time Class How exactly does the following void function in fig. 9.2 validate the hour, minutes, and seconds? .As_is void Time::setTime( int h, int m, int s ) .As_is { .As_is hour = ( h >= 0 && h < 24 ) ? h : 0; .As_is minute = ( m >= 0 && m < 60 ) ? m : 0; .As_is second = ( s >= 0 && s < 60 ) ? s : 0; .As_is } It does similar tests on h, m, and s. For `h` for example it carries out the following little algorithm .( if h>=0 and h<24 then set hour to h else set hour to zero. .) For example is h is 42 then hours becomes 0. Thus `hours` ends up with a valid value between 0 and 24 inclusive. Something similar happens with the other two attributes..... exercise. . Chapter 9 pages 492 -- Time Class Case Study Between Stanadard and Universal Time, is there one more preferrable for a programmer to use in a code? THis is not a question that a programmer should answer without talking to their cliets first. Different clients have different prefered formats for time. If the code is going to be used in many countries the problem becomes rather large and complicated. Personally I alwasy have problems with a date like 10/12 -- is it the 12th of October (USA) or the 10th of December (UK). . Chapter 9 pages 494-496 -- Accessing Class Members Can you explain how public class members are referenced through the three handles on the object shown in the book? There are three ways of refering to an object -- as a variable, a reference, and as a pointer. here are the typical declartions: .As_is Class variable(parameters); .As_is Class &reference = Class(parameters); .As_is Class *pointer = new Class(parameters); .As_is Class *pointer = & variable; Pointers can be moved to point to other objeccts any time we want. A public data member/attribute `data` is refered to like this .As_is variable.data .As_is reference.data .As_is pointer->data (Notice the arrow is used with the pointer). More, member functions (if public) are called like this .As_is variable.function(arguments) .As_is reference.function(arguments) .As_is pointer->function(arguments) . Chapter 9 pages 498 -- subject What is the difference between a utility function and an access function? can one return a value to another? A utility function is useful -- thats all. An access function provides data to the users of objects. Access functions should be useful.... and so utilties. Any function in a class can call any other function. . Chapter 9 pages 498-499 -- Acess Functions and Utility Functions What are predicate functions? A predicate is a function that returns a Boolean value -- either true or false. .As_is is it time for lunch? Predicates ask questions -- and computer answers. If a member function is a predicate .As_is bool Widget:: empty() { ...... } then it is also an access function... and normally should have a "const". . Chapter 9 pages 489 -- constructors in figure 9.2, is there a way to override the constructor's initial assignments of hours minutes and seconds to zero, using the header file code? As written the default intial values will always by 00:00:00. If you rewrite the file with a different constructor .As_is Time(int h, int m, int s) { setTime(h,m,s); } then you can declare a Time to be what you want. . Chapter 9 pages 506-507 -- Constructors/Destructors Can you give a better and simple explanation of how constructors and destructors are called? Constructors are nearly most often called by a declaration. Their job is to assign the correct internal values to the attributes of the declared object. Somethin similar happens with the "new" operator which uses a constructor to set up a block of storage, and then returns its address. Destructors are called automatically when an object goes out of scope. The rules are logical and complex -- we will talk about them when we get to polymorphism. . Chapter 9 pages 506 -- Destructors Can you elaborate on the functionality of destructors within classes? In which case would a destructor be useful? Destructors .As_is ~ClassName() { .....} are called when an object is deleted.... and are used to tidy up any storage allocated by "new" to the object or its components. Use a destructor to describe how to return any dynamic or heap storage to the heap. If the class, at any time, uses "new" then the destructor should "delete" whatever was constructed in the "new". When an object is deleted the destructor is called to tidy up. If the class does not tidy up properly, then you will have what is called a "memory leak" and you program will run out of memory when running for a long time. Funny story on memory leaks: Tony and the 2903. . Chapter 9 pages 506 -- Destructors I read in the book that a destructor must be public, what would happen if it was private? I'm not sure, but I suspect the program would not compile. . Chapter 9 pages 509 -- destructors What happens if destructors aren't used? Detructors are called by the program, altomatically -- if they are declared. If you don't declare a destructor you may create a memory leak. . Chapter 09 pages 521 -- Classes What happens in a class when things are declared under private and public? The whole program can refer to the things that are public, and only the class itself can refer to the private stuff. Private/public sets up a chinese wall or information barrier around the objects that the class describes. Notice well: If you don't write "public:" in a class, then, by default, the whole class is private -- a rather useless result. You need to think about what to keep private and what public -- first cut: attributes/variables should be private, and most functions should be public. . Exercises Based on the $Input above. Examples: take UML and code in C++, take given C++ and draw UML, ... A CaloryCounter counts up only. It has three dials: hundreds, tens, and units. It has three clickers/buttons: one for each dial. Draw a quick UML diagram, write a test program, and code class in C++. .Close CSci202 Computer Science II, Session 05, Objects in C++ . Lab on THe risks of character arrays .See http://www/dick/cs202/lab03.html (Next): More about C++ Classes (Chapter 8) .See http://www/dick/cs202/06.html . Next Project Project 2 .See ./project2.html is due on class 8.