Top Banner
Simple Classes
28

Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Dec 14, 2015

Download

Documents

Merilyn Small
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Simple Classes

Page 2: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

ADTs• A specification for a real world data item

– defines types and valid ranges– defines valid operations on the data.

• Specification is problem dependent.• e.g. A list

– Data• Name and height

– Operations• create empty lists• add item• remove item• add ten feet to every height • remove all records whose height is greater than 5 feet• etc

Page 3: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

C++ Classes

• Class– A structured data type used to represent ADTs

• Class member – a component of a class– data member– function (method)

Page 4: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Recall Structs

• Separates implementation of data from operations– Review Linked list demo last week

• Structs are said to be passive in the sense they just sit there and wait for something else to do something to them.

Page 5: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Classes

• Are active in the sense that the data and operations and bound together

• Data operates on itself and interacts with other data

Page 6: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Class types

• Like a struct a class is a programmer defined type.

• It is not a data object.

• You create or instantiate an object of a type.– Think of the class type like a stamp– and the objects are those things you stamp out.

Page 7: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Examplestruct Tdata {

int age;

double ht;

};

struct Node {

Tdata data;

Node * next;

};

typedef Node * NodePtr;

class TList

{

public:

void AddNode(int age, double ht);

void CreateList();

void DisplayList();

void DeleteNode(int N);

void DeleteList();

void DisplayNode(int N);

private:

NodePtr front;

int nNodes;

};

int main() {List A;

return 0;}

instantiate object A of typeList

Page 8: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Public and Private• If you look closely at the previous definition of the List class you

may note one or two things• class definition is similar to struct definition

– In fact C++ treats structs as classes where by default all members are public

• class definition contains function prototypes– the operations are bound tightly to the data– class functions know about class data so you don’t need to pass class data to

functions as arguments

• class definition has a line with a label public: this tells the compiler which parts of the class are visible to other things (clients) e.g main()

• class members by default are private. That is Private members functions and data are hidden to all.

• Private data and functions are only visible an object itself.

Page 9: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Class Data Manipulation

• The principle of Information hiding is the mantra for object oriented programming. Access to data is tightly controlled via public interface functions– “Setters” to set data members

• SetHeight(3);

– “Getters” to obtain values of data members• y = mylist.GetHeight();

Page 10: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Built in operators

• int, char, float, double have built in operators– + * / % etc

– classes do not have them

• like structs they have . (dot) and =• You have to define and implement your own

operators– It is possible but beyond this course to redefine the

built in operators to work on YOUR class.

Page 11: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Specification an Implementation

Like ADT’s there are two parts to classes

1. the specification – declare the class

2. implement the function definitions

Page 12: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Usual Practice• Put the class specification in a file

– e.g. Tlist.h

• Put implementation in another file– e.g. Tlist.cpp

• Add both files to a project that wants to use a Tlist. Add the .h to the headers folder.– Now the project “knows” that there are more files to compile.

• Add a line #include “Tlist.h” to your main source file– Now main “knows” about Tlist classes

DEMO 1

Page 13: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Specification file (.h)

• contains class and struct declarations

• contains class member function definitions

implementation file (.cpp)

RECALL DEMO 1

Page 14: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

DEMO1• Shows practical elements of object oriented programming .

– Information hiding• Via class syntax• Via implementation over multiple files

• Loose coupling– Classes are very self contained– There only interaction with other parts of a project is via its public interface

which can be strictly monitored– They very independent to other parts of your project– They can therefore be developed and tested in isolation

• They result in more robust code• They are easy to reuse/ adapt/extend/

– They are ideal for team programming• For large project they are more productive

– They are ideal for large programs

Page 15: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Constructors and Destructors

• Constructors guarantee initialisation

• We ALWAYS want to set up a list to use.

• Special member function called a constructor will “construct” or initialise any instance of our class exactly how we want.

• The constructor is called at the time of object instantiation!

Page 16: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Revised class declaration

class TList {public:

void AddNode(int age, double ht);//void CreateList(); do not need thisvoid DisplayList();void DeleteNode(int N);void DeleteList();void DisplayNode(int N);Tlist(); //constructor~Tlist() //destructor

private:NodePtr front;int nNodes;

};

Page 17: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Define constructor

TList::TList()

{

cout << “constructing a list…” << endl;

from = NULL;

nNodes = 0;

}

Page 18: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Define Destructor

TList::~TList()

{

cout << "Desctructing a List ..." << endl;

DeleteList();

}

DEMO 2 shows the active nature of classesThe constructor is called the moment an object is createdThe destructor is called the moment the object is destroyed(goes out of scope).

Page 19: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Purpose of constructor and destructor

• They ensure that objects created are properly initialised• They ensure that they are properly destroyed

– So they can sort out any garbage collection, for example memory de-allocation.

• Once this is done properly then class can be used more reliably.

Page 20: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Summary• A class is used to implement an ADT• A class specifies members - the data and operations

(methods)• Information hiding is a central idea to classes• class members are private by default• Interface functions are explicitly labelled as public• Classes provide automatic initialisation through constructors • Classes provide a place for garbage collection through

destructors.• Creation of an object is achieved via a declaration statement.

Page 21: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Summary continued

• It is usual practice to declare and implement classes in separate files (specification and implementation)– Aids reuse– Aids team development

Page 22: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Further Ideas

• C++ and other object oriented languages take things further.

• For example

• Suppose we had a problem modelling vehicles

• We need an ADT for a vehicle

Page 23: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Vehicle ADT

DATA:PositionSpeedheading

OperationAccelerateBreakTurnDisplayInfo

Page 24: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Suppose we want to model a bus

• A bus is a kind of vehicle

• It would be good if we could make use of out vehicle implementation and extend it to include bus specific data members and operations

Page 25: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Bus ADT

bus is a kind of vehicle plus

DATA:

nPassengers

Operations:

board passenger

alight passenger

DisplayInfo

Page 26: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

This can be done using inheritance• A class can inherit data and operations from another class• It can also redefine (polymorph) operations so that they

work with the new class– e.g DisplayInfo will need to be adapted to display bus specific

information as well as vehicle information.

• This process of inheritance further extends the programmers ability to reuse and extend code already written.

• These principles, lie at the heart of OOP– Information hiding– Inheritance and polymorphism

Page 27: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Object Oriented Programming

• Identify what the goals are for a problem• Identifying the objects that make up a

problem• Identifying how the object in a problem

interact with each other in order to satisfy the goals

• Specifying ADTs that describe the objects and operations needed for the problem

Page 28: Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

JAVA• The second year module in JAVA takes these

concepts further• JAVA is a “pure” object oriented language with a

distinclt C++ flavoured Syntax• It is pure in the sense you cannot program in Java

without classes.