PowerPoint Presentation
Basics in programming
4.3.2019
Object-orientated programming
So far we have been here
This is were we are going
To build today’s complex software it is just not enough to put together a sequence of programming statements and sets of procedures and modules
We need to incorporate construction techniques and program structures that are easy to comprehend implement and modify.
Object-orientated programming
Object Oriented Programming (OOP) is an approach to program organization and development that attempts to eliminate some of the pitfalls of conventional programming methods by incorporating the best of structured programming features with several powerful new concepts.
It is a way of organizing and developing programs and has nothing to do with any particular language. However, not all languages are suitable to implement the OOP concepts easily.
Object-orientated programming
In the procedure oriented approach, the problem is viewed as the sequence of things to be done such as reading, calculating and printing such as Cobol, Fortran and C. The primary focus is on functions.
Object-orientated programming
Procedure oriented programming basically consists of writing a list of instructions for the computer to follow, and organizing these instructions into groups known as functions.
We normally use flowcharts to organize these actions and represent the flow of control from one action to another.
Object-orientated programming
Some characteristics exhibited by procedure-oriented programming are:
• Emphasis is on doing things (algorithms).
• Large programs are divided into smaller programs known as functions.
• Most of the functions share global data.
• Data move openly around the system from function to function.
• Functions transform data from one form to another.
• Employs top-down approach in program design.
Object-orientated programming
Object-oriented programming (OOP) is a programming language model organized around objects rather than “actions” and data rather than logic.
Object-orientated programming
Some characteristics exhibited by procedure-oriented programming are:
• Emphasis is on doing things (algorithms).
• Large programs are divided into smaller programs known as functions.
• Most of the functions share global data.
• Data move openly around the system from function to function.
• Functions transform data from one form to another.
• Employs top-down approach in program design.
Object-orientated programming
The major motivating factor in the invention of object-oriented approach is to remove some of the flaws encountered in the procedural approach.
OOP treats data as a critical element in the program development and does not allow it to flow freely around the system. It ties data more closely to the function that operate on it, and protects it from accidental modification from outside function
Object-orientated programming
OOP allows decomposition of a problem into a number of entities called objects and then builds data and function around these objects.
Object-orientated programming
Some of the features of object oriented programming are:
• Emphasis is on data rather than procedure.
• Programs are divided into what are known as objects. • Data structures are designed such that they characterize the objects.
• Functions that operate on the data of an object are ties together in the data structure.
• Data is hidden and cannot be accessed by external function.
• Objects may communicate with each other through function.
• New data and functions can be easily added whenever necessary.
• Follows bottom up approach in program design.
Object-orientated programming
It is necessary to understand some of the concepts used extensively in object-oriented programming. These include:
• Objects
• Classes
• Data abstraction and encapsulation
• Inheritance
• Polymorphism
• Dynamic binding
• Message passing
Basic Concepts of Object Oriented Programming
Objects:
Objects are the basic run time entities in an object-oriented system. They may represent a person, a place, a bank account, a table of data or any item that the program has to handle. They may also represent user-defined data such as vectors, time and lists.
Program objects should be chosen such that they match closely with the real-world objects. Objects take up space in the memory and have an associated address
Basic Concepts of Object Oriented Programming
Objects:
When a program is executed, the objects interact by sending messages to one another.
For example, if “customer” and “account” are to object in a program, then the customer object may send a message to the count object requesting for the bank balance. Each object contain data, and code to manipulate data.
Basic Concepts of Object Oriented Programming
Objects:
Objects can interact without having to know details of each other’s data or code. It is a sufficient to know the type of message accepted, and the type of response returned by the objects.
Basic Concepts of Object Oriented Programming
Objects:
Basic Concepts of Object Oriented Programming
Objects:
Basic Concepts of Object Oriented Programming
Objects:
Objects and classes in Python
Classes:
Objects contain data, and code to manipulate that data. The entire set of data and code of an object can be made a user-defined data type with the help of class.
Objects are variables of the type class.
Basic Concepts of Object Oriented Programming
Classes:
For examples, Mango, Apple and Orange are members of class Fruit.
Classes are user-defined that types and behave like the built-in types of a programming language.
The syntax used to create an object is not different then the syntax used to create an String object in Python
Basic Concepts of Object Oriented Programming
Classes:
For examples, Mango, Apple and Orange are members of class Fruit.
Classes are user-defined that types and behave like the built-in types of a programming language.
The syntax used to create an object is not different then the syntax used to create an String object in Python
Basic Concepts of Object Oriented Programming
Classes:
Basic Concepts of Object Oriented Programming
Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax and semantics.
Python classes provide all the standard features of Object Oriented Programming:
-the class inheritance mechanism allows multiple base classes
-a derived class can override any methods of its base class or classes,
-method can call the method of a base class with the same name.
Objects can contain arbitrary amounts and kinds of data.
Classes in Python
A class is a code template for creating objects. Objects have member variables and have behaviour associated with them. In python a class is created by the keyword class.
An object is created using the constructor of the class. This object will then be called the instance of the class. In Python we create instances in the following manner
OOP in Python
The simplest class can be created using the class keyword. For example, let’s create a simple, empty class with no functionalities.
class House:
pass
OOP in Python
Note: Pass is used when a statement is required syntactically but you do not want any command or code to execute.
Now we can use our House class in main program
my_house = House()
print(my_house )
You can assign the class to a variable. This is called object instantiation. You will then be able to access the attributes that are present inside the class using the dot . operator.
OOP in Python
Attributes in class:
class House:
rooms_count = 1
address = ””
OOP in Python
Now we can use access name or room_count
my_house = House()
print(my_house .room_count)
print(my_house .address)
OOP in Python
Methods in class:
Once there are attributes that “belong” to the class, you can define functions that will access the class attribute.
These functions are called methods. When you define methods, you will need to always provide the first argument to the method with a self keyword.
OOP in Python
Methods in class:
class House:
rooms_count = 1
address = ””
def print(self):
print(This is my house’)
def setRoomCount(self, new_count):
self.rooms_count = count
OOP in Python
Constructors:
Class functions that begins with double underscore (__) are called special functions as they have special meaning.
Of one particular interest is the __init__() function. This special function gets called whenever a new object of that class is instantiated.
This type of function is also called constructors in Object Oriented Programming (OOP). We normally use it to initialize all the variables.
OOP in Python
Constructors:
class House:
rooms_count = 1
address = ””
def __init__(self, rooms_count ):
self. rooms_count = 0
OOP in Python
Create class that represents a real-life object Car and use it in main program as object
Car has following functions:
SetSpeed, set current speed of car
GetSpeed, gets current speed of car
SetDirection, sets direction of car
GetDirection, get direction of car
SetColor, sets color of car
GetColor, gets color of car
Print, prints all details of car
Create class that represents a real-life object Person and use it in main program as object. Design your own data and functions
In Exercise 8. we did Car module, what is difference
between module and class?
Exercises 11.
/docProps/thumbnail.jpeg