cpp13 - object orientation

25
Objects Michael Heron

Upload: michael-heron

Post on 01-Nov-2014

63 views

Category:

Software


4 download

DESCRIPTION

This is an introductory lecture on C++, suitable for first year computing students or those doing a conversion masters degree at postgraduate level.

TRANSCRIPT

Page 1: CPP13 - Object Orientation

ObjectsMichael Heron

Page 2: CPP13 - Object Orientation

Introduction• Over the past few weeks we have been working towards an

understanding of the syntax of C-type languages.• Loops and selections

• In this lecture and the next three, we are going to look at a special way in which we can put together programs in C++.• It is called Object Oriented Programming.

Page 3: CPP13 - Object Orientation

Object Orientation• Object orientation is a programming technique by which

functionality gets broken out into little, self contained objects.• C++ is an object oriented language, as is Java

• Syntactically, objects are a little different from the code we’ve seen so far.

• Conceptually, they are substantially different.• Need to think about programs in a different way.

Page 4: CPP13 - Object Orientation

What is Object Orientation?• Object orientation works on the principle of classes and

objects.• We write classes in our code, and then create objects from them.• They are something like writing your own data type.

• Like an int or string• But much more powerful.

• Good object design is hard to do.• That’s not our concern for now.

Page 5: CPP13 - Object Orientation

What is a Class?• Think of a class as a blueprint.• In C++, a class defines the following things:• What variables a class possesses.• What functions exist in that class.

• Classes are structural explanations for the compiler.• They tell the compiler what kind of things we are dealing with.

• They do not exist in your code.• Until you create an object.

Page 6: CPP13 - Object Orientation

What is an object?• An object is a specific instance of a class.• In technical terms, we say we instantiate an object when we

create it.• The class tells the object what variables it has.• The object contains the state of those variables.

• Think back to the idea of a class as a blueprint.• We send that blueprint into a factory, and out comes objects

based on that blueprint.• It’s not the easiest idea to get in your head…• It may take some time.

Page 7: CPP13 - Object Orientation

In The Abstract• Think of a chair.• A chair has a certain structure to it, that’s how we recognise

chairs.• It has legs• It may have a back• It may have arms• It will be made of some kind of material• It will be a colour of some kind

• We don’t know in advance what the value of any of these traits will be.• We only know when we create a specific chair – an object.

Page 8: CPP13 - Object Orientation

Why Object Orientation?• Writing programs is hard.• Useful to be able to break it down into its components.• Object orientation allows us to take a ‘divide and conquer’

approach to programming.• It’s important to maximize benefit from code.• Useful to be able to reuse it.

• They allow us to group together variables and the functions that act on them.• More on that later….

Page 9: CPP13 - Object Orientation

Object State• The state of an object is the value of all its variables.• The functions permitted in an object are usually those needed

to allow the object to function.• Consider a CD player class.• What functions are we likely to need to make it work?

• We don’t need to know, when designing an object, how it does it.• Black box mentality.

Page 10: CPP13 - Object Orientation

Writing a Class in C++• The process of writing a class in C++ is slightly awkward.• First we create a header file for the class definition.

• This allows us to prototype the class in the same way we do with functions.

• Then we need to create an implementation file for the code.• We keep the two separate to aid in portability of code.

Page 11: CPP13 - Object Orientation

The Class Definition• The class definition goes into a header file.• A .h file

• We have seen these before• But they have always been written for us.

• This is a file we include into our programs using the #include directive.

• All this file contains is the structure of our class.• No code.

Page 12: CPP13 - Object Orientation

The Class Definitionclass Car {private: float price;

public: void set_price (float p); float query_price();};

We use the special keyword class here – the name of our class is Car.

Ignore the public and private stuff just now – we’ll talk about that properly tomorrow.

Note that we end the structure with a semi-colon – that’s new.

Page 13: CPP13 - Object Orientation

The Class Implementation• We always put variables in our classes under the private part

of the code.• We’ll talk about why tomorrow.

• We put functions under the public part of the code.• What we are saying to C++ is:• We have a class called Car• It contains a float called price.• It contains two functions – set_price and query_price.

Page 14: CPP13 - Object Orientation

The Object Implementation• However, we also need to provide the code to drive these

functions.• C++ doesn’t do it for us.

• For this we create a second file – a cpp file.• This is not the same thing as the program we usually create.

• We write these functions in exactly the same way we have in the past.• Except…

Page 15: CPP13 - Object Orientation

Scope Resolution• The functions we have written so far have all been

unstructured.• They didn’t belong to an class.

• The functions we are writing now belong to a class.• As such, we need to tell C++ to which class they belong.

• This requires us to use a new operator.• The scope resolution operator.

Page 16: CPP13 - Object Orientation

The Class Implementation#include "car.h”

void Car::set_price (float p) { price = p;}

float Car::query_price() { return price;}

Note that we don’t have a main method here.

Our class sits there idly until we actually create an object from it.

We do that in our actual program (which looks much like it did before)

Page 17: CPP13 - Object Orientation

Our Main Program#include <iostream>#include "car.h”

using namespace std;

int main() { Car myCar;

myCar.set_price (100.0);

cout << "You car is worth: " << myCar.query_price() << endl; return 0;}

We use the dot notation to access the functions that have been defined as part of our class.

Here we create an object called myCar from the class Car we wrote earlier.

Page 18: CPP13 - Object Orientation

Right… why?• This class is not very powerful.• It really just contains a single variable and some methods for

acting on that variable.• The more functions and variables we provide, the more

powerful our objects become.• By breaking out this from our main program, our code

becomes easier to understand.• We communicate between our objects via the messages passed

through function calls.

Page 19: CPP13 - Object Orientation

Okay, but why?• Objects give us a mechanism by which we can begin to model

connections between separate parts of a system.• This is not easy otherwise.

• Imagine you needed to store an array of X,Y and Z co-ordinates.• How would you do that?

• Imagine you needed to express a relationship between universities, students and modules?• How would you do that?

Page 20: CPP13 - Object Orientation

A Bigger Example - Headerclass Account {private: int balance; int overdraft;public: int query_balance(); void set_balance (int); int query_overdraft(); void set_overdraft (int); void adjust_balance (int); };

Page 21: CPP13 - Object Orientation

A Bigger Example - CPP#include "Account.h”

void Account::set_balance (int v) { balance = v;}int Account::query_balance() { return balance;}void Account::set_overdraft (int v) { overdraft = v;}int Account::query_overdraft() { return overdraft;}

bol Account::adjust_balance (int v) { if (balance - v < 0 - overdraft) { return false; }

set_balance (balance - v); return true;}

Page 22: CPP13 - Object Orientation

A Bigger Example - Program#include <iostream>#include "Account.h"

using namespace std;

int main() { Account ob; int amount; bool success; cout << "How much would you like to withdraw?" << endl; cin >> amount; ob.set_balance (200); ob.set_overdraft (100); success = ob.adjust_balance (amount); if (success) { cout << "Transaction successful" << endl; } else { cout << "Transaction failed" << endl; } return 0;}

Page 23: CPP13 - Object Orientation

Object Orientation• Object orientation is a tremendously powerful way of writing

programs.• But regardless of what anyone says, it’s not an easy way of

writing programs.• It will take some time before the use becomes apparent.• Especially if you have some non OO programming experience.

• Almost all modern languages incorporate object orientation.• It’s the dominant way of programming these days.

Page 24: CPP13 - Object Orientation

Object Orientation• Think of objects as an especially powerful kind of variable.• One that contains functions as well as data.

• The object is your variable.• The class is your variable type.

• Most of the theoretical aspects of object orientation you can ignore for now.• That’s handy, because there are so very many of them.

Page 25: CPP13 - Object Orientation

Summary• New way of programming!• Object orientation.

• It’s tremendously powerful.• That power comes at the cost of simplicity.

• You won’t see why just yet.• It will take some time before it becomes obvious why object

orientation is a great way to program.• We’ll be talking more about this over the coming lectures.