Lecture Notes for Introduction to Computer Science II

5 June 2001 - Classes


  1. classes are just like structs

    1. classes group related data

      1. syntax - replace struct with class

      2. data in classes are called members, instance variables

      3. member types can be anything

      4. functions are called methods, member functions

      5. all member names within the same class must be different

      6. a class may have any number of members, including 0

    2. declaring members

      1. a class is like any other datatype - variable declarations, function declarations

    3. accessing members - the dot operator

    4. a members must be accessed through a class instance

  2. using classes

    1. classes may be passed around like any other variable

  3. the class definition must appear in the same file as member accesses

  • what's the difference

    1. it's good to delay decisions

    2. but, decisions need to be made

    3. make a dumb but immediate decsion and fix it later

    4. but, what happens when others latch onto your dumb decisions?

  • access control can solve this problem

    1. give selective access to struct and class members

    2. usually the functions, not the data

    3. public members can be accessed outside the class

    4. private members can only be accessed from inside the class

  • more syntax

    1. public: and private: keywords start public and private member declarations

    2. between member declarations, not in them

    3. may be repeated as many times as desired

    4. declare before use is suspended for members

    5. the default access for classes is private; for structs it's public


    This page last modified on 6 June 2001.