Introduction to Classes - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Classes

Description:

Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg – PowerPoint PPT presentation

Number of Views:145
Avg rating:3.0/5.0
Slides: 26
Provided by: SystemA404
Learn more at: http://www.cs.uni.edu
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Classes


1
Introduction to Classes
  • Intro to Computer Science
  • CS1510, Section 2
  • Dr. Sarah Diesburg

2
PA09
  • New homework - word clouds!

3
Programming Styles
  • Up until today, we have been programming in a
    procedural programming style
  • Series of computational steps to be carried out
  • We do this by writing functions that operate on
    data structures
  • This style of programming is suitable for smaller
    programming tasks

4
Programming Styles
  • Classes are part of Object Oriented Programming
    (OOP)
  • OOP is a way to think about objects in a
    program (such as variables, functions, etc).
  • A program becomes less a list of instructions and
    more a set of objects and how they interact.
  • Models the real world where objects (entities)
    work together to accomplish tasks

5
Responding to Messages
  • As a set of interacting objects, each object
    responds to messages sent to it.
  • The interaction of objects via messages makes a
    high level description of what the program is
    doing.

Start!
6
Everything in Python is an Object
  • In case you hadnt noticed, everything in Python
    is an object.
  • Thus Python embraces OOP at a fundamental level.
  • This is in contrast to other programming
    languages
  • C is completely procedural
  • Java is OOP but has some partial non-objects

7
OOP Helps in Software Engineering
  • Software engineering is the discipline of
    managing code to ensure its long-term use.
  • Weve already seen some code reuse
  • Counters, accumulators
  • Can write general functions
  • We will find the same thing with objects
  • Some basic objects are even easier to reuse
  • Strings, list, dictionaries,

8
More Refactoring
  • Hiding the details of what the message entails
    means that changes can be made to the object and
    the flow of messages (and their results) can stay
    the same.
  • Thus the implementation of the message can change
    but its intended effect stays the same.
  • This is encapsulation.

9
OOP Principles
  • Encapsulation hiding design details to make the
    program clearer and more easily modified later.
  • Modularity the ability to make objects stand
    alone so they can be reused (our modules). I.e.
    the math module.

10
OOP Principles
  • Inheritance create a new object by inheriting
    (like parent to child) many object
    characteristics while creating or over-riding for
    this object.
  • Polymorphism (hard) Allow one message to be sent
    to any object and have it respond appropriately
    based on the type of object it is.
  • Where have we seen this already??

11
Class versus Instance
  • One of the harder things to understand is what a
    class is and what an instance of a class is.
  • Consider the analogy of the cookie cutter and a
    cookie.

12
Class versus Instance
  • The cutter is a template for stamping out
    cookies, the cookie is what is made each time
    the cutter is used.
  • One template can be used to make an infinite
    number of cookies, each one just like the other.
  • No one confuses a cookie for a cookie cutter, do
    they?

13
Same in OOP
  • You define a class as a way to generate new
    instances of that class.
  • Both the instances and the classes are themselves
    objects.
  • The structure of an instance starts out the
    same, as dictated by the class.
  • The instances respond to the messages defined as
    part of the class.

14
Why a Class?
  • We make classes because we need more complicated,
    user-defined data types to construct instances we
    can use.
  • Each class has potentially two aspects
  • the data (types, number, names) that each
    instance might contain
  • the messages that each instance can respond to

15
A First Class
  • Say we need to make a new university information
    system
  • What is this?

16
A First Class
  • In a university information system, we need
    information about
  • Students
  • Faculty
  • Courses
  • Transcripts
  • Python does not have these
  • We need to create our own definitions of what a
    student is!

17
A First Class
  • What should a bundle of information called a
    student should know about in our code?
  • Name
  • Student ID
  • What else?

18
A First Class
  • What should a bundle of information called a
    student should know about in our code?
  • Name
  • Student ID
  • What else?
  • Could put this information in a tuple, but whats
    missing?

19
A First Class
  • What should a bundle of information called a
    student should know about in our code?
  • Name
  • Student ID
  • What else?
  • Could put this information in a tuple, but whats
    missing?
  • It would be nice to pass message to a student
    object, and have that student object operate on
    its own data

20
(No Transcript)
21
A sample class
  • class Student(object)
  • """Simple Student class."""
  • def __init__(self,first'', last'', id0)
    init instance
  • self.firstNameStr first
  • self.lastNameStr last
  • self.idInt id
  • def __str__(self) string representation
    for printing
  • return " , ID".format\
  • (self.firstNameStr,
    self.lastNameStr, self.idInt)

22
Constructor
  • When a class is defined, a function is made with
    the same name as the class
  • This function is called the constructor. By
    calling it, you can create an instance of the
    class.
  • We can affect this creation (more later), but by
    default Python can make an instance.

23
A Class is a New Type
  • gtgtgtmyLst 1, 2, 3
  • type(myLst) gt typeltlistgt
  • gtgtgtmyStr abc
  • type(myStr) gt typeltstrgt
  • gtgtgts1 Student(Jane",Doe",12345)
  • type(s1)
  • ltclass '__main__.Student'gt

24
Instance Knows its Class
  • Because each instance has as its type the class
    that it was made from, an instance remembers
    its class.
  • This is often called the instance-of
    relationship.

25
Dot Reference
  • We can refer to the attributes of an object by
    doing a dot reference, of the form
  • object.attribute
  • The attribute can be a variable or a function.
  • It is part of the object, either directly or by
    that object being part of a class.
Write a Comment
User Comments (0)
About PowerShow.com