Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 11-12 More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Similar presentations


Presentation on theme: "Chapter 11-12 More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg."— Presentation transcript:

1 Chapter 11-12 More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

2 New Programming Assignment PA10

3 The Three OOP Factors Remember, we said there were 3 factors that distinguished an Object Oriented Programming language:  Encapsulation  Inheritance  Polymorphism

4 We are Still at Encapsulation Feature of encapsulation:  Hide details of the implementation so that the program is easier to read and write  Modularity, make an object so that it can be reused in other contexts  Provide an interface (the methods) that is the approved way to deal with the class In a similar way to other classes Ex: the in operator

5 Instance Variables An instance variable is a variable inside of a class that stores information for a particular object In which class method do these variables first appear with some sort of initial value? 5

6 Let’s look at an updated version of the Student Class

7 How do we compute GPA? 7

8 First, convert letter grade into the number equivalent  A- is 3.67, C+ is 2.33, etc. Next, multiply the number of credits by the letter grade’s number equivalent to get a specific course’s grade point Finally, add up the number of grade points and divide by the total number of credits 8

9 Method Types Which class methods in class Student are accessor methods? Which class methods in class Student are mutator methods? 9

10 Two types of programmers now Programmer that builds the class Programmer that uses the class 10

11 Private Values We briefly talked about why class users may not want to access instance variables directly print(s1.firstNameStr) By only accessing data through methods, the class programmers are free to change how the class works  E.g. changing a variable name or adding new variables

12 Private Variables in an Instance Many OOP approaches allow you to make a variable or function in an instance private. Private means not accessible by the class user, only the class developer. There are advantages to controlling who can access the instance values.

13 ‘Privacy’ in Python Python takes the approach “We are all adults here.” No hard restrictions. Python implements this notion of privacy with the underscore: _  Which functions have we already seen with double underscores?

14 ‘Privacy’ in Python Provides naming to avoid accidents. Use _ in front of any variable Let’s modify our Student class to do that 14


Download ppt "Chapter 11-12 More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg."

Similar presentations


Ads by Google