Download Linked Lists

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Abstraction (computer science) wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Design Patterns wikipedia , lookup

Name mangling wikipedia , lookup

K-nearest neighbors algorithm wikipedia , lookup

Corecursion wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Object-oriented programming wikipedia , lookup

Class (computer programming) wikipedia , lookup

C++ wikipedia , lookup

C Sharp syntax wikipedia , lookup

Transcript
Simple Classes
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
C++ Classes
• Class
– A structured data type used to represent ADTs
• Class member – a component of a class
– data member
– function (method)
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.
Classes
• Are active in the sense that the data and
operations and bound together
• Data operates on itself and interacts with
other data
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.
struct Tdata {
int age;
double ht;
};
struct Node {
Tdata data;
Node * next;
};
Example
instantiate object A of type
List
int main() {
List A;
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;
};
return 0;
}
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.
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();
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.
Specification an Implementation
Like ADT’s there are two parts to classes
1. the specification – declare the class
2. implement the function definitions
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
Specification file (.h)
• contains class and struct declarations
implementation file (.cpp)
• contains class member function definitions
RECALL DEMO 1
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
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!
Revised class declaration
class TList
{
public:
void AddNode(int age, double ht);
//void CreateList();
do not need this
void DisplayList();
void DeleteNode(int N);
void DeleteList();
void DisplayNode(int N);
Tlist();
//constructor
~Tlist()
//destructor
private:
NodePtr front;
int nNodes;
};
Define constructor
TList::TList()
{
cout << “constructing a list…” << endl;
from = NULL;
nNodes = 0;
}
Define Destructor
TList::~TList()
{
cout << "Desctructing a List ..." << endl;
DeleteList();
}
DEMO 2 shows the active nature of classes
The constructor is called the moment an object is created
The destructor is called the moment the object is destroyed
(goes out of scope).
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.
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.
Summary continued
• It is usual practice to declare and implement
classes in separate files (specification and
implementation)
– Aids reuse
– Aids team development
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
Vehicle ADT
DATA:
Position
Speed
heading
Operation
Accelerate
Break
Turn
DisplayInfo
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
Bus ADT
bus is a kind of vehicle plus
DATA:
nPassengers
Operations:
board passenger
alight passenger
DisplayInfo
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
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
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.