1 / 14

Chapter 11-12 More on Classes

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.

rogersa
Download Presentation

Chapter 11-12 More on Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 11-12More 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 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?

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

  7. How do we compute GPA?

  8. 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

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

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

  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

More Related