1 introduction to classes and objects chapter 3 introduction to classes and objects chapter 3
TRANSCRIPT
![Page 1: 1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649f155503460f94c2b4aa/html5/thumbnails/1.jpg)
1
Introduction to Classes and Objects
Chapter 3
Introduction to Classes and Objects
Chapter 3
![Page 2: 1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649f155503460f94c2b4aa/html5/thumbnails/2.jpg)
2
Class vs Object
Blueprint House
Class Object
![Page 3: 1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649f155503460f94c2b4aa/html5/thumbnails/3.jpg)
3
Class Definition/InterfaceGradeBook.h
Function prototypes
Forgetting ; is a syntax error
![Page 4: 1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649f155503460f94c2b4aa/html5/thumbnails/4.jpg)
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
![Page 5: 1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649f155503460f94c2b4aa/html5/thumbnails/5.jpg)
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
![Page 6: 1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649f155503460f94c2b4aa/html5/thumbnails/6.jpg)
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
![Page 7: 1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649f155503460f94c2b4aa/html5/thumbnails/7.jpg)
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() {…}
![Page 8: 1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649f155503460f94c2b4aa/html5/thumbnails/8.jpg)
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();
![Page 9: 1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649f155503460f94c2b4aa/html5/thumbnails/9.jpg)
9
Main Program (Client Code)Testing Class GradeBook
![Page 10: 1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649f155503460f94c2b4aa/html5/thumbnails/10.jpg)
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
![Page 11: 1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649f155503460f94c2b4aa/html5/thumbnails/11.jpg)
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
![Page 12: 1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649f155503460f94c2b4aa/html5/thumbnails/12.jpg)
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
![Page 13: 1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649f155503460f94c2b4aa/html5/thumbnails/13.jpg)
13
The Compilation and Linking Process
![Page 14: 1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649f155503460f94c2b4aa/html5/thumbnails/14.jpg)
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
![Page 15: 1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649f155503460f94c2b4aa/html5/thumbnails/15.jpg)
15
UML Diagrams
![Page 16: 1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649f155503460f94c2b4aa/html5/thumbnails/16.jpg)
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
![Page 17: 1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649f155503460f94c2b4aa/html5/thumbnails/17.jpg)
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.
![Page 18: 1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649f155503460f94c2b4aa/html5/thumbnails/18.jpg)
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.
![Page 19: 1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649f155503460f94c2b4aa/html5/thumbnails/19.jpg)
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
![Page 20: 1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649f155503460f94c2b4aa/html5/thumbnails/20.jpg)
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.
![Page 21: 1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649f155503460f94c2b4aa/html5/thumbnails/21.jpg)
21
Class Diagram for the ATM System Model
![Page 22: 1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649f155503460f94c2b4aa/html5/thumbnails/22.jpg)
22
Class Diagram Showing Composition Relationship of a Class Car