You are on page 1of 27

Lecture 9:

Introduction on Object Oriented


Programming (OOP)

Dr. Fadi Alzhouri

Concordia University

Created by Rose Gomar, modified and presented by Fadi Alzhouri


Textbook (Copyright)

“C++ How to Program”, 10th edition, by Harvey and


Paul Deitel Print ISBN: 0-13-444823-5, Chapter 9,
10.

10/22/2022 2
What we learn in Lecture 9
• Introduction on OOP
• Classes
• Objects
• Encapsulations
• Member function
• Attributes
• Constructors

10/22/2022 3
Introduction

• Object-oriented programming (OOP)


• It is programming that favors using objects rather than
mathematical concepts.
• An object represents a thing in the real world that can be clearly
identified. For example, car, bus, and motorcycle.
• An object has attributes and behaviors. Object

• Encapsulates data (attributes) and functions (behavior) into attributes


packages called classes. behaviors

• Information hiding
• Class objects communicate across well-defined interfaces
• Implementation details hidden within classes themselves

4
What is a Class?

• A class is an abstract representation of the objects that


have the same attributes and the same behavior.
• A class is a construct that defines objects of the same
type.
• A class uses variables to define data fields (attributes)
and functions to define behaviors.
• Classes allow you to create new data types unknown to
C++
• E.g, the class Account can be used to represent all the bank
accounts of a certain bank
• An object of the class Account can be created as follows:
Account a;

5
What is a Class? (Cont.) Class name

• User-defined classes

Data

Constructors

Operations

• Similar to blueprints – reusable semicolon


• Object: a class instance

6
How to Create a Class in C++?

• A class consist of 4 main parts


• Data members
• Used to represent the attributes of objects
• Of any type, including classes already defined, pointers to objects of any
type, or references to objects of any type
• Constructor
• A function used to create objects and initialize member variables of an
object
• Member functions
• Used to represent the behavior of objects (a.k.a. methods)
• Destructor (optional)
• A function which is invoked when an object is destroyed (i.e., removed from
memory)

7
Classes and Objects
class
Circle

+radius: float

+Circle()

+getArea(): float

circle1: Circle circle2: Circle

radius=10.5 radius=14.25

Objects

8
Classes and Objects (cont.)

• Class
• Defined using keyword class
• Member access specifiers
• public:
• Accessible everywhere in the program
• private:
• Accessible only by member functions of the class that define the
members

9
Encapsulation

• Member access specifiers private and public control access to


data members and member functions of the class
• private:
• Class’s private members can be accessed only by member functions of that
class

• They can not be used outside the class. In other words, they are
encapsulated or hidden

• public:
• Class’s public members can be accessed outside the class

10
Class Definition and Access Specifiers

11
Constructors

• A constructor is to create and initialize data members of objects

• A constructor is invoked each time an object of the class that


defines the constructor is created (instantiated)

• The name of a constructor must be the same as the name of the


class that defines it
• E.g.,
Time(); // Default constructor

• A constructor does not have a return-type, not even void

12
Constructor Function

• Special member function


• Initializes data members

• Same name as class

• Called when object instantiated

• Several constructors

• Function overloading

• No return type

13
Data Members

• They represent the attributes of objects


• They can be of any data types (basic data types or user defined
data types)
• A data member is usually hidden from outside the class using the
keyword private
• Example of data members of class Time
private:
int hour; // 0 - 23
int minute; // 0 - 59
int second; // 0 - 59

14
Member Functions

• Define what the class does

• They are also called methods. They have the same


syntax as functions we have seen before

• Member functions are usually defined as public


• Because they describe what we can do with an object, they
need to be accessed from outside the class

• Example of a member function on next slide

15
Member Functions

• Member functions defined outside a class - or inside


the class(we will see later)
• Binary scope resolution operator (::)
• “Ties” member name to class name

• Uniquely identify functions of a particular class

• Format for defining member functions outside the class:


ReturnType ClassName::MemberFunctionName( )
{

}

16
Example of a Member Function

17
What is an Object?

• An object can represent any real world entity


• E.g., bank account, student, house, geometrical circle, etc.

• An object has attributes and a behavior


• E.g., a bank account can have the following attributes: account
number, account type (checking or saving), an owner, a
balance, etc.
• The behavior describes what we can do with an object
• E.g., we can withdraw money from a bank account, deposit money, transfer
money from one account to another, etc.

18
Creating Objects

• The action of creating an object is referred to as Class


instantiation

• The constructor of the class is automatically called after each


instantiation

• Create two objects t1 and t2 of type Time:


Time t1; // invoke constructor of Time
Time t2; // invoke constructor of Time

• Memory representation of t1 and t2:


t1 t2
hour 0 hour 0
minute 0 minute 0
second 0 second 0

19
Creating Objects

• Objects of a class are like variables for normal data


types
• Objects are also called instances
• Example
Time sunset; // object of type Time
Time arrayOfTimes[ 5 ]; // array of Time objects

20
Accessing Member Functions

• Member functions allow you to access the private data members of


a class
• E.g., the member function setTime of the class Time
• Other member functions
int getHour(); // return the hours
int getMinute(); // return the minutes
int getSecond(); // returns the seconds

21
Definition of Class Time

Line 6: Definition of class begins with keyword class. Class body starts with left brace
Line 10-12: Function prototypes for public member functions
Line 15-17: private data members accessible only to member functions.
Line 8 and 14: Member access specifiers

30
Implementation of Class Time (cont.)

Line 24: Constructor initializes private data members to 0


Line 34: public member function checks parameter values for
validity before setting private data members.

31
Implementation of Class Time (cont.)

Line 43 and 52: No arguments (implicitly “know” purpose is to print


data members); member function calls more concise.

32
Using the Class Time
Declare variable t to be
object of class Time.

Invoke public member


functions to print time.

Set data members using


public member function.

Attempt to set data


members to invalid
values using public
member function.

Invoke public member


functions to print time.

33
Using the Class Time (cont.)
The initial universal time is 00:00:00
The initial standard time is 12:00:00 AM
Data members set to 0 after
Universal time after setTime is 13:27:06 attempting invalid settings.
Standard time after setTime is 1:27:06 PM

After attempting invalid settings:


Universal time: 00:00:00
Standard time: 12:00:00 AM

34
Specification – Implementation

• Separating specification from implementation


• Use a header file (.h file) to describe the class
• E.g. time.h can contain the description of the class Time

• Use a .cpp file to describe the implementation of the member


functions of the class
• E.g., time.cpp can contain the member function of class Time

• The file time.cpp must include time.h

• Any other cpp file that you create can use the class Time if you
include both time.h and time.cpp

35

You might also like