Вы находитесь на странице: 1из 17

4/8/2018

Object Oriented Programming


Spring 2018

Week 4: Structure, Introduction to Object-Oriented


Programming and Software Development

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.

• A struct is heterogeneous in that it can be composed of data of different types.

• 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

Student1 and Student2 are variables of


StudentRecord type.

Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 8

4
4/8/2018

Accessing Members

• You can treat the members of a struct just like variables.


• You need to use the member access operator '.'
(pronounced "dot"):

cout << student1.Name << endl;


student1.Id = 23847;
student1.Gender = ‘F’;
Abc Def
Student1
23847 F

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';

Student2 = Student1; Chan Tai Man

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

 An array of structs: Multiple types of data in each


array element.

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

• Pointers to structures are used often.


• There is another member access operator used with
pointers: ->

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

void update_grades(StudentGrade *stu)


{
double tot=0;
struct StudentGrade{
char Name[22];
char Course[5];
int Lab[12];
int Homework[3];
for (int i=0;i<3;i++)
int Exam[2]; tot += stu->Homework[i];
double ave;
};
for (int i=0;i<12;i++)
tot += stu->Lab[i];

stu->ave = tot/15;
}

Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 15

Object Oriented Programming

Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 16

8
4/8/2018

Classes and Objects

• Object-oriented programs use objects.


• An object is a thing, both tangible and intangible. Account, Vehicle,
Employee, etc.
• To create an object inside the computer program, we must provide a
definition for objects—how they behave and what kinds of information they
maintain —called a class.
• An object is called an instance of a class.

Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 17

Graphical Representation of a Class

<Class Name> We use a rectangle to


represent a class with
its name appearing
inside the rectangle.

Example: Account Motorcycle

The notation we used here is based on the industry standard notation


called UML, which stands for Unified Modeling Language.

Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 18

9
4/8/2018

Graphical Representation of an Object

We use a rectangle to
represent an object and
<Object Name> place the underlined
name of the object
inside the rectangle.

Example:

This is an object named


SV198 SV198.

Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 19

An Object with the Class Name

This notation indicates


<Object Name> : <Class Name> the class which the
object is an instance.

Example:

This tells an object


SV198 : BankAccount SV198 is an instance of
the BankAccount class.

Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 20

10
4/8/2018

Messages and Methods


• To instruct a class or an object to perform a task, we send
a message to it.
• You can send a message only to the classes and objects
that understand the message you sent to them.
• A class or an object must possess a matching method to be
able to handle the received message.
• A method defined for a class is called a class method, and
a method defined for an object is called an instance
method.
• A value we pass to an object when sending a message is
called an argument of the message.

Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 21

Sending a Message

Message deposit with


the argument 250.00 is
sent to a BankAccount
object SV198.

deposit 250.00
SV198 : BankAccount

Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 22

11
4/8/2018

Sending a Message and Getting an Answer

Ask for the current


balance of this
particular account.

getCurrentBalance()
SV198 : BankAccount

current balance

The current balance of


SV198 is returned.

Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 23

Calling a Class Method

Ask for the maximum


possible speed for all
MobileRobot objects is
returned.

MobileRobot
getMaximumSpeed()

maximum speed

Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 24

12
4/8/2018

Class and Instance Data Values

• An object is comprised of data values and methods.


• An instance data value is used to maintain information specific to individual
instances. For example, each BankAccount object maintains its balance.
• A class data value is used to maintain information shared by all instances or
aggregate information about the instances.
• For example, minimum balance is the information shared by all Account objects,
whereas the average balance of all BankAccount objects is an aggregate
information.

Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 25

Sample Instance Data Value

SV129 : BankAccount SV098 : BankAccount SV211 : BankAccount

current balance current balance current balance


908.55 1304.98 354.00

All three BankAccount


The actual dollar
objects possess the
amounts are, of course,
same instance data
different.
value current balance.

Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 26

13
4/8/2018

Sample Class Data Value


BankAccount
minimum balance There is one copy of
minimum balance for
100.00 the whole class and
shared by all instances.

This line is an
instance-of
relationship.

SV129 : BankAccount SV098 : BankAccount SV211 : BankAccount

current balance current balance current balance


908.55 1304.98 354.00

Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 27

Object Icon with Class Data Value

SV129 : BankAccount

minimum balance When the class icon is


100.00 not shown, we include
the class data value in
current balance the object icon itself.
908.55

Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 28

14
4/8/2018

Inheritance

• Inheritance is a mechanism in OOP to design two or more entities that are


different but share many common features.
– Features common to all classes are defined in the superclass.
– The classes that inherit common features from the superclass are called
subclasses.
• We also call the superclass an ancestor and the subclass a descendant.

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

Masters Doctoral Law Commuting Resident

Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 31

Software Engineering

• Much like building a skyscraper, we need a disciplined approach in


developing complex software applications.
• Software engineering is the application of a systematic and disciplined
approach to the development, testing, and maintenance of a program.
• In this class, we will learn how to apply sound software engineering
principles when we develop sample programs.

Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 32

16
4/8/2018

Software Life Cycle


• The sequence of stages from conception to operation
of a program is called software life cycle.
• Five stages are
– Analysis
– Design
– Coding
– Testing
– Operation and Maintenance

Dr. Mumraiz Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 4/8/2018 33

17

Вам также может понравиться