Title: Object Implementation
1ObjectImplementation
- Implementation of Classes Relationships In C
2Implementation of Classes In C
Case Study A Library System - Class Diagrams
3/ Generated by Together / ifndef
BOOK_H define BOOK_H class Book public
void display() private string
title string author int price
int number_of_pages endif //BOOK_H
4/ Generated by Together / ifndef
USER_H define USER_H class User public
void displayBooks() void
borrowBook() private string name
string address endif //USER_H
5/ Generated by Together / ifndef
LIBRARY_H define LIBRARY_H class Library
public void addBook() void
displayBooks() void borrowBook() endi
f //LIBRARY_H
6Class Implementation
- So how are classes implemented in C?
- This can be achieved in two ways
- using the struct construct (similar to C)
- using C Class
7Class Implementation
class Book private // this keyword does not
allows access to the //
following members by other objects char
title40 char author25 int
no_of_pages float price public //this keyword
allows other object to invoke the
//following operation on the objects of this
class Book (char bktitle, char aut, int page,
float pr) // constructor. Book ( ) // 2nd
default constructor. Book( ) // Destructor
void display( )
instance variables
operations/methods
8Aspects of a C Class
class Book private // this keyword does not
allows access to the //
following members by other objects char
title40 char author25 int
no_of_pages float price public //this keyword
allows other object to invoke the
//following operation on the objects of this
class Book (char bktitle, char aut, int page,
float pr) // constructor. Book ( ) // 2nd
default constructor. Book( ) // Destructor
void display( )
access modifiers
members
constructors
destructor
9Anatomy of an Object
interface (public)
initialise disable, access functions public
members,
CODE
private members
10Access 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
11Constructors
- 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
12Destructor
- Destructors are mainly used for memory management
techniques. - A destructor does not have arguments.
- each object type can have only one destructor.
13Defining Member Functions
class Book private // this keyword does not
allows access to the //
following members by other objects char
title40 char author25 int
no_of_pages float price public //this keyword
allows other object to invoke the
//following operation on the objects of this
class Book (char bktitle, char aut, int page,
float pr) // constructor. Book ( ) // 2nd
default constructor. Book( ) // Destructor
void display( )
these member functions must be defined
14Defining Member Functions
Syntax
return_type className methodName(parametersList
) // method statements
15Defining 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.
16Defining Member Functions
class Book private char title40
char author25 int no_of_pages float
price public Book (char bktitle, char aut,
int page, float pr) Book ( )
Book( ) void display( )
void Bookdisplay() coutltltTitle
ltlttitleltltendl coutltltAuthor
ltltauthorltltendl coutltltPages
ltltno_of_pagesltltendl coutltltPrice
ltltpriceltltendl
17Defining Constructor
class Book private char title40
char author25 int no_of_pages float
price public Book (char bktitle, char aut,
int page, float pr) Book ( )
Book( ) void display( )
BookBook (char bktitle, char aut, int page,
float pr) strcpy(bktitle,title)
strcpy(aut,author)
no_of_Pagespage price pr
BookBook ( )
18Defining Destructor
class Book private char title40
char author25 int no_of_pages float
price public Book (char bktitle, char aut,
int page, float pr) Book ( )
Book( ) void display( )
BookBook( )
19Class 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,
20Case Study A Library System - Class Relationship
21/ 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
22Class Relationship ImplementationAggregation
class User private char name25
char address40 Book books_borrowed5
int noofBorrowedBooks public User (char nme,
char add) User ( ) User( ) void
displayBooks( ) void borrowBook(Book
aBook)
User
displayBooks borrowBook
name address
Class - User
23Class Relationship ImplementationAggregation
class Library private Book
theCollection20 int noofBooks public
Library ( ) Library( ) void addBook(
) void displayBooks( ) void borrowBook(User
aUser)
24Class Relationship Implementation Association
class Library private Book
theCollection20 int noofBooks public
Library ( ) Library( ) void addBook(
) void displayBooks( ) void borrowBook(User
aUser)
25Class Relationship Implementation Association
class Library ... public User newMember(
) .
26Interaction Diagram - Its relation to Methods
implementation
lib1
u1
u2
b1
b2
b3
b4
displayBooks
display
display
display
display
borrowBooku2
borrowBookb2
displayBooks
display
displayBooks
addBook
27C 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
28C 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
29Object 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.
30Object 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.