Classes Part II - Chapter 7

Document Sample
Classes Part II - Chapter 7 Powered By Docstoc
					      Classes Part II - Chapter 7
• Constant Objects
• A guiding principle in programming is to keep
  as many things constant as possible.
  – This allows the compiler to see when you are
    making an error (changing something that should
    not be changed)
• If we have an object that is to be a constant,
  we can declare it to be constant
  – Need to give it initial values
  – ex., const Date newCentury(“Ja”,1,2000);
     Constant Member Functions
• A constant member function cannot change
  the data in the object.
• Use the word const after the argument list in
  both the prototype and the definition
  – int getYear() const;
  – int Date::getYear() const {return year;}
• The “get” functions are usually constant.
• Const objects can only call member functions
  declared const (even if the non const function
  does not change the member data).
  – thisYear=newCentury.getYear();
  – newCentury.setYear(1999); //invalid
         Constant Constructors
• Earlier, it was said that methods cannot
  change the contents of a constant object.
• There is one exception, the constructor
• Of course, the constructor has to be able to
  change (to initialize) the data in the object.
  Otherwise, the object cannot hold any values.
• The same argument applies to the destructor.
  The destructor removes the object (that is
  changing it). But it must be allowed even for
  a constant object.
        Constant Data Members
• An object may have some data members that
  are constant and some that are variables.
• This would not be the same as a constant
  object (all data members are constant).
• To initialize in the constructor use
  – Increment::Increment(int c, int i):
• Can use this syntax with non constants.
  – Increment::Increment(int c, int i):
     increment(i), count(c)
      Constant Object Summary
• If the object is not going to change, make that
  object a constant.
• If a member function in the class is not going
  to change any of the member data, then the
  function should be declared const.
• If a data member is not going to change after
  it has been initialized, make it const.
• There is no such thing as a constant CLASS.
• Can include an object as a member of a class.
• Allows building more involved classes from
  already built classes.
• For example, if we have a time class and are
  building an alarm clock, we can use the time
  class in the alarm clock.
• If we have a date class, we can use that in an
  employee class (date of hire, date of birth,
  date of retirement, etc.)
             Composition Example
• Look at a simple employee class. It will contain
   –   first name - character
   –   last name - character
   –   birthday - date
   –   hire date - date
• The constructor has 8 arguments (fName, lName,
  bmonth, bday, byear, hmonth, hday, hyear)
• The constructor can look like
Employee::Employee(char * firstName, char * lastName,
  int bmonth, int bday, int byear, int hmonth, int hday, int
  hyear) :
  birthDate(bmonth, bday, byear),
  hireDate(hmonth, hday, hyear)
{strcpy(firstName,fname); strcpy(lastName,lname);}
             Friend Functions
• A friend function of a class is defined outside
  of that class.
• It can access the private parts of the object.
• A function or an entire class may be a friend
  of another class.
• To declare a function as a friend of class Date,
  precede the function prototype in class Date
  with the word friend.
  – This function is NOT a part of the class.
• Now that function can access the private parts
  of class Date.
               Friend Classes
• You can make an entire class a friend of your
• You would just have friend class
  CLASSNAME inside the class that is
  allowing CLASSNAME access to its data.
• WHY?
• There is an ostream class for output. The
  ostream class knows what to do for ints,
  floats, etc. It does not know what to do with
  Date. You can define a function for ostream
  (outside the ostream file) that tells what to do
  when you want to output a Date (like
  cout<<myBirthday;). However, ostream
  needs to get to the data in Date.
           Friendship Granting
• When class A says that class B is a friend,
  then class B can get to class A’s private parts.
• However, friendship is not reciprocating, class
  A cannot get to class B private parts.
• A class has to let another class access to its
  own private parts, it cannot take access to
  another classes private parts.
• If class A says that class B is a friend, and
  class B says that class C is a friend, that does
  not mean that class A and C are friends (no
  relationships between A and C).
• Friendship granting is a “weakening” of object
  oriented design. It should be used carefully
  and sparingly.
           Returning an Object
• Every object has a pointer to itself.
• The pointer is called this.
• The member function will work with the
  individual parts easily.
• When done, you just
  – return *this;

Shared By: