CS304: Lecture 8 - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

CS304: Lecture 8

Description:

Objects can be declared as 'const' in the same manner as primitive data types ... And they are not allowed to modify the object. ... Alarm clock 'has-a' time (or two) ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 22
Provided by: matthe49
Category:
Tags: alarm | clock | cs304 | lecture

less

Transcript and Presenter's Notes

Title: CS304: Lecture 8


1
CS304 Lecture 8
  • Classes A Deeper Look, pt. 2
  • Deitel, Ch. 10
  • http//www.cis.uab.edu/cs304

2
Topics
  • Const objects and const members
  • Composition in objects
  • Friendship
  • This pointer
  • Dynamic Memory Allocation

3
Const Objects
  • Objects can be declared as const in the same
    manner as primitive data types
  • Only member functions also declared as const
    will be legal to call
  • And they are not allowed to modify the object.
  • const is specified both in the prototype and in
    the definition Overloading can be based on the
    constness of the function

4
Const Member Functions
  • Constructors and Destructors for constant
    objects?

5
Const Members
  • class test int xint ytest ( int a, int b)
    x a
  • y b
  • Const members have to be initialized in a special
    way.
  • class test const int xconst int y
  • test ( int a, int b)
  • x (a), y(b) //empty body

6
Initializer
  • All data members can be initialized using
    initializer.
  • Const data members and reference members must be
    initialized using initializers.
  • So does member objects
  • If not, a default constructor must be provided
    for the member objects.
  • So does base-class portions of derived classes
  • Member initializer list executes before the body
    of the constructor executes.

7
Composition
  • Some things are in a has-a relationship
  • Alarm clock has-a time (or two)
  • This relationship is satisfied by compisition, or
    having another object as a member.
  • Member objects can be constructed at
    instantiation of the host object

8
Member object instantiation
  • Imagine a time class with constructor Time(int
    hour, int minute).
  • class AlarmClock Time currenttime,
    alarmtimeAlarmclock(Time settime, Time
    setalarm) currenttime(settime),
    alarmtime(setalarm)

9
Construction/Destruction Order
  • As seen in the last side, objects are
    constructed from the inside out
  • Member objects are constructed before the host
    object is
  • Objects are destructed from the outside in
  • The host object is destroyed before the member
    objects are

10
Friend Functions/Classes
  • A friend function of a class is defined outside
    that classs scope, yet has the right to access
    the non-public members of the class.
  • Classes may also be declared as friends
  • Why do it? Performance and operator overloading.
  • A lt B vs. A.lessthan(B)
  • To make a function a friend, precede the function
    prototype in the class definition with keyword
    friend.
  • For classes, include friend class ClassName in
    the declaration of the class.

11
Friendship Rules
  • Friend functions are not members, but have their
    prototypes listed in the class definition.
  • You can choose your friends!
  • Friendship is granted, not taken
  • Friendship is not symmetric
  • Friendship is not transitive
  • A B C

12
Friend Example
  • class Count friend void setX( Count , int )
  • privateint x
  • void setX( Count c, int val )
  • c.x val

13
this, Formalized
  • this is basically implemented as an argument of
    every member function call
  • It is a pointer that points to the object it is a
    member of
  • Use can be implicit or explicit (to correct
    scoping issues, or passing the object to another
    function)
  • One of the more useful uses of this is to
    prevent an object from being assigned to itself
  • Finally, it is a pointer, not reference (as in
    Java)!!

14
Cascading Function Calls Using this
  • Imagine a group of functions that returned the
    object being used
  • Time TimesetMinute(int m) minute (m gt 0
    m lt 60) ? m 0return this
  • Functions could be call, a-like-sot.setHour(18).
    setMinute(30).setSecond(22)

Why return type is a reference??
15
Dynamic Memory Allocation new and delete
  • New creates memory, delete deallocates it
  • New and delete operate on pointer, not object,
    not reference.
  • New memory is always assigned to a pointer
  • This memory is allocated from the heap, and this
    where the pointer points
  • Time timeptr
  • timePtr new Time

16
Destroying Memory
  • Automatic garbage collection doesnt happen in
    C
  • When we are finished using this dynamically
    allocated memory, we need to free it
  • timeptr new Time()
  • delete timeptr
  • //This implicitly calls the destructor
  • Failing to release the memory is called a memory
    leak

17
Initializers/Constructors
  • Newly created objects can be assigned a value at
    creation.
  • double ptr new double( 3.14159 )
  • Time timeptr new Time(hour, min, sec)

18
Dynamically managing an array
  • Allocating an array is like weve seen before
  • int array new int10
  • Deallocating happens differently for an array
  • delete array
  • This ensures that the destructor is called for
    each element of the array.

19
Static Class Members
  • Only one copy is kept for all instantiations
  • Access is allowed before any objects of this
    class are created
  • Use the operator to access without an object
  • ClassNameStaticMember
  • Rule of thumb Static functions

20
Container Classes and Iterators
  • One of the most popular data types is the
    container class, one that exists only to hold
    several objects
  • Vector, Queue, Deque, Set
  • These commonly have iterator objects, whose only
    purpose is to walk through these structures
  • The ultimate in data hiding, and used extensively
    in the STL

21
Proxy classes
  • The basic idea of this is to hide the class
    definition from the client by providing only a
    limited interface through another class
  • For every public function in the implementation
    class, make a function in the interface class
    whose only purpose is to delegate back to the
    implementation class
Write a Comment
User Comments (0)
About PowerShow.com