Lecture 9 - Introduction On OOP - 1
Lecture 9 - Introduction On OOP - 1
Concordia University
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
• Information hiding
• Class objects communicate across well-defined interfaces
• Implementation details hidden within classes themselves
4
What is a Class?
5
What is a Class? (Cont.) Class name
• User-defined classes
Data
Constructors
Operations
6
How to Create a Class in C++?
7
Classes and Objects
class
Circle
+radius: float
+Circle()
+getArea(): float
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
• 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
12
Constructor Function
• Several constructors
• Function overloading
• No return type
13
Data Members
14
Member Functions
15
Member Functions
16
Example of a Member Function
17
What is an Object?
18
Creating Objects
19
Creating Objects
20
Accessing Member Functions
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.)
31
Implementation of Class Time (cont.)
32
Using the Class Time
Declare variable t to be
object of class 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
34
Specification – Implementation
• Any other cpp file that you create can use the class Time if you
include both time.h and time.cpp
35