140 likes | 158 Views
Chapter 11-12 More on Classes. Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg. New Programming Assignment PA10. The Three OOP Factors. Remember, we said there were 3 factors that distinguished an Object Oriented Programming language: Encapsulation Inheritance Polymorphism.
E N D
Chapter 11-12More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
The Three OOP Factors • Remember, we said there were 3 factors that distinguished an Object Oriented Programming language: • Encapsulation • Inheritance • Polymorphism
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
Instance Variables • An instance variableis 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?
How do we compute GPA? • 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
Method Types • Which class methods in class Student are accessor methods? • Which class methods in class Student are mutator methods?
Two types of programmers now • Programmer that builds the class • Programmer that uses the class
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
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.
‘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?
‘Privacy’ in Python • Provides naming to avoid accidents. Use _ in front of any variable • Let’s modify our Student class to do that