18
Agenda Object Oriented Programming Reading: Chapter 14

Agenda Object Oriented Programming Reading: Chapter 14

Embed Size (px)

Citation preview

Page 1: Agenda Object Oriented Programming Reading: Chapter 14

Agenda

Object Oriented Programming

Reading: Chapter 14

Page 2: Agenda Object Oriented Programming Reading: Chapter 14

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.

Page 3: Agenda Object Oriented Programming Reading: Chapter 14

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

Page 4: Agenda Object Oriented Programming Reading: Chapter 14

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.

Page 5: Agenda Object Oriented Programming Reading: Chapter 14

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

Page 6: Agenda Object Oriented Programming Reading: Chapter 14

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);

Page 7: Agenda Object Oriented Programming Reading: Chapter 14

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.

Page 8: Agenda Object Oriented Programming Reading: Chapter 14

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.

Page 9: Agenda Object Oriented Programming Reading: Chapter 14

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.

Page 10: Agenda Object Oriented Programming Reading: Chapter 14

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();

Page 11: Agenda Object Oriented Programming Reading: Chapter 14

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.

Page 12: Agenda Object Oriented Programming Reading: Chapter 14

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';

}

}

Page 13: Agenda Object Oriented Programming Reading: Chapter 14

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.

Page 14: Agenda Object Oriented Programming Reading: Chapter 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.

Page 15: Agenda Object Oriented Programming Reading: Chapter 14

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);

Page 16: Agenda Object Oriented Programming Reading: Chapter 14

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!

Page 17: Agenda Object Oriented Programming Reading: Chapter 14

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);

Page 18: Agenda Object Oriented Programming Reading: Chapter 14

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?