0% found this document useful (0 votes)
15 views

Lect02 CPP Class

The document discusses objects, classes, and object-oriented design. It defines an object as an entity with state and behavior, and a class as a description of objects that share attributes and operations. It provides examples of mapping real-world things to software and the process of identifying classes and behaviors in object-oriented design.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lect02 CPP Class

The document discusses objects, classes, and object-oriented design. It defines an object as an entity with state and behavior, and a class as a description of objects that share attributes and operations. It provides examples of mapping real-world things to software and the process of identifying classes and behaviors in object-oriented design.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

CLASSES AND OBJECTS

Bùi Tiến Lên

2022
Contents

1. What is an object?

2. What is a class?

3. OO Design

4. Classes and Objects in C++

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.

• Informally, an object represents an entity, either physical, conceptual, or


software.
Physical entity

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

Workshop Professor Clark’s attributes

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

Professor Clark’s behaviors

Submit Final Grades


Accept Course Offering
Set Max Load

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

Professor "J Clark" Professor "J Clark"


teaches Biology teaches Biology

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

Classes and Concept 3


Objects in
C++ An attribute is a named property of a class that describes the range of values
Technical that instances of the property may hold.
Terms

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

Workshop • Think of a class as similar to a data type


• Class can be used to create instances of objects

14
Mapping the real world to software
What is an
object?

What is a
class?

OO Design

Classes and Real world Abstraction Software


Objects in
C++

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++

Technical • Block (square block)


Terms
• Piece (composed of several blocks)
Workshop
• Player (is it necessary?)
• Line of Blocks

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++

Technical • Places: Stadium, City…


Terms
• Things: Ball (is it necessary?)
Workshop
• Organizations: Team, National Football Association
• Concepts: Half, Round, Season…
• Events: Match (is this a concept or an event?), Goal

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

Classes and Syntax


Objects in
C++ class <Name of the class>
Technical {
Terms
private:
Workshop
<private attributes and methods>
public:
<public attributes and methods>
};

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 ();
... ...
}; };

int CDate :: getDay () { // implementation file


return m_iDay ; int CDate :: getDay () {
} return m_iDay ;
}

28
Inline Member Functions
What is an
object?

What is a
class?

OO Design

Classes and Concept 5


Objects in
C++ When the body of a member function is written inside a class declaration, it is
Technical declared inline
Terms

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

Workshop class CDate {


private :
...
public :
int getDay () const {
return m_iDay ;
}
...
};

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 ();

• Alternatively, we would dynamically allocate for an instance of class Human


as we would an int using new:
int* pNumber = new int;
delete pNumber ;
Human* pAnotherHuman = new Human ();
delete pAnotherHuman ;

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

Human* pTom = new Human ();


...
pTom -> IntroduceSelf ();
delete pTom;
32
Data hiding
What is an
object?

What is a
class?

OO Design

Classes and int main () {


Objects in
C++
CDate today;
CDate tomorrow , someDay ;
Technical
Terms cout << today. m_iMonth ; // compile error
Workshop cout << today. getMonth (); // OK
...
}

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++

Technical • Fraction with numerator and denominator


Terms
• Employee
Workshop

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.

You might also like