Classes Part II Chapter 7 - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Classes Part II Chapter 7

Description:

The constructor has 8 arguments (fName, lName, bmonth, bday, byear, hmonth, hday, hyear) ... char * lastName, int bmonth, int bday, int byear, int hmonth, int ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 12
Provided by: willth9
Category:
Tags: bday | chapter | classes | part

less

Transcript and Presenter's Notes

Title: Classes Part II Chapter 7


1
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)

2
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 DategetYear() 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).
  • thisYearnewCentury.getYear()
  • newCentury.setYear(1999) //invalid

3
Constant Constructors
  • Earlier, it was said that methods cannot change
    the contents of a constant object.
  • There is one exception, the constructor function.
  • 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.

4
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
  • IncrementIncrement(int c, int i)
    increment(i) countc
  • Can use this syntax with non constants.
  • IncrementIncrement(int c, int i)
    increment(i), count(c)

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

6
Composition
  • 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.)

7
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
  • EmployeeEmployee(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)

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

9
Friend Classes
  • You can make an entire class a friend of your
    class.
  • 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 coutltltmyBirthday).
    However, ostream needs to get to the data in Date.

10
Friendship Granting
  • When class A says that class B is a friend, then
    class B can get to class As 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.

11
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
Write a Comment
User Comments (0)
About PowerShow.com