rossella lau lecture 6, dco10105, semester b,2005-6 dco10105 object-oriented programming and design ...

22
Rossella Lau Lecture 6, DCO10105, Semester B,2005-6 DCO10105 Object-Oriented Programming and Design Lecture 6: More on class construction UML and an introduction to inheritance More on class construction • Multiple inclusion • Use of accessor and mutator • Function overload and signature • Members of pointer • The resolution operator :: and namespace UML and an introduction to inheritance • UML: Class diagram • Some class diagrams in the real world • Some class diagrams for typical applications -- By Rossella Lau

Post on 20-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Rossella Lau Lecture 6, DCO10105, Semester B,2005-6

DCO10105 Object-Oriented Programming and Design

Lecture 6: More on class construction UML and an introduction to inheritance

More on class construction• Multiple inclusion• Use of accessor and mutator• Function overload and signature• Members of pointer• The resolution operator :: and namespace

UML and an introduction to inheritance• UML: Class diagram• Some class diagrams in the real world• Some class diagrams for typical applications

-- By Rossella Lau

Rossella Lau Lecture 6, DCO10105, Semester B,2005-6

#inndef _INT_ARRAY_H#define _INT_ARRAY_H......#endif

These are pre-processor statements to avoid multiple inclusion

The pre-processor uses a symbol _INT_ARRAY_H to indicate that the body within #inndef -- #endif, including _INT_ARRAY_H, will be defined if it is not defined yet

When the same header file is included the second time, since it is already defined, the body will not be included

Revisit IntArray.h & IntArray.cpp

Rossella Lau Lecture 6, DCO10105, Semester B,2005-6

The dynamic array -- pointer

The dynamic array *array actually is a pointer

Its main purpose is to get dynamic memory allocation for the array

However, whenever you have a pointer variable, it complicates the constructors and destructors and usually you must define your own rather than using those generated by the compiler

In other words, for simple classes such as clockType and Qudratic, we can use the compiler generated constructors and destructors

Rossella Lau Lecture 6, DCO10105, Semester B,2005-6

The identifier starting with _

Identifiers of data members in IntArray start with _ (an underscore)

Traditionally, C defines system (library functions) variables with a prefix “_” to avoid conflict with programmer defined variable names

Rossella Lau Lecture 6, DCO10105, Semester B,2005-6

Use of accessor and mutator

Although a member can access any other members in a class, it is a good habit to use accessor and mutator in service functions and use mutator to set the value for the data in the constructor if the respective data does have a domain checking and maintaining in the mutator; e.g., DateType.cpp

I.e., Data members are usually only directly referenced in constructors, accessors, and mutators; recheck: IntArray.cpp & Quadratic.cpp

Rossella Lau Lecture 6, DCO10105, Semester B,2005-6

Function overload Several constructor ids and identical function ids can

exist in the same class or the same program IntArray(int), IntArray(IntArray const &) fillData(double[],size_t), fillData(int[],size_t)

Same function ids with different signatures are allowed in modern languages

Return type & parameters of a function are the function’s signature

When a function call is encountered, the compiler checks the type(s) of its parameter(s), the signature, and finds the function with such signature to call

Rossella Lau Lecture 6, DCO10105, Semester B,2005-6

:: and namespace

:: identifies a member of a class (. identifies a member of an object)

:: can also identify members in namespace

Namespace: Malik’s slide: 8:18-23

Same symbol but different application You cannot use “using namespace IntArray” to save the

typing of “IntArray::” for the function implementation in IntArray.cpp

Rossella Lau Lecture 6, DCO10105, Semester B,2005-6

UML

Unified Modeling Language

Software blueprint language Similar to blueprints, standard graphical language, for the

architects and builders

There are nine kinds of modeling diagrams: Use Case diagrams, Class diagrams, Object diagrams,

Sequence diagrams, Collaboration diagrams, Statechart diagrams, Activity diagrams, Component diagrams, and Deployment diagrams

Reference: www.borland.com

Rossella Lau Lecture 6, DCO10105, Semester B,2005-6

Class diagram

It gives an overview of a system by showing the relationships among them

Association: relationship between instances of two classes• It is a link in a diagram

Aggregation: an association in which one class belongs to a collection

• It is a link with a diamond end pointing to the part containing the whole

Generation: indicating inheritance between classes• It is a link with a triangle end pointing to the base class

from sub classes

Rossella Lau Lecture 6, DCO10105, Semester B,2005-6

Navigation and multiplicity

In the above class diagram, it uses navigability arrow to represent the relationship rather than the standard

It shows which direction the association can be traversed

Multiplicity shows the possible instances for an aggregation; e.g.,

An order has at least one order item A catalog has zero or many products

Rossella Lau Lecture 6, DCO10105, Semester B,2005-6

An example: customer orders

http://bdn.borland.com/article/0,1410,31863,00.html#classdiagrams

Rossella Lau Lecture 6, DCO10105, Semester B,2005-6

Class notation

A 3-piece rectangle: E.g., Malik’s Figure 12-1

Class name

Attributes

Operations

Rossella Lau Lecture 6, DCO10105, Semester B,2005-6

Visibility and scope

http://bdn.borland.com/article/images/31863/classdiagram.html

Rossella Lau Lecture 6, DCO10105, Semester B,2005-6

Introduction to inheritance

A sub class has the attributes and properties (operations) of its base class

A sub class inherits everything from its base class in OO view: all the data members and function members

It is quite similar to class taxonomy or class classification in the real world

• Animals can be classified as: mammal, bird, fish; there are different kinds of mammals: man, pet, wild animal, farm animal, whale, etc

Rossella Lau Lecture 6, DCO10105, Semester B,2005-6

Real world example : CityU

Rossella Lau Lecture 6, DCO10105, Semester B,2005-6

Real world example : Animal

Rossella Lau Lecture 6, DCO10105, Semester B,2005-6

Application: The shapes

Rossella Lau Lecture 6, DCO10105, Semester B,2005-6

Application: Course management

Rossella Lau Lecture 6, DCO10105, Semester B,2005-6

A class diagram for gourmetCoffee

Rossella Lau Lecture 6, DCO10105, Semester B,2005-6

Other typical applications

A restaurant system

A retail system

A course management system

A library system

A payroll system

Rossella Lau Lecture 6, DCO10105, Semester B,2005-6

Summary

In this class, we revisited class construction with some related concepts and techniques: pre-processor’s supports, pointer member and class, good programming practice in using accessor and mutator, and :: & namespace

The class diagram of the UML helps in modeling a system

The class diagram also introduces the relationship of inheritance

Rossella Lau Lecture 6, DCO10105, Semester B,2005-6

Reference

Malik: 8.3, 12.1.1, 13

http://bdn.borland.com/article/0,1410,31863,00.html

-- END --