Lect02 CPP Class
Lect02 CPP Class
2022
Contents
1. What is an object?
2. What is a class?
3. OO Design
5. Technical Terms
6. Workshop
What is an object?
What Is an Object?
What is an
object?
What is a
class?
OO Design
Classes and
Concept 1
Objects in
C++ An object is an entity with a well-defined boundary and identity that
Technical encapsulates state and behavior.
Terms
• State is represented by attributes and relationships.
Workshop
• Behavior is represented by operations, methods, and state machines.
Conceptual entity
Table
PK uniqueId
Soffware entity row 1
row 2
row 3
4
An Object Has State
What is an
object?
What is a
class?
OO Design
• State is a condition or situation during the life of an object, which satisfies
Classes and
Objects in
C++
some condition, performs some activity, or waits for some event.
Technical • The state of an object normally changes over time.
Terms
Name: J Clark
Employee ID: 567138
Date Hired: July 25, 1991
Status: Tenured
Discipline: Finance
Maximum Course Load: 3 classes
5
An Object Has Behavior
What is an
object?
What is a
class?
OO Design
• Behavior determines how an object acts and reacts.
Classes and
Objects in
C++
• The visible behavior of an object is modeled by a set of messages it can
Technical respond to (operations that the object can perform).
Terms
Workshop
6
An Object Has Identity
What is an
object?
What is a
class?
OO Design
• Each object has a unique identity, even if the state is identical to that of
Classes and
Objects in
C++
another object.
Technical
Terms
Workshop
7
What is a class?
What Is a Class?
What is an
object?
What is a
class?
OO Design
Classes and
Concept 2
Objects in
C++ A class is a description of a set of objects that share the same attributes,
Technical operations, relationships, and semantics. An object is an instance of a class.
Terms
Workshop
9
What Is a Class? (cont.)
What is an
object?
What is a
class?
OO Design
• A class is an abstract definition of an object.
Classes and
Objects in
C++
• It emphasizes relevant characteristics.
Technical
• It suppresses other characteristics.
Terms • It defines the structure and behavior of each object in the class.
Workshop
• It serves as a template for creating objects.
• Classes are not collections of objects.
10
Representing Classes in the UML
What is an
object?
What is a
class?
OO Design
A class is represented using a rectangle A class can be represented using a sim-
with three compartments: ple rectangle:
Classes and
Objects in
C++
• The class name
Technical
Terms
• The attributes SimpleClass
Workshop
• The operations
FullClass
attribute1
attribute2
operation1()
operation2()
11
What Is an Attribute/Operation?
What is an
object?
What is a
class?
OO Design
Workshop
• A class may have any number of attributes or no attributes at all.
Concept 4
An operation is the implementation of a service that can be requested from any
object of the class to affect behavior. In other words, an operation is an
abstraction of something you can do to an object that is shared by all objects of
that class.
• A class may have any number of operations or none at all.
12
OO Design
Object-oriented design
What is an
object?
What is a
class?
OO Design
• Abstract Data Types (ADT)
Classes and
Objects in
C++
• Divide project into a set of cooperating classes
Technical • Each class has a very specific functionality
Terms
14
Mapping the real world to software
What is an
object?
What is a
class?
OO Design
Technical
Terms Animal
Workshop
Attributes
Truck
Behaviors
Human
15
Designing process
What is an
object?
What is a
class?
OO Design
Identifying classes
Classes and
Objects in • Abbott and Booch:
C++
Technical
• use nouns, pronouns, noun phrases to identify objects and classes
Terms • note: not all nouns are really going to relate to objects
Workshop
• Coad and Yourdon:
• identify individual or group "things" in the system/problem
• Ross:
• common object categories: people, places, things, organizations,
concepts, events
16
Designing process (cont.)
What is an
object?
What is a
class?
OO Design
Identifying behaviors
Classes and
Objects in • Decide whether behavior is accomplised by a single class or through the
C++
Technical
collaboration of a number of “related” classes
Terms
• Static behavior: behavior always exists
Workshop
• Dynamic behavior: depending of when/how a behavior is invoked, it might or
might not be legal
• Look for verbs in problem description
17
Example 1
What is an
object?
What is a
class?
OO Design
Game “Tetris”, possible classes:
Classes and
Objects in • Board
C++
18
Example 2
What is an
object?
What is a
class?
OO Design
• e-Shopping Website, possible classes:
Classes and
Objects in
C++
Product
Technical
• Attributes: Name, ID, price, status, manufacturer’s name, images,
Terms
technical description.
Workshop
Product Category
• Attributes: Name
Manufacturer
• Attributes: Name, Country, Website
19
Example 3
What is an
object?
What is a
class?
OO Design
Website “National Foolball Competition”
Classes and
Objects in • People: Player, Referee, Coach, Team Manager…
C++
20
Example 4
What is an
object?
What is a
class?
OO Design “Joe’s Automotive Shop services foreign cars and specializes in servicing
Classes and
Objects in
cars made by Mercedes, Porsche, and BMW. When a customer brings
C++
a car to the shop, the manager gets the customer’s name, address, and
Technical
Terms telephone number. The manager then determines the make, model, and
Workshop year of the car and gives the customer a service quote. The service quote
shows the estimated parts charges, estimated labor charges, sales tax, and
total estimated charges.”
• Finding the classes
21
Classes and Objects in C++
Class declaration in C++
What is an
object?
What is a
class?
OO Design
Scope
• private: only visible to the class itself.
• public: can be use from inside of the class or any client outside
23
UML
What is an
object?
What is a
class?
OO Design
• Using symbol
Classes and
Objects in
C++
Name
Technical
Terms -attribute1 symbol - means private
Workshop -attribute2
symbol + means public
+operation1()
+operation2()
• Using icon
Name
attribute1 icon square means private
attribute2
icon circle means public
operation1()
operation2()
24
An example
What is an
object?
What is a
class?
OO Design
• A class models a date
Classes and
Objects in
C++ class CDate {
Technical
Terms
private :
int m_iDay , m_iMonth , m_iYear ;
Workshop
public :
CDate ();
int getDay (); // return day
int getMonth (); // return month
int getYear (); // return year
...
};
25
An example (cont.)
What is an
object?
What is a
class?
OO Design
• A class models a human
Classes and
Objects in
C++ class Human {
Technical // Data attributes :
Terms
private :
Workshop
string Name;
string DateOfBirth ;
string PlaceOfBirth ;
string Gender ;
// Methods :
public :
void Talk( string TextToTalk );
void IntroduceSelf ();
...
};
26
Scope resolution operator ::
What is an
object?
What is a
class?
OO Design
• The two colons :: are called the scope resolution operator.
Classes and
Objects in
C++
• It identifies methods or attributes as members of a certain class
Technical • For example:
Terms
Workshop
CDate::getDay()
CDate::getMonth()
27
Separation declaration from definition
What is an
object?
What is a
class?
OO Design
Classes and
Objects in
// keep in one file // header file
C++ class CDate { class CDate {
Technical private : private :
Terms
... ...
Workshop
public : public :
int getDay (); int getDay ();
... ...
}; };
28
Inline Member Functions
What is an
object?
What is a
class?
OO Design
Workshop
// keep in one file
class CDate {
private :
...
public :
int getDay () {
return m_iDay ;
}
...
};
29
Using const with Member Functions
What is an
object?
What is a
class?
OO Design
• When the key word const appears after the parentheses in a member
Classes and
Objects in
C++
function declaration, it specifies that the function will not change any data
Technical
stored in the calling object.
Terms
30
Instantiating an Object of a Class
What is an
object?
What is a
class?
OO Design
• Instantiating an object of type class Human is similar to creating an
Classes and
Objects in
C++
instance of another type, say double:
Technical double Pi = 3.1415;
Terms
Human Tom;
Workshop
Human Jerry = Human ();
31
Accessing Members Using the Dot Operator . and
What is an
object?
What is a
class? Pointer Operator ->
OO Design
Classes and
Objects in
C++
• The dot operator (.) helps us access attributes or methods of an object.
Technical
Terms
Human Tom;
Workshop
...
Tom. IntroduceSelf ();
• If an object has been instantiated on the free store using new or if we have a
pointer to an object, then we use the pointer operator (->) to access the
member attributes and functions
What is a
class?
OO Design
33
Encapsulation and data hiding
What is an
object?
What is a
class?
OO Design
• Encapsulation:
Classes and
Objects in
C++
A C++ class provides a mechanism for packaging data and the operations
Technical
that may be performed on that data into a single entity
Terms
• Information Hiding:
Workshop
A C++ class provides a mechanism for specifying access
34
Technical Terms
Taxonomy of member functions
What is an
object?
What is a
class?
OO Design
A common taxonomy:
Classes and
Objects in • Constructor/Initalization: an operation that creates a new instance of a
C++
Technical
class
Terms • Constraint Checking methods?
Workshop
• Observer: an operation that reports the state of the data members (aka
Accessors, Getters)
• Provides value of an internal attribute
• Provides some value calculated from internal attributes only
• Provides some value calculated from internal attributes AND some
external parameter(s)
36
Taxonomy of member functions (cont.)
What is an
object?
What is a
class?
OO Design
• Mutator: an operation that changes the state of the data members of an
object (aka Setters)
Classes and
Objects in
C++
• Updates value of an internal attribute
Technical
Terms • Transforms values of internal attributes
Workshop • Constraint Checking methods?
• Iterator: an operation that allows processing of all the components of a data
structure sequentially
37
Workshop
Quiz
What is an
object?
What is a
class?
OO Design
Classes and
1. What is the difference between a class and an instance of the class?
Objects in
C++
.........................................................................
Technical
.........................................................................
Terms .........................................................................
Workshop
2. What is the default access specification of class members?
.........................................................................
.........................................................................
.........................................................................
3. Is it a good idea to make member variables private? Why or why not?
.........................................................................
.........................................................................
.........................................................................
39
Exercises
What is an
object?
What is a
class?
OO Design
Design and implement the following classes:
Classes and
Objects in • Date
C++
40
References
Deitel, P. (2016).
C++: How to program.
Pearson.
Gaddis, T. (2014).
Starting Out with C++ from Control Structures to Objects.
Addison-Wesley Professional, 8th edition.
Jones, B. (2014).
Sams teach yourself C++ in one hour a day.
Sams.