Page 1 :
Pointers & Polymorphism in C++, , , , GO) J, , , , Pointers and Polymorphism in C++, > Unit Outcomes, — Create C++ programs to perform the given arithmetic operations using pointers., — Use function overloading to solve the given problem., — Use operator overloading to solve the given problem., — Implement run time polymorphism using virtual functions in the given C++ program., > Topics and Sub-topics, — Concepts of Pointer : Pointer declaration, Pointer operator, address operator, Pointer arithmetic., — Pointer to Object : Pointer to Object, this pointer, Pointer to derived class. Virtual Base Class and, Abstract Class, — Introduction of Polymorphism, Types of Polymorphism,, = Compile time Polymorphism : Function overloading, operator overloading, overloading of unary and, binary operators, Rules for operator overloading., , , , — Runtime polymorphism : Virtual functions, rules for virtual functions, pure virtual function., , , , , , Syllabus Topic : Concept of Pointer : Pointer Declaration | — Pointers are variables that are used to store the address of, 2 another variable., , — Address of a variable is the memory location number which, , is allotted to the variable. The memory addresses are, D> (8-14, 5-415, W415, WeI7) 0,1,23... and so on up to the capacity of the memory. The, address is normally displayed in hexadecimal form., , , , — Hexadecimal form is a representation of number somewhat, similar to binary number system studied in chapter 1, Here, four binary digits are combined together to form a, hexadecimal number., , 3 Pointers unlike other variables do not store values. As stated, they store the address of other variables., , ee It is already mentioned in the first statement that pointers are, also variables. Hence, we can also have a pointer that is, pointing to another pointer.
Page 3 :
“p”, as it is associated with a “*” sign., statement is very simple wherein the value 125 is, , statement i.e. “p=&a” uses of the address Operator., nt assigns the address of the variable “a” to the, jable “p”. As already seen address of a variable is, ory location (number), where it is stored., , statement prints the value of the variable “a”, , 125 is displayed as shown in the output., “cout” statement displays the value of the, e variable “p” is a pointer, the value, ‘This is the address of the variable, , gia ar & salto. teen hem, value like:, , en oes
Page 4 :
lect Oriented Programming using C++ (MSBTE. 7-4 Pointers & Polymorphism jn cw, , , , , = The value of “p1” will be the address of the pointer “p”, since, the address of operator (“&") is used in the program to assign, the address of the pointer “p” to the variable pl,, , — The next statement displays the value of the variable pointed Explanation :, Ty te palais ws asetnan e provions program - Note : when a pointer is incremented or added by 1, it, seen athe ater eociee tp tse” will’ cieplay, the actually increments by the number equal to the size of the, value of the variable pointed by the pointer “pl”. But the data type for which it is a pointer. For example; if an integer, , variable “p1” points to another address (or variable) i.e. the, pointer “p”, which has the address of variable “a”. Hence, the, address of variable “a” stored in the pointer variable “p” is : A, displayed on the screen, - The same concept is demoi.strated in the above program, _ Initially; the addresses of “a” und “b” are displayed as those, , are the contents of the pointers. Thereafter; when the Pointers, , pointer is incremented, it will increment by 2, while if float, type pointer is incremented it will increment by 4,, , — Finally; to access the actual yalue pointed by the pointer “pi, through the pointer “p” is displayed. This is the variable “a”,, , hence the value of this variable i.e. 125 is displayed, Steelngeiteutr Dey inciment by Zand 4 Eessetely foe, a “a” and “b”. Hence, the new values that are displayed are, Syllabus Topic : Pointer Arithmetic addition of 2 and 4 respectively w.r.t their previous values, 7.2.3 Pointer Arithmetic: Increment and en, Decrement Operators Operation on ae ets 17, Pointer Variables > Program 7.2.4 : Write the output of the following, , > (w-14,5-15,s-16) | __ Program., , » Program 7.2.3 : Write the output of the following, program.
Page 5 :
vy Object Oriented Programming using C++ (MSBTE) 7-5 Pointers & Polymorphism inGes, , , , Note : The name of the array works as a pointer to the array., The name of the array always points to the first element of, the array., , ‘The values of “ali], *(a+i) and *(i+a) are same, since “a” is a, pointer pointing to the first element in the array. When the, yalue of the variable “i" is “0”, the location pointed by *(a+i), and *(i+a) is the first location of the array,, , , , , , , , , , , Similarly, when the value of “i” is “1”, the location pointed, by *(@ti) and *(i+a) is the second location of the array., e, for all the three statements the value displayed in the, is the same., , Passing Pointers to Functions, , ¢ 7.2.5 : Write a program to add two numbers, function. Pass the pointers to the variables as, ce to the functions., , Explanation :, , Note ; To pass the parameters using pointers we pass the, address of the variables using the address of operator ("8")., And hence to accept this address in the function we must, have pointers in the argument list, Remember in this case, when address of the variable is passed, the value of the actual, parameters can be altered, We will see this concept in more, details in the subsequent programs., , As discussed, the pointers are used to accept the address of, the variables passed from the main function. the values of, these variables are accessed using the pointers in the function, “sum”,, , The sum is calculated and is redisplayed in the function itself., , 7.2.6, , Call by Value and Call by Reference, > (W-14, S-15, W-15, S-16, S-17, W-17)