Week 2 Lecture 4 Introduction to Object-Oriented Programming
Object-Oriented Programming Overview
Focus of the class on what Object-Oriented Programming (OOP) is and how it is executed.
Key components:
Classes
Methods
Objects
Key Concepts of Object Oriented Programming
Two primary concepts in OOP:
Classes: Blueprints for creating objects.
Defined by a name with a capital letter in Camel Case format.
Example:
class Person:Objects: Instances of classes and reference to the class.
Created from classes to utilize functions and properties defined in the class.
Example:
cj = Person()wherecjis an object of thePersonclass.
Class and Object Explanation
Classes organize code and help manage complexity:
Example: For a game development project (GTA), if creating a character named CJ, we would set up a class for CJ with various methods (functions) such as:
def move_head(self):for head movement.def move_legs(self):for leg movement.All attributes and methods are scoped within the class representing that character.
Objects allow us to call these methods.
Common Features of OOP
OOP features six main principles, sometimes referred to as the acronym PIE ACE:
Polymorphism
Inheritance
Encapsulation
Abstraction
Classes
Objects
Advantages of Object-Oriented Programming
OOP promotes organization of code through encapsulation of related data and functions.
Improves manageability and error handling, making debugging easier by isolating functionalities into classes.
Example scenario with students:
Each student belongs to a specific class organized by a class structure.
Example of a Student Class
To illustrate OOP for students:
Define student class with attributes:
name,ID,major.class Student:Using an init method (initializer):
def __init__(self, name, ID, major):initializes input parameters for creating student objects.Example: `student1 = Student(