.Open Classes . Introduction Here are the things that you need to be able to do with classes: .Box Know the words: $class, $object, $class_name, $constructor, $member_function, $data_field, $UML, $accessor, $mutator, $getter, $setter. On quizzes and the CS201 final you will have to label code correctly with the above words. Work out what a simple program with Classes does. Imagine new classes of objects. Draw a picture of your classes using the Unified Modeling Language or $UML. I will be handing out an introduction to the UML that has more than you will need in CS201. Write a class in C++ that does what you want. Read a diagram the uses the Unified Modeling Language to document some classes. I'll hand out an example of UML documenting Horstmann's CCC library. Given the UML for a class, write the C++ code. Given the C++ code, draw a rough UML diagram of it. .Close.Box A class is a description of a set of similar objects. Once a class is defined you can declare new objects that belong in that class. The class describes how the objects function. A class lists functions that can be applied to objects in that class. It lists the ways you construct new objects. It can even give special meanings for operators like + and -. . Exercise on Real Objects and Real Classes In groups of 2 and then 4 write down the common or shared features of all your watches. The common features will be used to define the class Watch. Repeat for the class: Calculator. . C++ Classes Programs contain objects, Objects contain data. Objects can do things. Each object's class describes what the object can do and the type of data stored inside the object. An object in C++ has an internal structure. An object is a piece of storage divided into sub-boxes. The class lists the sub-boxes (data fields) and gives them names. Each object can have different data values for its data_fields. All have the same data fields however. The functions listed in a class are allowed to manipulate the data inside the objects. Normally other functions are stopped from doing this. The member functions (those listed in a class) work with the data fields exactly as if they were variables. The class therefore declares data fields exactly as if they where variables. Template for a class: .As_is class put_name_here .As_is { .As_is public: .As_is list member function prototypes here .As_is private: .As_is declare data fields here .As_is }; Note: Do `not` forget the semicolon at the end of the class. Note: Do `not` forget the semicolon at the end of the class. Note: Do `not` forget the semicolon at the end of the class. Template for a member function: .As_is type class_name::function_name( arguments ) .As_is { .As_is put behavior of function here .As_is } Note: Do `not` put semicolon at the end of a function. Note: Do `not` put semicolon at the end of a function. . Glossary classes::=plural of $class. class::=a collection of $objects with similar behavior($member_functions, $constructors) and internal data($data_field) with a $class_name. class_name::=a singular noun phrase attached to a $class or similar $objects. For example in C++: .As_is class Counter /*class name*/ .As_is { public: .As_is Counter();/*constructor*/ .As_is void count();/*mutator member function*/ .As_is int value()const;/*accessor member function*/ .As_is private: .As_is int state;/*data field*/ .As_is }; UML::="Unified Modeling Language". The UML is used to draw pictures of classes: .Image counter.gif Counter class in UML The name is in the first compartment, the data fields are in the second compartment, and the operations/functions are put in the third part of the box. You can omit the 2nd and 3rd compartments if you want. objects::= plural of $object. object::=an instance of a $class which hides certain data inside it and has its functionality defined by its class. An object can be created by using a $class_name as a type in a C++ declaration: .As_is Counter the_count; /* the_count is a new object in class Counter*/ The UML can also be used to draw a picture of an object. The name of the object and its class are in the top part of the box and are underlined. The values of the data in object are put in the second compartment. For example: .Image ch08c.gif Counter the_count in the UML constructors::=plural of $constructor. constructor::=a way to create a new $object that is an instance of a $class with the same name as the $class's name. A constructor with no arguments is called when objects are declared. It is called the default constructor: .As_is Counter::Counter() { state=0; } member_functions::= plural of $member_function. member_function::=A function listed inside a $class that has access and change the $private data inside an $object of that $class. A member_function is often listed in the definition of the class but fully defined separately using a double-colon symbol: .As_is void Counter::count() { state = state + 1; } .As_is int Counter::value()const { return state; } A member function is nearly always called by first naming an object and then the function. For example we can ask `the_count` to record a count: .As_is the_count.count(); /* ask the_count to count something*/ It executes the following (found in the class Counter): .As_is { the_count.state = the_count.state + 1; } And now it looks like this: .Image ch08d.gif Counter the_count after the_count.count() We can ask `the_count` what it's value is and output the result: .As_is cout << the_count.value(); /* apply value() to the_count*/ Object `the_count` is in class `Counter` so it executes this code: .As_is { return the_count.state; } Inside a $member_functions definition, all calls to functions that are members of the same class refer to the same `implicit` object. const::=in a member function the reserved word "const" indicates that the function does not change the data fields in the object. In the class `Counter` the function `value` was like this. We therefore know that .As_is the_count.value() does not change `the count` in any visible way. This means that it is an $accessor function. If there is no `const` the function can change the object. It is a $mutator. accessors::=`plural of $accessor`. accessor::=`a member function that does not change the object to which it is applied`, see $const. getter::=`an $accessor that returns the value of a private data field`. mutators::=`plural of $mutator`. mutator::=`a member function that may change the object to which it is applied`. Mutators are often `void` member functions, but are never $const. setter::=`a $mutator that seems to change a single hidden data item`. Also: see $constructors above. data_fields::=plural of $data_field. data_field::=A variable that is hidden inside an object, it must have the name and type declared in the $object's $class. .As_is int state;/*data field*/ As a rule: keep all data private and minimize the number of $mutators and $accessors. Never forget: YAGNI::principle="You Ain't Gonna Need It". This principle suggests you don't add a function because you might need it, you only add it when you have a real immediate need for it. `Need` here means: you can give a client some value by using it. . Example program testing a class .As_is int main() .As_is { .As_is Counter i; .As_is while( i.value() < 10 ) .As_is { .As_is cout << i.value() << " " << i.value() * i.value() << "\n"; .As_is i.count() .As_is } .As_is }//end main Note. Shift click the following to download a program made up of the Counter classes, its functions and the main program: .See http://www/dick/cs201/ex08b.cpp Can you predict what it will do before you compile and run it? Now download and try to predict this program -- that uses the same class but has two objects: .See http://www/dick/cs201/ex08c.cpp Now download and try to predict this program -- that uses the same class but has two objects in a different way: .See http://www/dick/cs201/ex08d.cpp If you want to see what this process looks like download and run the following Graphic CCC version: .See http://www/dick/cs201/ex08e.cpp .Open Chapter 6 . 6.1 Discovering Classes You notice that you are repeating yourself.... . Common Error 6.1 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 >> just after it has done a getline. . 6.2 Interfaces 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 it. Hidden behind it are some things that you can use 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! Memorize the syntax of the C++ class definition. Notice the semicolon at the end of the class. . Common Error 6.2 Don't forget the ________________________________ at the end of a class! . 6.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! . 6.4 Member Functions Functions are like buttons that let you do things to objects. Notice the syntax .As_is type class::name( arguments ) { body } For example .As_is void Snake::slither( double meters ) { .... } .As_is Sound Snake::sound( )const { return "hiss" } .As_is void Snake::swallow( Animal prey ) { .... } Member functions are either $constructors, $mutators, $accessors, or useless. . Common Error 6.3 - the missing const The description of the function in the class must match the form of the function definition. . 6.5 Default Constructor What happens when you declare an object? .As_is Class object; Answer: The Class::Class constructor is called to make sure that 'object' is ready for use. The default constructor constructs default objects! . 6.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!) .As_is Class object(parameters); . Common Error 6.4 Duh! . Common Error 6.5 Constructors don';t change variables. They are used implicitly to declare variables. They can also be used rather like constants in expressions: .As_is object = Class(); .As_is object = Class(parameters); Notice that the new value of the object overwrites the values in the old object. . Advanced Topic 6.1 -- initializing fields Come back to this next week. . Advanced Topic 6.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. . 6.7 Accessing data fields Would your watch work better if it had a hole in it and you could poke a knife into it? . 6.8 Comparing Member Functions with Normal Functions Member functions belong to classes. .As_is Class::function(arguments) They are called with objects. .As_is object.function(arguments) Normal functions are out side classes and are called without any object: .As_is function(argument) C++ gets confused if you muddle them up. . Productivity Hint 6.1 -- debugger Skip. . Quality Tip 6.1 Organize your files like this: .Box /* who did what, when and why */ include header files declare constants declare classes declare functions .Close.Box Productivity hint: write yourself a "fill-in-the-blanks" file. . Random Fact 6.1 - Productivity Read and inwardly digest. . 6.9 Separate Compilation . Random Fact 6.2 Art or science? So what do `you` mean by `art`? Don't you think that Einstein looks more like an artist than a lab-coated scientist? George Seurat: I just apply my method. Should an artist design a pace maker? Do scientists design things? .Close Chapter 6 .Close Classes