Title: COM262: Object Development
1COM262 Object Development
- Implementation of Classes Relationships In C
2Implementation of Classes In C
Case Study A Library System - Class Diagrams
3ifndef BOOK_H define BOOK_H class Book
public void display() private
string title string author int
price int number_of_pages endif
//BOOK_H
/ Generated by Together /
4 ifndef USER_H define USER_H class User
public void displayBooks() void
borrowBook() private string name
string address endif //USER_H
/ Generated by Together /
5 ifndef LIBRARY_H define LIBRARY_H class
Library public void addBook()
void displayBooks() void
borrowBook() endif //LIBRARY_H
/ Generated by Together /
6Class Implementation
class Book private // this keyword does not
allows access to the //
following members by other objects string
title string author int
no_of_pages float price public //this keyword
allows other object to invoke the
//following operation on the objects of this
class Book (string bktitle, string aut, int
page, float pr) // constructor. Book ( ) //
2nd default constructor. Book( ) //
Destructor void display( )
instance variables
operations/methods
7Aspects of a C Class
class Book private // this keyword does not
allows access to the //
following members by other objects string
title string author int
no_of_pages float price public //this keyword
allows other object to invoke the
//following operation on the objects of this
class Book (string bktitle, string aut, int
page, float pr) // constructor. Book ( ) //
2nd default constructor. Book( ) //
Destructor void display( )
access modifiers
members
constructors
destructor
8Anatomy of an Object
interface (public)
initialise disable, access functions public
members,
CODE
private members
9Access Modifiers
- enable encapsulation
- data members should be accessible by objects own
operations in almost all cases - data members to be accessible by objects own
operations are designated as private - members that are to be accessible directly from
outside the object are defined as public - private, public and protected are access modifiers
10Constructors
- Constructors are member functions that return
initialised objects. They however do not have
return type like ordinary member functions. - Constructors have the same name as the class
- The most common use of constructors is for
initialising object instances, ie. giving its
instance variables values. - There can be more that one constructor for a class
11Destructor
- Destructors are mainly used for memory management
techniques. - A destructor does not have arguments.
- each object type can have only one destructor.
12Defining Member Functions
class Book private // this keyword does not
allows access to the //
following members by other objects string
title string author int
no_of_pages float price public //this keyword
allows other object to invoke the
//following operation on the objects of this
class Book (string bktitle, string aut, int
page, float pr) // constructor. Book ( ) //
2nd default constructor. Book( ) //
Destructor void display( )
these member functions must be defined
13Defining Member Functions
Syntax
return_type className methodName(parametersList
) // method statements
14Defining Member Functions
- return_type - any standard built-in types, user
defined types, void if method does not return any
values - className - name of the class
- - scope resolution
- parametersList - parameters which takes values
into the method. parameters have to be declared
of some type ie. - (type param1, type param2,etc)
- method statements - statements which achieve
the purpose of the method.
15Defining Member Functions
class Book private string title
string author int no_of_pages float
price public Book (string bktitle, string
aut, int page, float pr) Book ( )
Book( ) void display( )
void Bookdisplay() coutltltTitle
ltlttitleltltendl coutltltAuthor
ltltauthorltltendl coutltltPages
ltltno_of_pagesltltendl coutltltPrice
ltltpriceltltendl
16Defining Constructor
class Book private string title
string author int no_of_pages float
price public Book (string bktitle, string
aut, int page, float pr) Book ( )
Book( ) void display( )
BookBook (string bktitle, string aut, int page,
float pr) title bktitle
author aut no_of_Pagespage
price pr
BookBook ( )
17Defining Destructor
class Book private string title
string author int no_of_pages float
price public Book (string bktitle, string
aut, int page, float pr) Book ( )
Book( ) void display( )
BookBook( )
18Class Destructor
- a complement of a constructor
- can have at most one destructor
- called when an object is about to be destroyed
- name is the name of the class preceded by a
tilde, or squiggle character,
19Case Study A Library System - Class Relationship
20/ Generated by Together / class User ifndef
LIBRARY_H define LIBRARY_H include
"Book.h" class Library public void
addBook() void displayBooks() void
borrowBook() private / _at_link
aggregation _at_clientCardinality 1../ Book
lnkBook User lnkUser endif
//LIBRARY_H
21Class Relationship ImplementationAggregation
class User private string name
string address Book books_borrowed5
int noofBorrowedBooks public User (string nme,
string add) User ( ) User( ) void
displayBooks( ) void borrowBook(Book
aBook)
Class - User
22Class Relationship ImplementationAggregation
class Library private Book
theCollection20 int noofBooks public
Library ( ) Library( ) void addBook(
) void displayBooks( ) void borrowBook(User
aUser)
Class - Library
23Class Relationship Implementation Association
class Library private Book
theCollection20 int noofBooks public
Library ( ) Library( ) void addBook(
) void displayBooks( ) void borrowBook(User
aUser)
Class - Library
24Class Relationship Implementation Association
class Library ... public User newMember(
) .
Class - Library
25Interaction Diagram Methods implementation
26C Class - Summary
- facility to create automatic initialisation
functions through use of constructors - facility to clean up automatically as an object
goes out of scope via a destruction function - constructor(s) have the same name as the class
itself, and can have parameters lists like normal
functions - allow more than one constructor - differentiate
by a parameter list either in the number or types
of parameters
27C Class - Summary
- destructor is identified by the name of the class
preceded by the (tilda) character. The
destructor cannot have any parameters (so only
one destructor is allowed) - the constructor and destructor functions have no
type specifier, e.g. BookBook( ) instead of
void BookBook( ) since no return type is
expected - all members of a C class are private by
default - the public keyword is used to allow access to
member variables and functions whose declaration
follow it
28Object Development
A teaching block consists of several rooms
each with a unique room number and specified
seating capacity. Rooms may be booked on a
particular day for lectures. Each booking must
start on the hour and can be of any duration. It
must be possible to book a room in the teaching
block if it is free and generate a display of the
status of each room in the teaching block for a
particular day.
29Object Development
- Identify the major classes present.
- Identify their most important operations and
properties. - Construct a class diagram showing any
associations or aggregations. - Construct suitable instance diagrams
- Construct message trace diagrams showing
interactions between objects. - Construct a model showing how a room in the
teaching block can be booked.