0% found this document useful (0 votes)
4 views34 pages

CEII Lecture 5

Uploaded by

alihalawa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views34 pages

CEII Lecture 5

Uploaded by

alihalawa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

EEC 124

Computer Engineering II

Lecture 5
Classes
Chapter 1 introduced the problem-solving methodology called object-oriented
design (OOD). In OOD, the first step is to identify the components, called
objects. An object combines data and the operations on that data in a single
unit. In C++, the mechanism that allows you to combine data and the operations
on that data in a single unit is called a class.

Computer Engineering II Dr. Mohamed A. Torad 2


Classes
Chapter 1 introduced the problem-solving methodology called object-oriented
design (OOD). In OOD, the first step is to identify the components, called
objects. An object combines data and the operations on that data in a single
unit. In C++, the mechanism that allows you to combine data and the operations
on that data in a single unit is called a class.

Computer Engineering II Dr. Mohamed A. Torad 3


Classes
Chapter 1 introduced the problem-solving methodology called object-oriented
design (OOD). In OOD, the first step is to identify the components, called
objects. An object combines data and the operations on that data in a single
unit. In C++, the mechanism that allows you to combine data and the operations
on that data in a single unit is called a class.

Computer Engineering II Dr. Mohamed A. Torad 4


Classes
Chapter 1 introduced the problem-solving methodology called object-oriented
design (OOD). In OOD, the first step is to identify the components, called
objects. An object combines data and the operations on that data in a single
unit. In C++, the mechanism that allows you to combine data and the operations
on that data in a single unit is called a class.

Computer Engineering II Dr. Mohamed A. Torad 5


Unified Modeling Language Class Diagrams

Computer Engineering II Dr. Mohamed A. Torad 6


Classes
from now on we will use the term class object, or simply object, for a
class variable. The syntax for declaring a class object is the same as that
for declaring any other variable. The following statements declare two
objects of type clockType:

The general syntax for an object to access a member of a class is:


Computer Engineering II Dr. Mohamed A. Torad
classObjectName.memberName
7
Classes

Computer Engineering II Dr. Mohamed A. Torad 8


Built-in Operations on Classes
Most of C++’s built-in operations do not apply to classes. You cannot use
arithmetic operators to perform arithmetic operations on class objects (unless
they are overloaded; see Chapter 15).

The two built-in operations that are valid for class objects are member access
(.) and assignment (=).

(For example, if myClock is a clockType object, in the statement myClock.


incrementSeconds();, myClock accesses the member incrementSeconds.)

Computer Engineering II Dr. Mohamed A. Torad 9


Functions and Classes

Computer Engineering II Dr. Mohamed A. Torad 10


Functions and Classes
In C++, you can pass a variable by reference and still prevent the function from
changing its value by using the keyword const in the formal parameter
declaration. As an example, consider the following function definition:

Computer Engineering II Dr. Mohamed A. Torad 11


Implementation of Member Functions

Computer Engineering II Dr. Mohamed A. Torad 12


let us give the definitions of the other member functions of the class clockType

Computer Engineering II Dr. Mohamed A. Torad 13


Computer Engineering II Dr. Mohamed A. Torad 14
Computer Engineering II Dr. Mohamed A. Torad 15
Accessor and Mutator Functions
We can categorize the member functions of the class clockType into two
categories:
• member functions that modify the member variables
• member functions that only access, and do not modify, the member
variables.

Accessor function: A member function of a class that only accesses (that is,
does not modify) the value(s) of the member variable(s).

Mutator function: A member function of a class that modifies the value(s) of


the member variable(s).

Computer Engineering II Dr. Mohamed A. Torad 16


Computer Engineering II Dr. Mohamed A. Torad 17
Computer Engineering II Dr. Mohamed A. Torad 18
Computer Engineering II Dr. Mohamed A. Torad 19
Constructors
when we printed the value of yourClock without calling the function setTime,
the output was some strange numbers (see the output of Line 5 in the sample
run).

Because the private members of a class cannot be accessed outside of the class
(in our case, the member variables), if the user forgets to initialize these
variables by calling the function setTime, the program will produce erroneous
results.

To guarantee that the member variables of a class are initialized, you use
constructors. There are two types of constructors: with parameters and
without parameters. The constructor without parameters is called the default
constructor.

Computer Engineering II Dr. Mohamed A. Torad 20


Constructors

Computer Engineering II Dr. Mohamed A. Torad 21


Constructors

Computer Engineering II Dr. Mohamed A. Torad 22


Constructors

Computer Engineering II Dr. Mohamed A. Torad 23


Constructors
We can write the definition of the constructor with parameters by calling the
function setTime, as follows:

Recall that when a class object is declared, a constructor is automatically


executed. Because a class might have more than one constructor, including the
default constructor, next we discuss how to invoke a specific constructor.

Computer Engineering II Dr. Mohamed A. Torad 24


Invoking a Constructor with Parameters

Computer Engineering II Dr. Mohamed A. Torad 25


Invoking a Constructor with Parameters

Computer Engineering II Dr. Mohamed A. Torad 26


Invoking a Constructor with Parameters

Computer Engineering II Dr. Mohamed A. Torad 27


Invoking a Constructor with Parameters

Computer Engineering II Dr. Mohamed A. Torad 28


Constructors and Default Parameters

Computer Engineering II Dr. Mohamed A. Torad 29


Classes and Constructors: A Precaution

Computer Engineering II Dr. Mohamed A. Torad 30


Arrays of Class Objects (Variables) and Constructors

Computer Engineering II Dr. Mohamed A. Torad 31


Arrays of Class Objects (Variables) and Constructors

Computer Engineering II Dr. Mohamed A. Torad 32


Arrays of Class Objects (Variables) and Constructors

Computer Engineering II Dr. Mohamed A. Torad 33


Destructors

Computer Engineering II Dr. Mohamed A. Torad 34

You might also like