Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda Object Oriented Programming Reading: Chapter 14.

Similar presentations


Presentation on theme: "Agenda Object Oriented Programming Reading: Chapter 14."— Presentation transcript:

1 Agenda Object Oriented Programming Reading: Chapter 14

2 Object Oriented Programming C++ is an Object Oriented Programming language. So are Java, C#, VB, Smalltalk, and Objective-C (Mac/Iphone development). What does this mean? Now, instead of only working with simple data types like integers, doubles, characters, etc...we can make objects to represent anything we like. Do you want to keep a list of students and track their school progress? Make a Student class. Do you want to do math with X,Y coordinates? Make a Point class.

3 Object Oriented Programming So far we have seen the following data types: Integer (int) A whole number Double (double) A number with decimals Character (char) A single character String (string) A series of characters Boolean (bool) True or False

4 Object Oriented Programming There are many more types. There are also special, more abstract types, that are defined by classes or structures. C++ String An example of a class is a C++ string. A variable of data type string is really an object of type string. A class is a special data type. A variable declared with that data type is called an object of that data type.

5 Object Oriented Programming student Let's say you have a class called student. It may have the following properties: string firstName; string lastName; date birthDate; int id; schedule classSchedule; *date and schedule may be other classes

6 Classes, Objects, Structures, etc student could also have the following functions: void study (double hours); double getGrade(int courseNum); void register(int courseNum); To call any of these functions, we would declare a student variable and then type variable name dot function name, including any function parameters. student myStudent; myStudent.study(2.0);

7 Object Oriented Programming Classes use constructors to create the initial version of an object. For our student class, we might define a constructor (which is a function) that will set the id, first name, and last name of our student. If we had a constructor, we would use it like this: student myStudent (1,”John”, “Doe”); The code in the constructor function would assign the values passed in as parameters to their corresponding properties in the object.

8 Object Oriented Programming student is just an example of the many, many possibilities with classes. You can also make a student structure, which is slightly different from a class, but the same concept applies – you make your own “type” with its own properties.

9 Note about the book PLEASE NOTE: Your textbook is written for the standardized AP Computer Science exam. As a result, it utilizes some classes written specifically for the AP exam that are NOT available to you unless you include them. All of the code in the slides, and anything you find by looking at online documentation, will omit those classes. Please use what we use in class. For example, if you see apstring in the book, use string instead.

10 Object Oriented Programming Another example. We could make up a card class with the following properties and functions: Card char suit; char rank; char color; string getSuitName();

11 Private and Public Variables For our class, it could be useful to have some variables that code outside the class cannot see. These would be variables that we don't want to be changed manually. For example...if the suit of our card is changed, the color has to change, so it would be better to write a function to do that and not allow the programmer to change it from their code. Actually, most commonly, programmers make all class variables private and use functions to read or change their value. Variables that cannot be seen or modified from outside the class are called private variables. Variables that can be seen or modified from outside the class are called public variables.

12 Private and Public Functions Similarly, it may be useful to have functions that can only be used from within our class. Usually private functions are created when the function is only needed by another function in the class and really wouldn't serve a purpose to a programmer using our class. Example: we could write a function to synchronize the suit and color variables: void syncColor() { if (suit == 'D' || suit == 'H') { color = 'R'; } else { color = 'B'; }

13 Header Files While it is possible to approach this in many ways, it is good practice to have multiple files to separate your class code and program code. Example: PlayCards.cpp – The program you are writing that may use your class(es) Card.h – The code for the Card class definition: what private and public variables it has, the definitions of the private and public functions it has Card.cpp – The bodies (implementations) of the functions belonging to your Card class.

14 Constructors All classes should have at least one function that is used for initializing an object of that class. This function must have the same name as the class. It may have as few or as many parameters as you like. Note: Constructors do not return anything and should have no return type specified (not even void). Define a constructor like this: Card (); When we want to create a new object of our class, we would type (in our program's.cpp file): Card myCard(); If we wrote our constructor to accept parameters, we would include those between the parenthesis as with any function.

15 Constructors for our Card class Overloading Constructors You can overload a function by creating a second function with the same name that accepts different parameters. Here are two possible constructors for our card class. Card (); Card (char suit, char rank);

16 Class definition This is what a class definition would look like (in your.h file) class ClassName () { private: //List of private variables //List of private functions public: //Constructors //List of public functions }; Don't miss this semi-colon!

17 Accessor Functions - Getters and Setters Like I said, it is common practice to not make any class variables public. Instead, we write getter and setter functions for each. So our card class could have the following public functions: char getSuit(); char getRank(); void setSuit(char s); void setRank(char r);

18 Card Class Can you write the Card.h file? After that, can you write the Card.cpp file? After that, a program called DealCards.cpp that creates 7 card objects?


Download ppt "Agenda Object Oriented Programming Reading: Chapter 14."

Similar presentations


Ads by Google