CIS 3100 Structured Programming Intro

Object identifiers we have three identifiers to

This preview shows page 22 - 24 out of 31 pages.

  OBJECT IDENTIFIERS   We have three identifiers to discuss in regards to classes. We can define class constants that  work essentially the same as constants in structured programming logic except that class  constants are defined within a class file. The second type of identifier can be defined as instance  variables or class variables. They both can be updated can modified but they do have different  purposes.   INSTANCE VARIABLES   Instance variables get their name because for every class that is instantiated an instance variable  is created. If you instantiate five objects from the same class, each object will have their own set  of instance variables. Instance variables are typically the first members identified in a class.   class CheckBook: # Instance variables self.deposits = 0 # initialize deposits instance variable to 0 self.checks = 0 # initialize checks instance variable to 0 self. balance = 0 # initialize balance instance variable to 0 - Here we use the PYTHON init  to describe and initialize instance variables.  
Image of page 22

Subscribe to view the full document.

CLASS VARIABLES   Class variables are variables which are shared across all its instantiated classes. For example,  maybe a class contains a counter class variable that holds the number of classes that have been  instantiated. With a class variable, all of the class objects would have access to the same class  variable and could update or use the value in their logic. Class variables are identified with  modifiers. In Java for example, you use the static modifier to designate a variable as a class  variable.     private static counter; Programming Tip:  Different programming languages use different approaches and modifiers  to identify instance and class variables. Some languages (like VB.Net) use fairly obvious  modifiers like const to identify the member and others (like Java) use modifiers like static that  are not as obvious and may require closer investigation. OBJECT METHODS   We have dedicated a large portion of this program to modules. With class files, we store our  modules inside the class as methods. Like the modules we discuss earlier, the methods in  classes can accept parameters and return values. Like variables, when we call a method owned  by a class from the program we must qualify the name of the method with the object variable and  a dot (dot notation).   Methods  - Modules implemented into a class are called methods. Like modules, methods can  accept parameters and optionally return a value at the conclusion of the module.
Image of page 23
Image of page 24
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern