Академический Документы
Профессиональный Документы
Культура Документы
April 8, 2018
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 1
Objectives
After you have read and studied this chapter, you should be
able to
• Understand Structures and its use
• Name the basic components of object-oriented programming
• Differentiate classes and objects.
• Differentiate class and instance methods.
• Differentiate class and instance data values.
• Draw program diagrams using icons for classes and objects
• Describe significance of inheritance in object-oriented
programs
• Name and explain the stages of the software lifecycle
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 2
1
4/8/2018
Structures
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 3
Structures
• A Structure is a container, it can hold a bunch of things.
– These things can be of any type.
• In contrast, array is homogeneous since it can contain only data of the same
type.
• Syntax:
struct identifier { };
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 4
2
4/8/2018
Structures examples
Examples:
Student record: student id, name, major, gender, start year, …
Bank account: account number, name, currency, balance, …
Address book: name, address, telephone number, …
In database applications, structures are called records.
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 5
struct basics
Definition of a structure:
struct <struct-type>{
Each identifier
<type> <identifier_list>;
defines a member
<type> <identifier_list>; of the structure.
...
};
Example:
struct Date {
The “Date” structure
int day;
int month; has 3 members,
int year; day, month & year.
};
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 6
3
4/8/2018
Example:
struct StudentRecord {
char Name[22];
int Id; The “StudentRecord” structure
char Dept[22]; has 4 members of different types.
char Gender;
};
Example:
struct StudentGrade{
char Name[22];
char Course[5]; The “StudentGrade” structure
int Lab[12]; has 6 members with 5 members
int Homework[3]; of different array types.
int Exam[2];
double ave;
};
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 7
Using a struct
Declaration of a variable of struct type:
<struct-type> <identifier_list>;
Example:
StudentRecord Student1, Student2;
Name Name
Student1 Student2
Id Gender Id Gender
Dept Dept
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 8
4
4/8/2018
Accessing Members
COMP
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 9
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 10
5
4/8/2018
struct-to-struct assignment
The values contained in one struct type variable
can be assigned to another variable of the same
struct type.
Chan Tai Man
Example:
strcpy(Student1.Name, 12345 M Student1
"Chan Tai Man");
Student1.Id = 12345; COMP
strcpy(Student1.Dept, "COMP");
Student1.gender = 'M';
12345 M Student2
COMP
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 11
Arrays of structures
An ordinary array: One type of data
0 1 2 … 98 99
0 1 2 … 98 99
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 12
6
4/8/2018
Example:
StudentRecord Class[100];
strcpy(Class[98].Name, "Chan Tai Man");
Class[98].Id = 12345;
strcpy(Class[98].Dept, "COMP"); Chan Tai Man
Class[98].gender = 'M';
Class[0] = Class[98]; 12345 M
COMP
...
0 1 2 … 98 99
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 13
Pointers to Structures
StudentGrade *sptr;
…
cout << "Name is" << sptr->name;
cout << "Ave is " << sptr->ave;
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 14
7
4/8/2018
stu->ave = tot/15;
}
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 15
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 16
8
4/8/2018
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 17
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 18
9
4/8/2018
We use a rectangle to
represent an object and
<Object Name> place the underlined
name of the object
inside the rectangle.
Example:
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 19
Example:
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 20
10
4/8/2018
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 21
Sending a Message
deposit 250.00
SV198 : BankAccount
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 22
11
4/8/2018
getCurrentBalance()
SV198 : BankAccount
current balance
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 23
MobileRobot
getMaximumSpeed()
maximum speed
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 24
12
4/8/2018
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 25
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 26
13
4/8/2018
This line is an
instance-of
relationship.
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 27
SV129 : BankAccount
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 28
14
4/8/2018
Inheritance
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 29
A Sample Inheritance
• Here are the superclass Account and its subclasses Savings
and Checking.
Account
Checking Savings
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 30
15
4/8/2018
Inheritance Hierarchy
• An example of inheritance hierarchy among different types
of students.
Student
Graduate Undergrad
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 31
Software Engineering
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 32
16
4/8/2018
Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 33
17