2cpp03 - object orientation fundamentals

22
OBJECT ORIENTATION FUNDAMENTALS Michael Heron

Upload: michael-heron

Post on 01-Nov-2014

75 views

Category:

Software


2 download

DESCRIPTION

This is an intermediate conversion course for C++, suitable for second year computing students who may have learned Java or another language in first year.

TRANSCRIPT

Page 1: 2CPP03 - Object Orientation Fundamentals

OBJECT ORIENTATION FUNDAMENTALSMichael Heron

Page 2: 2CPP03 - Object Orientation Fundamentals

Introduction• In this lecture we are going to look at the structure and

syntax of an object oriented program in C++.• In theory, it is the same as in Java.

• We shall also cover briefly the things that you should remember from your previous exposure to the idea of OOP.• We’ll go into it all in much more detail as the course progresses.

Page 3: 2CPP03 - Object Orientation Fundamentals

Reminder of Concepts• The basic building blocks of an Object Oriented program

are classes and objects.• Classes define a blueprint, or structure• Objects define the state.

• An object is an instantiation of a class.• The class tells the object what structure and data fields it should

have.• The object contains the value of those data fields.

Page 4: 2CPP03 - Object Orientation Fundamentals

Reminder of Concepts• An Object Oriented program combined objects together to

achieve some total goal.• Each object represents one part of a subdivision of labour.

• You can think of a class as a complex, user-defined data type.

• You can create multiple objects from a single class.• Usually

Page 5: 2CPP03 - Object Orientation Fundamentals

OOP Good Practise• Methods and Attributes in an Object Oriented program

have degrees of visiboility.• Variables should almost always be private.

• They are modified through public accessor methods that you define.

• Methods should have the level of visibility that minimises the impact of change associated with them.• More on this later, it’s an important concept.

Page 6: 2CPP03 - Object Orientation Fundamentals

Declaring A Class (Java)public class Car { private double price; private String colour;

public void setPrice (double p) { price = p; }

public double getPrice() { return price; }

public void setColour (String c) { colour = c; }

public String getColour() { return colour; }}

Page 7: 2CPP03 - Object Orientation Fundamentals

Declaring a Class (C++)#include <iostream>

using namespace std;

class Car {private: float price; string colour;

public: void set_price (float p); float query_price(); void set_colour (string c); string query_colour();};

Page 8: 2CPP03 - Object Orientation Fundamentals

Differences• Visibility declared in groups in C++

• Defined at the method/variable level in Java

• Class declaration ends with a semi-colon in C++• No such syntactic requirement in Java

• Difference in naming conventions.• Not enforced by the compiler, but an adopted stylistic convention.

• Code definitions separated from code declarations.

Page 9: 2CPP03 - Object Orientation Fundamentals

A C++ Class• A C++ class usually exists in two files.

• The header file defining the class declaration.• That’s what we just say.

• The code file defining the method bodies.

• In Visual Studio, the wizards will handle this separation for you.• Other IDEs will most likely not.

• C++ uses the scope resolution operator (::) to define code as belonging to a member function of a class.

Page 10: 2CPP03 - Object Orientation Fundamentals

Car Code File#include "car.h"

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

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

void Car::set_colour (string c) { colour = c;}

string Car::query_colour() { return colour;}

Page 11: 2CPP03 - Object Orientation Fundamentals

Instantiating an Object in C++• In Java, this was done using the new keyword.• In C++, we also use new, but we also must declare a

pointer to the object we are about to create:• When we have a pointer to an object, we use a ->

operator to access defined methods.• Variables too, but don’t do that.• This is analagous to the dot operator in Java.

• Be careful – C++ also has a dot operator.• It doesn’t work work with pointers.

Page 12: 2CPP03 - Object Orientation Fundamentals

Using A Class#include <iostream>#include "car.h”

int main() { Car *my_car;

my_car = new Car();

my_car->set_price (100.0);

cout << my_car->query_price() << endl; return 1;}

Page 13: 2CPP03 - Object Orientation Fundamentals

Instantiating An Object (2)• You can also instantiate objects without using pointer

notation.• You access the methods and variables using the dot notation, just

as in Java.• You don’t use the new keyword, you just declare the variable.

• The object then exists on the stack.

• When working with this object though, you will be working with the value of it.• This is somewhat limiting, as you cannot make changes to the

state of object outside of its context.

Page 14: 2CPP03 - Object Orientation Fundamentals

Instantiating An Object (2)#include <iostream>#include "car.h"

void set_value (Car, float);

int main() { Car my_other_car; my_other_car.set_colour ("blue"); set_value (my_other_car, 100.0); cout << my_other_car.query_colour() << endl; cout << my_other_car.query_price() << endl; return 1;}

void set_value (Car myCar, float val) { myCar.set_price (val);}

Page 15: 2CPP03 - Object Orientation Fundamentals

Classes and Pointers• The most flexible way to work with objects in C++ (and the

way that will most closely map onto your experience with objects in Java) is through pointers.

• The output for the program shown the slide before would be:

blue 0.0

Page 16: 2CPP03 - Object Orientation Fundamentals

Classes and Pointers#include <iostream>#include "car.h"

void set_value (Car*, float);

int main() { Car* my_car; my_car = new Car(); my_car->set_colour ("blue");

set_value (my_car, 100.0);

cout << my_car->query_colour() << endl; cout << my_car->query_price() << endl; return 1;}

void set_value (Car *myCar, float val) { myCar->set_price (val);}

Page 17: 2CPP03 - Object Orientation Fundamentals

Java Memory Management• One of the features of java is the garbage collector.

• Essentially it does all the cleaning up of objects that are no longer in use in a program.

• It means you don’t need to explicitly destroy objects once you are done with them.• The garbage collector will get to them eventually.

• Usually

• C++ requires you to destroy pointers to objects when you are done with them.• Or they stay in memory forever.

• Until the program finishes execution.

Page 18: 2CPP03 - Object Orientation Fundamentals

C++ Memory Management• Whenever you are completely finished with an object in

C++, you must delete it.• This frees up the memory that it had previously been assigned.• From that point on, the memory is null memory.

• You’ll get an error message if you try to access it.

• The delete keyword is used to free up memory:

delete my_car;

Page 19: 2CPP03 - Object Orientation Fundamentals

Pointers and Objects• Those of you who are especially observant may have

noticed some differences in the way we work with pointers when dealing with primitive data types and with objects.• There’s no inconsistency really, it’s just a consequence of how they

are defined.

• Remember what our two pointer operators actually do.• * means ‘the value of’• & means ‘the address of’

Page 20: 2CPP03 - Object Orientation Fundamentals

Pointers in Objects• When we declare a class as a pointer, we are saying ‘we

are working with the value of this memory location’• When we pass it into the function, we are already working

with a pointer.• Thus we do not need to use the & operator when passing the

parameter.

• Within the function, we can use the variable name without the * operator.• The -> operator works directly on sections of memory.

Page 21: 2CPP03 - Object Orientation Fundamentals

Pointers in C++• This can be tremendously confusing.

• It does become clear as time goes by, but expect this to be one of the hurdles you must overcome to be a confident C++ programmer.

• As a result of this, the tutorial exercises tomorrow are on pointers and pointer manipulation.• Wear your thinkin’ caps!

Page 22: 2CPP03 - Object Orientation Fundamentals

Summary• In the abstract, C++ classes work just like Java classes.

• There are syntactical and structural differences.

• The issue is further complicated by C++’s reliance on pointers.• Very powerful, but very confusing to begin with.

• Remember that Java does a fair degree of ‘administration’ for you.• C++ is not quite so thoughtful!