CHAPTER 15 POINTERS, CLASSES, AND VIRTUAL FUNCTIONS - PowerPoint PPT Presentation

1 / 79
About This Presentation
Title:

CHAPTER 15 POINTERS, CLASSES, AND VIRTUAL FUNCTIONS

Description:

Learn about the pointer data type and pointer variables ... In C , the ampersand, &, called the address of operator, is a unary operator ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 80
Provided by: dma121
Category:

less

Transcript and Presenter's Notes

Title: CHAPTER 15 POINTERS, CLASSES, AND VIRTUAL FUNCTIONS


1
CHAPTER 15POINTERS, CLASSES, AND VIRTUAL
FUNCTIONS
2
  • In this chapter, you will
  • Learn about the pointer data type and pointer
    variables
  • Explore how to declare and manipulate pointer
    variables
  • Learn about the address of operator and the
    dereferencing operator
  • Discover dynamic variables
  • Explore how to use the new and delete operators
    to manipulate dynamic variables
  • Learn about pointer arithmetic
  • Discover dynamic arrays
  • Become aware of the shallow and deep copies of
    data
  • Discover the peculiarities of classes with
    pointer data members
  • Learn about virtual functions
  • Examine the relationship between the address of
    operator and classes

3
  • THE POINTER DATA TYPE AND POINTER VARIABLES
  • Pointer variable A variable whose content is an
    address (that is, a memory address).
  • Declaring Pointer Variables
  • The general syntax of declaring a pointer
    variable is
  • dataType identifier
  • int p
  • char ch

4
  • The statement
  • int p
  • is equivalent to the statement
  • int p
  • which is equivalent to the statement
  • int p
  • The character can appear anywhere between the
    data type name and the variable name.
  • int p, q
  • Only p is the pointer variable, not q. Here q is
    an int variable.
  • To avoid confusion, we prefer to attach the
    character to the variable name.

5
  • THE ADDRESS OF OPERATOR ()
  • In C, the ampersand, , called the address of
    operator, is a unary operator that returns the
    address of its operand.
  • Given the statements
  • int x
  • int p
  • the statement
  • p x
  • assigns the address of x to p. That is, x and
    the value of p refers to the same memory
    location.

6
  • THE DEREFERENCING OPERATOR ()
  • C also uses as a unary operator.
  • When used as a unary operator, , commonly
    referred to as the dereferencing operator or
    indirection operator, refers to the object to
    which its operand (that is, a pointer) points
  • int x 25
  • int p
  • p x //store the address of x in p
  • The statement
  • coutltltpltltendl
  • prints the value of x. Also the statement
  • p 55
  • will store 55 in the memory location pointed to
    by p, that is, in x.

7
  • int p
  • int num
  • p is a pointer variable of the type int and num
    is a variable of type int.

8
  • num 78

9
  • p num

10
  • p 24

11
  • 1. p, p, and p all have different meanings.
  • 2. p means the address of pthat is, 1200 (in
    Figure 15-4).
  • 3. p means the content of p (1800 in Figure
    15-4).
  • 4. p means the content (24 in Figure 15-4) of
    the memory location (1800 in Figure 15-4) pointed
    to by p (that is, pointed to by the content of
    memory location 1200).

12
  • Example 15-1
  • int p
  • int x

13
  • After the statement
  • x 50

14
  • After the statement
  • p x
  • After the statement
  • p 38

15
  • 1. A declaration such as
  • int p
  • allocates memory for p only, not for p.
  • 2. Assume the following
  • int p
  • int x
  • a. p is a pointer variable
  • b. Content of p only points to a memory location
    of the type int
  • c. Memory location x exists and is of the type
    int.
  • The assignment
  • p x
  • is legal. After the execution of this statement
    p is valid and meaningful.

16
  • Example 15-2
  • //Chapter 15 Example 15-2
  • include ltiostreamgt
  • using namespace std
  • int main()
  • int p
  • int x 37
  • coutltlt"Line 1 x "ltltxltltendl //Line 1
  • p x //Line 2
  • coutltlt"Line 3 p "ltltp
  • ltlt", x "ltltxltltendl //Line 3

17
  • coutltlt"Line 8 Value of the memory location "
  • ltlt"pointed to by p "ltltpltltendl //Line 8
  • coutltlt"Line 9 Address of x
    "ltltxltltendl //Line 9
  • coutltlt"Line 10 Value of x
    "ltltxltltendl //Line 10
  • return 0
  • Sample Run
  • Line 1 x 37
  • Line 3 p 37, x 37
  • Line 5 p 58, x 58
  • Line 6 Address of p 006BFDF4
  • Line 7 Value of p 006BFDF0
  • Line 8 Value of the memory location pointed to
    by p 58
  • Line 9 Address of x 006BFDF0
  • Line 10 Value of x 58

18
  • CLASSES, STRUCTS, AND POINTER VARIABLES
  • You can also declare pointers to other data
    types, such as classes and structs.
  • struct studentType
  • char name26
  • double gpa
  • int ssn
  • char grade
  • studentType student
  • studentType studentPtr
  • studentPtr student

19
  • student.gpa 3.9
  • (studentPtr).gpa 3.9
  • store 3.9 in the component gpa of the object
    student.
  • Since dot has a higher precedence than the
    dereferencing operator, parentheses are
    important.
  • C provides another operator, called the member
    access operator arrow, -gt.
  • The syntax for accessing a class (struct) member
    using the operator -gt is
  • pointerVariableName-gtclassMemberName
  • The following statements are equivalent.
  • (studentPtr).gpa 3.9
  • studentPtr-gtgpa 3.9

20
  • Example 15-3
  • class classExample
  • public
  • void setX(int a)
  • void print() const
  • private
  • int x
  • void classExamplesetX(int a)
  • x a
  • void classExampleprint() const
  • coutltlt"x "ltltxltltendl

21
  • int main()
  • classExample cExpPtr //Line 1
  • classExample cExpObject //Line 2
  • cExpPtr cExpObject //Line 3
  • cExpPtr-gtsetX(5) //Line 4
  • cExpPtr-gtprint() //Line 5
  • return 0
  • Output
  • x 5

22
  • INITIALIZING POINTER VARIABLES
  • p NULL
  • p 0

23
  • DYNAMIC VARIABLES
  • Variables that are created during program
    execution are called dynamic variables.
  • With the help of pointers, C creates dynamic
    variables.
  • C provides two operators, new and delete, to
    create and destroy dynamic variables,
    respectively.
  • When a program requires a new variable, the
    operator new is used when a program no longer
    needs a dynamic variable, the operator delete is
    used.
  • In C, new and delete are reserved words.

24
  • The syntax to use the operator new is
  • new dataType //to allocate a single
    variable
  • new dataTypeintExp //to allocate an array of
    variables
  • where intExp is any expression evaluating to a
    positive integer.
  • The operator new allocates memory (a variable) of
    the designated type and returns a pointer to
    itthat is, the address of this allocated memory.
  • The allocated memory is uninitialized.

25
  • int p
  • char q
  • int x
  • The statement
  • p x
  • stores the address of x in p. No new memory is
    allocated.
  • The statement
  • p new int
  • creates a variable during execution somewhere in
    the memory, and stores the address of the
    allocated memory in p.
  • The allocated memory is accessed via pointer
    dereferencing, p.
  • The statement
  • q new char16

26
  • int p //p is a pointer of the type int
  • char name //name is a pointer of the type char
  • string str //str is a pointer of the type
    string
  • p new int
  • p 28 //stores 28 in the allocated memory
  • name new char5
  • strcpy(name, "John") //stores John in name
  • str new string //allocates memory of the type
    string
  • //and stores the address of the
  • //allocated memory in str
  • str "Sunny Day" //stores the string "Sunny
    Day" in
  • //memory pointed to by str

27
  • The C operator delete is used to destroy
    dynamic variables. The syntax to use the operator
    delete has two forms
  • delete pointer //to destroy a single dynamic
    variable
  • delete pointer //to destroy a dynamically
    created
  • //array
  • The statements
  • delete p
  • delete name
  • deallocate memory referenced by the pointers p
    and name.

28
  • OPERATIONS ON POINTER VARIABLES
  • Assignment
  • Value of one pointer variable can be assigned to
    another pointer variable of the same type.
  • Relational operations
  • Two pointer variables of the same type can be
    compared for equality, and so on.
  • Some limited arithmetic operations.
  • Integer values can be added and subtracted from a
    pointer variable.
  • Value of one pointer variable can be subtracted
    from another pointer variable.

29
  • int p, q
  • The statement
  • p q
  • copies the value of q into p.
  • The expression
  • p q
  • evaluates to true if both p and q have the same
    value.
  • The expression
  • p ! q
  • evaluates to true, if both p and q point to
    different memory locations.

30
  • Arithmetic operations, that are allowed, differ
    from the arithmetic operations on numbers.
  • int p
  • double q
  • char chPtr
  • studentType stdPtr //studentType data type is
  • //as defined before
  • The memory allocated for an int variable is 4
    bytes, a double variable is 8 bytes, and a char
    variable is 1 byte. The memory allocated for a
    variable of the type studentType is 39 bytes.
  • The statement
  • p or p p1
  • increments the value of p by 4 bytes.

31
  • The statements
  • q
  • chPtr
  • increment the value of q by 4 bytes and the
    value of chPtr by 1 byte
  • The statement
  • stdPtr
  • increments the value of stdPtr by 39 bytes.
  • The statement
  • p p 2
  • increments the value of p by 8 bytes.

32
  • When an integer is added to a pointer variable,
    the value of the pointer variable is incremented
    by the integer times the size of the memory that
    the pointer is pointing to.
  • When an integer is subtracted from a pointer
    variable, the value of the pointer variable is
    decremented by the integer times the size of the
    memory to which the pointer is pointing.

33
  • DYNAMIC ARRAYS
  • An array created during the execution of a
    program is called a dynamic array.
  • To create a dynamic array, we use the second form
    of the new operator.
  • The statement
  • int p
  • declares p to be a pointer variable of the type
    int.
  • The statement
  • p new int10
  • allocates 10 contiguous memory locations, each
    of the type int, and stores the address of the
    first memory location into p.

34
  • The statement
  • p 25
  • stores 25 into first memory location.
  • p
  • p 35
  • stores 35 into the second memory location.
  • C allows us to use array notation to access
    these memory locations.
  • p0 25
  • p1 35

35
  • int list10
  • list is a pointer variable.
  • We always want list to point the first array
    component.
  • Any attempt to use increment or decrement
    operation on list will result in compile time
    error.
  • If p is a pointer variable of the type int, then
    the statement
  • p list
  • copies the value of list into p. We are allowed
    to perform increment and decrement operations on
    p.
  • An array name is a constant pointer.

36
  • Example 15-4
  • int intList //Line 1
  • int arraySize //Line 2
  • coutltlt"Enter array size " //Line 3
  • cingtgtarraySize //Line 4
  • coutltltendl //Line 5
  • intList new intarraySize //Line 6
  • The statement at Line 1 declares intList to be a
    pointer of the type int
  • The statement at Line 2 declares arraySize to be
    an int variable.
  • The statement at Line 3 prompts the user to enter
    the size of the array.
  • The statement at Line 4 inputs the array size
    into the variable arraySize.
  • The statement at Line 6 creates an array of the
    size specified by arraySize, and the base address
    of the array is stored in intList.
  • From this point on, you can treat intList just
    like any other array. For example, you can use
    the array notation to process the elements of
    intList and pass intList as a parameter to the
    function.

37
  • Functions and Pointers
  • A pointer variable can be passed as a parameter
    to a function either by value or by reference.
  • In C, to make a pointer a reference parameter
    in a function heading, appears before the
    between the data type name and the identifier.
  • void example(int p, double q)
  • .
  • .
  • .
  • Both p and q are pointers. The parameter p is a
    reference parameter the parameter q is a value
    parameter.

38
  • Pointers and Function Return Value
  • In C, a function can return a value of the type
    pointer.
  • The return type of the function
  • int testExp(...)
  • .
  • .
  • .
  • is a pointer of the type int.

39
  • SHALLOW VERSUS DEEP COPY AND POINTERS
  • int p
  • p new int
  • The first statement declares p to be a pointer
    variable of the type int.
  • The second statement allocates memory of the type
    int, and the address of the allocated memory is
    stored in

40
  • p 87

int first int second first new int10
41
  • second first //Line

42
  • delete second

In a shallow copy, two or more pointers of the
same type point to the same memory that is, they
point to the same data.
43
  • second new int10
  • for(int j 0 j lt 10 j)
  • secondj firstj
  • In a deep copy, two or more pointers have their
    own data.

44
  • CLASSES AND POINTERS SOME PECULIARITIES
  • class pointerDataClass
  • public
  • ...
  • private
  • int x
  • int lenP
  • int p
  • pointerDataClass objectOne
  • pointerDataClass objectTwo

45
  • The Destructor
  • The object objectOne has a pointer data member p.
  • Suppose that during program execution the pointer
    p creates a dynamic array.
  • When objectOne goes out of scope, all data
    members of objectOne are destroyed.
  • However, p created a dynamic array, and dynamic
    memory must be deallocated using the operator
    delete.
  • If the pointer p does not use the delete operator
    to deallocate the dynamic array, the memory space
    of the dynamic array would stay marked as
    allocated, even though no one can access it.
  • How do we ensure that when p is destroyed, the
    dynamic memory created by p is also destroyed?

46
  • If a class has a destructor, the destructor
    automatically executes whenever a class object
    goes out of scope.
  • We can put the necessary code in the destructor
    to ensure that when objectOne goes out of scope,
    the memory created by the pointer p is
    deallocated.
  • pointerDataClasspointerDataClass()
  • delete p
  • class pointerDataClass
  • public
  • pointerDataClass()
  • ...
  • private
  • int x
  • int lenP
  • int p

47
  • The Assignment Operator

48
  • objectTwo objectOne
  • If objectTwo.p deallocates the memory space to
    which it points, objectOne.p would become
    invalid.

49
  • To avoid this shallow copying of data for classes
    with a pointer data member, C allows the
    programmer to extend the definition of the
    assignment operator.
  • This process is called overloading the assignment
    operator.
  • Chapter 16 explains how to accomplish this task
    by using operator overloading.
  • Once the assignment operator is properly
    overloaded, both the objects objectOne and
    objectTwo have their own data, as shown in Figure
    15-19.

50
  • The Copy Constructor
  • Consider the following statement
  • pointerDataClass objectThree(objectOne)
  • The object objectThree is being declared and is
    also being initialized by using the value of
    objectOne.
  • This initialization is called the default
    member-wise initialization.
  • The default member-wise initialization is due to
    the constructor, called the copy constructor
    (provided by the compiler.)
  • This default initialization would lead to a
    shallow copying of the data.

51
  • void destroyList(pointerDataClass paramObject)
  • destroyList(objectOne)

52
  • If a class has pointer data members
  • During object declaration, the initialization of
    one object using the value of another object
    would lead to shallow copying of data if the
    default member-wise copying of data is allowed.
  • If, as a parameter, an object is passed by value
    and the default member-wise copying of data is
    allowed, it would lead to shallow copying of
    data.
  • In both cases, to force each object to have its
    own copy of the data, we must override the
    definition of the copy constructor provided by
    the compiler.
  • This is usually done by putting a statement that
    includes the copy constructor in the definition
    of the class, and then writing the definition of
    the copy constructor.
  • For the class pointerDataClass, we can overcome
    this shallow copying of data problem by including
    the copy constructor in the class
    pointerDataClass.

53
  • The copy constructor automatically executes in
    two situations (as described in the previous
    list)
  • When an object is declared and initialized by
    using the value of another object
  • When, as a parameter, an object is passed by
    value
  • Once the copy constructor is properly defined for
    the class pointerDataClass, both objectOne.p and
    objectThree.p will have their own copies of the
    data. Similarly, objectOne.p and paramObject.p
    will have their own copies of the data.

54
  • Example 15-5
  • class pointerDataClass
  • public
  • void print() const
  • void setData()
  • void destroyP()
  • pointerDataClass(int sizeP 10)
  • pointerDataClass()
  • pointerDataClass (const pointerDataClass
    otherObject)
  • //the copy constructor
  • private
  • int x
  • int lenP
  • int p //pointer to an int array

55
  • void pointerDataClassprint() const
  • coutltlt"x "ltltxltltendl
  • coutltlt"p "
  • for(int i 0 i lt lenP i)
  • coutltltpiltlt" "
  • coutltltendl
  • void pointerDataClasssetData()
  • coutltlt"Enter an integer for x "
  • cingtgtx
  • coutltltendl
  • coutltlt"Enter "ltltlenPltlt" numbers "

56
  • void pointerDataClassdestroyP()
  • lenP 0
  • delete p
  • p NULL
  • pointerDataClasspointerDataClass(int sizeP)
  • x 0
  • if(sizeP lt 0)
  • coutltlt"Array size must be positive"ltltendl
  • coutltlt"Creating an array of size 10"ltltendl
  • lenP 10
  • else

57
  • pointerDataClasspointerDataClass()
  • delete p
  • //copy constructor
  • pointerDataClasspointerDataClass
  • (const pointerDataClass
    otherObject)
  • x otherObject.x
  • lenP otherObject.lenP
  • p new intlenP
  • for(int i 0 i lt lenP i)
  • pi otherObject.pi

58
  • include ltiostreamgt
  • include "ptrDataClass.h"
  • using namespace std
  • void testCopyConst(pointerDataClass temp)
  • int main()
  • pointerDataClass one(5) //Line 1
  • one.setData() //Line 2
  • coutltlt"Line 3 Object one's data"ltltendl
    //Line 3
  • one.print() //Line 4
  • coutltlt"Line 5______________________________"
  • ltlt"______________"ltltendl //Line 5
  • pointerDataClass two(one) //Line 6

59
  • one.print() //Line 12
  • coutltlt"Line 13_______________________________"
  • ltlt"_____________"ltltendl //Line 13
  • coutltlt"Line 14 Calling the function
    testCopyConst"
  • ltltendl //Line 14
  • testCopyConst(one) //Line 15
  • coutltlt"Line 16_______________________________"
  • ltlt"_____________"ltltendl //Line 16
  • coutltlt"Line 17 After a call to the function "
  • ltlt"testCopyConst, object one
    is"ltltendl //Line 17
  • one.print() //Line 18
  • return 0 //Line 19

60
  • void testCopyConst(pointerDataClass temp)
  • coutltlt"Line 20 Inside function "
  • ltlt"testCopyConst "ltltendl //Line 20
  • coutltlt"Line 21 Object temp data"ltltendl //Line
    21
  • temp.print() //Line 22
  • temp.setData() //Line 23
  • coutltlt"Line 24 After changing the object "
  • ltlt"temp, its data is "ltltendl //Line 24
  • temp.print() //Line 25
  • coutltlt"Line 26 Exiting function "
  • ltlt"testCopyConst "ltltendl //Line 26

61
  • Sample Run In this sample run, the user input is
    in red.
  • Enter an integer for x 28
  • Enter 5 numbers 2 4 6 8 10
  • Line 3 Object one's data
  • x 28
  • p 2 4 6 8 10
  • Line 5___________________________________________
    _
  • Line 7 Object two's data
  • x 28
  • p 2 4 6 8 10
  • Line 9___________________________________________
    _
  • Line 11 Object one's data after destroying
    object two.p
  • x 28
  • p 2 4 6 8 10
  • Line 13__________________________________________
    __
  • Line 14 Calling the function testCopyConst

62
  • Enter an integer for x 65
  • Enter 5 numbers 1 3 5 7 9
  • Line 24 After changing the object temp, its data
    is
  • x 65
  • p 1 3 5 7 9
  • Line 26 Exiting function testCopyConst
  • Line 16__________________________________________
    __
  • Line 17 After a call to the function
    testCopyConst, object one is
  • x 28
  • p 2 4 6 8 10

63
  • INHERITANCE, POINTERS, AND VIRTUAL FUNCTIONS
  • C allows the user to pass an object of a
    derived class to a formal parameter of the base
    class type.
  • class baseClass
  • public
  • void print()
  • baseClass(int u 0)
  • private
  • int x
  • class derivedClass public baseClass
  • public
  • void print()
  • derivedClass(int u 0, int v 0)

64
  • void baseClassprint()
  • coutltlt"In baseClass x "ltltxltltendl
  • baseClassbaseClass(int u)
  • x u
  • void derivedClassprint()
  • coutltlt"In derivedClass "
  • baseClassprint()
  • coutltlt"In derivedClass a "ltltaltltendl
  • derivedClassderivedClass(int u, int v)
  • baseClass(u)

65
  • void callPrint(baseClass p)
  • p.print()
  • int main()
  • baseClass one(5) //Line 1
  • derivedClass two(3, 15) //Line 2
  • one.print() //Line 3
  • two.print() //Line 4
  • coutltlt" Calling the function callPrint
    "
  • ltltendl //Line 5
  • callPrint(one) //Line 6
  • callPrint(two) //Line 7
  • return 0

66
  • Output
  • In baseClass x 5
  • In derivedClass In baseClass x 3
  • In derivedClass a 15
  • Calling the function callPrint
  • In baseClass x 5
  • In baseClass x 3

67
  • In compile-time binding, the necessary code to
    call a specific function is generated by the
    compiler.
  • Compile-time binding is also known as static
    binding.
  • For the statement in Line 7, the actual parameter
    is of the type derivedClass.
  • When the body of the function two executes,
    logically the print function of object two should
    execute, which is not the case.
  • C corrects this problem by providing the
    mechanism of virtual functions.
  • The binding of virtual functions occurs at
    program execution time, not at compile time.
  • This kind of binding is called run-time binding.
  • In run-time binding, the compiler does not
    generate code to call a specific function
    instead, it generates enough information to
    enable the run-time system to generate the
    specific code for the appropriate function call.
  • Run-time binding is also known as dynamic binding.

68
  • class baseClass
  • public
  • virtual void print() //virtual function
  • baseClass(int u 0)
  • private
  • int x
  • class derivedClass public baseClass
  • public
  • void print()
  • derivedClass(int u 0, int v 0)
  • private
  • int a

69
  • If we execute the previous program with these
    modifications, the output is as follows.
  • Output
  • In baseClass x 5
  • In derivedClass In baseClass x 3
  • In derivedClass a 15
  • Calling the function callPrint
  • In baseClass x 5
  • In derivedClass In baseClass x 3
  • In derivedClass a 15

70
  • //Chapter 15 Virtual Functions
  • include ltiostreamgt
  • include "classExtTestVirtual.h"
  • using namespace std
  • void callPrint(baseClass p)
  • int main()
  • baseClass q //Line 1
  • derivedClass r //Line 2
  • q new baseClass(5) //Line 3
  • r new derivedClass(3,15) //Line 4
  • q-gtprint() //Line 5

71
  • void callPrint(baseClass p)
  • p-gtprint()
  • Output
  • In baseClass x 5
  • In derivedClass In baseClass x 3
  • In derivedClass a 15
  • Calling the function callPrint
  • In baseClass x 5
  • In derivedClass In baseClass x 3
  • In derivedClass a 15

72
  • //Chapter 15 Virtual Functions and value
    parameters
  • include ltiostreamgt
  • include "classExtTestVirtual.h"
  • using namespace std
  • void callPrint(baseClass p)
  • int main()
  • baseClass one(5) //Line 1
  • derivedClass two(3, 15) //Line 2
  • one.print() //Line 3
  • two.print() //Line 4
  • coutltlt" Calling the function callPrint
    "

73
  • void callPrint(baseClass p) //p is a value
    parameter
  • p.print()
  • Output
  • In baseClass x 5
  • In derivedClass In baseClass x 3
  • In derivedClass a 15
  • Calling the function callPrint
  • In baseClass x 5
  • In baseClass x 3

74
  • THE ADDRESS OF OPERATOR AND CLASSES
  • The address of operator is also used to create
    aliases to an object.
  • Consider the following statements
  • int x
  • int y x
  • Both x and y refer to the same memory location.
  • y is like a constant pointer variable.
  • The statement
  • y 25
  • sets the value of y and hence of x to 25.
    Similarly the statement
  • x 2 x 30
  • updates the value of x and hence of y.

75
  • The address of operator, , can also be used to
    return the address of private data members of a
    class. However, if we are not careful this can
    result in serious errors in the program.
  • //header file testadd.h
  • ifndef H_testAdd
  • define H_testAdd
  • class testAddress
  • public
  • void setX(int)
  • void printX() const
  • int addressOfX() //this function returns
    the
  • //address of the private data
    member
  • private
  • int x
  • endif

76
  • //Implementation file testAdd.cpp
  • include ltiostreamgt
  • include "testAdd.h"
  • using namespace std
  • void testAddresssetX(int inX)
  • x inX
  • void testAddressprintX() const
  • coutltltx
  • int testAddressaddressOfX()
  • return x

77
  • //Test program
  • include ltiostreamgt
  • include "testAdd.h"
  • using namespace std
  • int main()
  • testAddress a
  • int y a.addressOfX()
  • a.setX(50)
  • coutltlt"x in class testAddress "
  • a.printX()
  • coutltltendl
  • y 25
  • coutltlt"After y 25, x in class testAddress "
  • a.printX()
  • coutltltendl

78
  • ifndef H_testAdd
  • define H_testAdd
  • class testAddress
  • public
  • void setX(int)
  • void printX() const
  • const int addressOfX() //this function
    returns the
  • //address of the
    private data
  • //member
  • private
  • int x
  • endif
  • const int testAddressaddressOfX()
  • return x

79
  • The definition of the function addressOfX in the
    implementation file is
  • const int testAddressaddressOfX()
  • return x
  • The same program now will generate compile time
    error.
Write a Comment
User Comments (0)
About PowerShow.com