Post on 04-Jan-2016
transcript
1
Introduction to Classes and Objects
Chapter 3
Introduction to Classes and Objects
Chapter 3
2
Class vs Object
Blueprint House
Class Object
3
Class Definition/InterfaceGradeBook.h
Function prototypes
Forgetting ; is a syntax error
4
Class Implementation FileGradeBook.cpp
Define class member functions in separate
source-code file
Including the header file causes the class definitions
to be copied into the file
5
Constructor and Default Constructor
• C++ requires a constructor call for each object created– No return type, not even void– Compiler provides default constructor when a
constructor is not explicitly provided
• Default constructor– Constructor with no parameters– Two ways to provide default constructor
• The compiler implicitly creates a default constructor in a class without a constructor (may contain garbage value for its data members)
• The programmer explicitly defines a constructor that takes no arguments
6
Data Member and Member Function
• Data members– Variables declared inside a class definition but outside the
class’s member function body– Represents the attributes of the class– Each object has its own copy of data members
• Member functions– Get functions (accessors) to access private data members– Set functions (mutators) to change the values of private data
members– All objects of the same class share one copy of each member
function
7
Scope Resolution Operator
• Scope resolution operator (::) is used to define the member functions outside the class
• A token to allow access to static members of a class
• Scope resolution operator is preceded by a class name
Example: void GradeBook::setCourseName(string name) {…}string GradeBook:getCourseName() {…}
8
Dot Member Selection Operator
• Dot operator (.) is used to access a member of an object
• Preceded by an object name
• ExamplegradeBook1.setCourseName(“COMP1520: C++”);
gradeBook1.displayMessage();
9
Main Program (Client Code)Testing Class GradeBook
10
Class Diagram for GradeBook Class
Minus indicates a private member
Plus indicates public members
Name of the class in boldface
Class attributes
Class operations
11
Separating Interface from Implementation
• Interfaces– Define and standardize the ways in which things interact with each other– Describes what services class’s client can use and how to request those services– Not how the class carries out the services– Consists only a class’s public members
• Why separate interface from the implementation– Ensures programmers do not write client code that depends on the class’s
implementation details– Client codes are less likely to break if the implementation changes– The class is reusable– The clients of the class know what member functions the class provides, how to
call them and what return types to expect– The clients do not know how the class's member functions are implemented
• How– Defining a class’s interface with function prototypes
• Function prototypes describes the class's public interface without revealing the class's member function implementations
– Defining member functions in a separate source-code file
12
How Header Files Are Located
• When the preprocessor encounters a header file name in quotes (e.g., "GradeBook.h"), the preprocessor
– Attempts to locate the header file in the same directory as the file in which the #include directive appears.
– If failed, searches for it in the same location(s) as the C++ Standard Library header files.
• When the preprocessor encounters a header file name in angle brackets (e.g., <iostream>), it
– assumes that the header is part of the C++ Standard Library
13
The Compilation and Linking Process
14
Unified Modeling Language (UML)
• General-purpose modeling language
• Create abstract model of a system using graphical notation
• Used to specify, visualize, construct, and document software-intensive systems
• Allow software developers to concentrate more on design and architecture
15
UML Diagrams
16
Software Engineering Case Study: ATM Class Diagram
• Determine the classes used in the system– ATM– Screen– Keypad– Cash dispenser– Deposit slot– Account– Bank database– Balance inquiry– Withdrawal– Deposit
17
Representing a class in the UML using a class diagram
Name of the class
Class’s attributes
Class’s operations
Compartments can be suppressed to create more readable diagrams.
18
Association between Classes
Solid line represents association between classes
Multiplicity value
One object of class ATM executes zero or one objects of class Withdrawal.
19
Multiplicity Types
Symbol Meaning
0 None
1 One
m An integer value
0..1 Zero or one
m, n m or n
m..n At least m, but not more than n
* Any nonnegative integer (zero or more)
0..* Zero or more (identical to *)
1..* One or more
20
Composition Relationship
Composition relationship or “has-a”
relationship
1. The diamond can be placed on only one end of the association line.2. The whole is responsible for creating and destructing its parts.3. A part may belong to only one whole at a time.
An ATM has a screen, a keypad, a cash dispenser and a deposit slot.
21
Class Diagram for the ATM System Model
22
Class Diagram Showing Composition Relationship of a Class Car