A. Procedural and Object-Oriented Design
1. Procedural Programming
a. method of writing software that centers on the procedures or functions in a program
b. data is stored in variables which are separate from (and in fact must be passed to) functions
c. Problems with Procedural Programming
1). programs with excessive global data (data protection)
2). complex programs (i.e., excessive functions, some of which aren’t related to similar data)
3). programs that are difficult to modify
2. Object-Oriented Programming (OOP)
a. method of writing software that centers around objects
b. objects encapsulate data and functions into one variable
c. philosophy is now to collect data and functions that operate on that data into one type of data unit (“encapsulation”) while “hiding” it from code outside of itself
3. Pictorial representation
a. data – and functions – common to similar type variables can be collected into one unit
b. data can be “protected” – usually, we can only modify the data through the object functions
B. Classes – An Introduction
1. ADT that serves as the data type for objects
2. In essence, are lists with two modifications:
a. We add member “functions” that actually DO something.
b. We can “protect” the data stored in the class by declaring data to be “private” data, restricting its accessibility.
3. Classes are templates; actual class variables are objects. (“An object is an instance of a class.”)
4. Parts of a class
1). also known as member variables, data members, or attributes
2). are declared as in ordinary programs
3). 2 types in terms of accessibility (access specifiers)
a). public – can be freely accessed by program
b). private – can ONLY be accessed by member functions
1). also known as member functions, methods, or behaviors
2). These functions act as normal functions, plus . . .
3). . . . they can access private data.
5. Defining a class
a. Define the entire class globally (i.e., BEFORE main).
b. Define the class blueprint:
1). First, we declare the class and its name.
2). Second, we use a special, built-in function: __init__
a). This function defines and initializes the member data of the class
b). Is similar to a constructor in C++.
3). Then we define any and all member functions of the class.
c. function headers and implementations follow this blueprint
self.<variable_name1> = <value> #data attribute #1
self. <variable_name2> = <value> #data attribute #2
# then define various member functions
def <function_name> (self,<parameter1>,<parameter2>,…):
# statement 1
# statement 2
# other functions may follow
# in main
<object name> = <class name>(<parameter1>,<parameter2>,…)
# declares an object of the class
# other code follows
a). must be any valid identifier
b). This name is the name of the new data type, NOT a new variable name!
2). The __init__ function
a). Must be followed by parentheses and at least one parameter – in this case, self – which refers to the object making the function call.
b). Other parameters may be passed to the function.
c). Syntax for declaring functions is again followed.
d). The object self then refers to the object making the call.
3). Member functions are then defined in the normal way for functions.
4). In main, to declare and actual object (instance of the class), we define an object name and set it equal to the class name with parentheses.
Get Free Quote!
268 Experts Online