Lec16 - Lecture 16: Classes – Lecture Overloading and...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture 16: Classes – Lecture Overloading and Files PIC 10A Todd Wittman Overloading Last class we mentioned you can define a function with several different parameter choices. This is called overloading a function. overloading void drawMap ( ); void drawMap void drawMap (Point TheShire); void drawMap (Point TheShire void drawMap (Point TheShire, Point Moria); void drawMap (Point TheShire Point Moria void drawMap (string mapName); void drawMap (string mapName When we call the function drawMap, iit picks the drawMap t function with matching number of parameters and parameter types. and 1 Operator Overloading We can also overload an operator, which defines operations like overload which +-*/>< for a class. Recall we had an is_better_than function for the Product class. is_better_than function Remember this compared the “bang-for-the-buck” ratios of the ratios the Products next and best. the bool Product :: is_better_than (Product b) const { Product is_better_than if (b.price == 0) return false; if (price == 0) return true; return score/price > b.score/b.price; return b.score/b.price } We used it like this: We if (next.is_better_than (best) ) Operator Overloading Similarly, we could define the operator > for the Product class. class. bool Product :: operator> (Product b) const { if (b.price == 0) return false; if (price == 0) return true; return score/price > b.score/b.price; return b.score/b.price } Then we could call with: Then if (next > best) To be thorough, you could also define <, >=, <=. To We saw operator overloading with + for the string class. We string name = “Frodo “ + string2; string Frodo 2 Sec 5.7: Accessing Data Fields Only the member functions can access the private fields of a class. Only A member function just uses the variable directly, it’s already declared member already in the class private section. These are called implicit variables. implicit Recall the Point class has private variables x, y and a move function. Recall The explicit variables dx, dy have to be passed to the function. explicit dy class Point { public: void move (double dx, double dy) const; public: dx double dy private: double x, y; }; void Point::move (double dx, double dy) const { void Point::move (double dx double dy x = x + dx; dx dx, dy are explicit variables -- to be used they have to be passed to the function y = y + dy; dy return; x, y are implicit variables -- they can be accessed directly by all member functions } Sec 5.8: Member vs. Non-member Functions To prevent a member function from changing an implicit parameter, add the word const to the function. const void Point::move (double dx, double dy) void Point::move (double dx double dy Can change the implicit parameters x, y void Point::move (double dx, double dy) const void Point::move (double dx double dy Can’t make changes to x, y For non-member functions, we have to add an & to change For member to the value of an explicit parameter. the void move (double dx, double dy) void dx double dy Can’t change explicit value parameters dx, dy change dx dy void move (double& dx, double& dy) void dx double& dy Can change explicit reference parameters dx, dy Can dx dy 3 Member vs. Non-member Functions Explicit Parameter Value Parameter (cannot change) Default: Changes to Default: a passed parameter are not sent back are Reference Parameter Pass with a & to Pass send back any (can change) changes changes Implicit Parameter Use const after Use const after function to prevent changes changes Default: Private Default: variables can be changed by any member function member Operator Overloading As another example, let's define the <= sign to compare two Rectangles based on their area. compare r1 r2 r1 <= r2 The declaration should look like: The bool operator<= (Rectangle r2) const; 4 File Layout Last class, we saw how to declare classes all in one file: Last Included header files Included Constants Constants Global variables Global Class declarations Class Class member functions Class Functions Functions One file to rule them all! Main routine Main Note: This is how you’re expected to set up your file for HW 5. Note But this makes your cpp file rather long and complicated. cpp And it would be nice to re-use the classes you made in other And use programs. programs. We can put the class information in a separate header file. We Sec 5.9: Separate Compilation Sec 5.9 The source file (.cpp) contains the basic program. The Definitions of global variables Definitions Non-member functions Non The main routine The The header file (.h) contains the definition of the classes we use. The se. The header file sets up and supports your program. Some useful things you might want to separate from the source file: things Definition of constants Definition Declaration of classes Declaration Definition of member functions Definition Declaration of non-member functions Declaration Declaration of global variables Declaration Then this header file can be used for other programs. Then For example, you’ve used “ccc_win.h” in your graphics programs. For ve 5 Using Header Files The source file includes the header file at the top: The #include “filename.h” #include The basic header file looks like: The #ifndef FILENAME_H #define FILENAME_H #include <libraries> **YOUR CLASSES HERE** #endif The ifndef statements prevent multiple inclusion. ifndef So even if this header file is included by more than one program, it will still only be compiled once. program, Typical File Setup Put the main routine and its non-member functions in the main source cpp file. Put Class declarations in Class.h (or whatever class name is). Put Class member function definitions in Class.cpp (same name). main.cpp (e.g. hw6.cpp) Class.h Class.cpp #include “Class.h” void fun( ) { ... } int main( ) { .... } #ifndef CLASS_H #define CLASS_H class Class { ... }; #endif #include “Class.h” Class::Class ( ) { ... } void Class::fun ( ) { ... } •Note both main.cpp and Class.cpp #include the Class header file. •Let’s take a quick peek at your ccc graphics files... 6 Header Files for Declarations Even if we don’t use classes, it’s common programming practice to Even common declare functions in a separate header file. declare So in hw5.h, we might have: So void DrawMap( ); void DrawMap double distance2points (Point x, Point y); double distancePointLine (Line xy, Point a); double distancePointLine (Line xy For your HW5, you could create such a file with just these 3 lines. For es. (Don’t do this! I’m just saying that you theoretically could do it.) (Don Then in the source file hw5.cpp, we define the functions as normal. Then al. Remember to add to the top: Remember #include “hw5.h” #include A curious programmer can just look at your header file to see what curious at functions it contains and whether it would be of any use to her. functions Really Global Variables Suppose we want a variable to be available to all source files using that library. source For example, your graphics programs all used the cwin object for drawing. cwin Declare it as an external variable in your .h file. external extern GraphicWindow cwin; extern GraphicWindow The associated source file has the definition: The GraphicWindow cwin; The variable cwin can be used by all the functions cwin can in all the files. One variable to rule them all! One 7 Art vs. Science Do you have to use header files to declare functions? No, but it’s a llittle more aesthetically ittle functions? pleasing than throwing everything into one file. pleasing The last thing in Chapter 6 is a very philosophical question: Is computer programming an art or a science? Is It’s a llittle both. Your homework program has to ittle It compile, but you also get graded on style. compile, You should be proud of your artwork. You can eYou mail your executable files to your family and friends. mail Let your parents know what their tuition checks are paying for. paying 8 ...
View Full Document

This note was uploaded on 04/27/2010 for the course PIC 157-050-21 taught by Professor Wittman during the Fall '10 term at UCLA.

Ask a homework question - tutors are online