1 OOP - An Introduction ISQS 6337 John R. Durrett.
-
Upload
edward-newman -
Category
Documents
-
view
212 -
download
0
Transcript of 1 OOP - An Introduction ISQS 6337 John R. Durrett.
1
OOP - An Introduction
ISQS 6337
John R. Durrett
Development Paradigms
• Increasing program complexity
• 1. Front panel switches
• 2. Assembly language
• 3. High level languages (3gl)(COBOL)
• 4. Structured Programming– reduce complex to simple parts– top down
• 5. OOP– self-contained == data + methods– inside out from classes
3
Top Down, procedure-oriented design steps
• Identify tasks (verbs) to perform
• Stepwise refinement (N. Wirth)– break task into sub-tasks until
sub-tasks are simple enough to implement directly
• Write procedures to solve sub-tasks
• Combine simple procedures to create functionality required
4
Procedural Programming
Main
Function 1Function 1 Function 5Function 4Function 3Function 2Function 1
Function 2.1 Function 2.2 Function 2.3
Function 2.2.1 Function 2.2.2
Function
Function
Global Data
FunctionFunction
5
OO design steps
• Identify objects (nouns) in environment
• Establish behavior and attributes in objects
• Identify relationships between objects
• Specify class/object interface & implementation
6
Object Oriented Programming
Data
Method Method
MethodMethod
Data
Method Method
MethodMethod
Data
Method Method
MethodMethod
7
Classes & Objects
• Class– abstract pattern or template– “cookie cutter”– interface defining behavior– description of data & related
operations
• Object– particular instance or specimen– “cookie”– instantiation of abstract class– definition of memory
8
Example: Integers
• Data (Attributes, fields, data members)
– Integers
• Operations (Methods, member funcs)
– Account_Balance()– Deposit()– Withdrawal()– Interest()
• All a user needs to care about is information input to operations and info returned
Encapsulation (information hiding)
• binds data & code together• black box
– no need for programmer to understand how methods are implemented
• fields are safe from outside interference– program never access data members
– only access is through public methods
– semi-global variables
• key is to make data private & provide public access methods– i.e. a well defined interface
10
C++ Access specifiers - control level of encapsulation
• All can contain data & methods
• public– available anywhere with scope – provide interface to private– like struct
• private– accessible only through public
member methods
• protected– Just like private (for now)
• friend
Polymorphism
• one name many purposes– C: abs() labs() fabs()– C++ abs()
• one interface multiple methods
• function overloading
• operator overloading
• compile time polymorphism– int abs(int);– long abs(long);– float abs(float);
12
Methods
• Functions that are members of class & therefore available in objects
• interface to private data
• Constructors (next slide)
• Mutators
• Accessors
• Destructors
Initialization
• automatic initialization of member data
• Constructors
• C++ ex:class myclass {
private: // default for class
int a;
public:
myclass (int x);
void show();
};
myclass::myclass(int x) {
a = x;
}
void myclass::show(void) {
cout << a;
}
void main(){
myclass ob(4);
ob.show(); // ok
ob.a = 12; // wrong!
}
Inheritance
• one object acquires properties of another
• hierarchical classification
• single inheritance (Java)
• multiple inheritance (C++)
• class - abstract representation
• object - defined variable of class type
15
Operator Overloading
• Increased flexibility
• Increased ease of use
• Increased control of class actions
• Not in java
• Example• Employee E1, E2;
• E1 = E2; cout << E1;
• Class-1.cpp example