2. 2
Previous Lecture
• Overloaded function
– Constructor
• const (constant)
– object
– member function
– data member
– object as function argument
• friend function
• this pointer
3. 3
Today’s Lecture
• Reference variable
• this pointer
– Cascading function calls
• Dynamic memory allocation
• static class member
– static functions
– static data member
• sdaf
4. 4
C++ Reference variable
• C++ references allow you to create a second
name for a memory location that you can use
to read or modify the original data stored in
that location
• Syntax
– int& foo = ....;
– reference to an int
• When a reference is created, you must tell it
which variable it will become an alias for
10. 10
Cascading function calls using this pointer
• In cascaded member-function calls invoked
multiple functions in the same statement
(Ref. of object t)
(Ref. of object t)
12. 12
Cont.
• Why does the technique of returning *this as a
reference work?
• The dot operator (.) associates from left to right
• first it evaluates t.setHour( 18 ) then returns a
reference to object t
• The remaining expression is then interpreted as
– t.setMinute( 30 ).setSecond( 22 );
• t.setMinute( 30 ) call executes and returns a
reference to the object t.
– t.setSecond( 22 );
Go to program
13. 13
Dynamic Memory Management
• C++ enables programmers to control the
allocation and de-allocation of memory in a
program for any built-in or user-defined type
• Performed with operators new and delete
• E.g. in Employee class
– name char array
– It has some size e.g. 25
• Through dynamic memory we can allocate array
space same as the number of character in name
14. 14
Cont.
• Dynamically allocating memory in this fashion causes
an array to be created in the free store (heap)
• Heap is a region of memory assigned to each program
for storing objects created at execution time
• Once the memory is allocated in the free store,
pointer points to the first byte of that allocated
memory
• After used, memory can be return to heap by using
delete operator
15. 15
Dynamic memory – basic types
• C++ allows you to provide an initializer for a
newly created fundamental-type variable
delete ptr;
*ptr
3.14159
704 704
16. 16
Dynamic memory – array
• new operator can be used to allocate arrays
dynamically
*gradeArray 704
704 708 712 716 720
17. 17
Simple example
• Example 1:
– char pointer
– Allocate dynamic memory using new operator
– Write string and display it
– Delete the dynamic memory using delete operator
• Example 2:
– struct definition
– Allocate dynamic memory
– Write string and display structure contents
– Delete the dynamic memory
Go to program
18. 18
Dynamic memory – objects
• Consider the following declaration and
statement:
0
0
0
hours
minutes
seconds
*timePtr
Go to program
19. 19
Composition – Objects as Members of Classes
• An AlarmClock object needs to know when it is
supposed to sound its alarm
• So why not include a Time object as a member
of the AlarmClock class
• It is refer as has-a relationship
• We will see how an object's constructor can
pass arguments to member-object
constructors