csi 3125, preliminaries, page 1 polymorphism, virtual function

Download CSI 3125, Preliminaries, page 1 Polymorphism, Virtual function

If you can't read please download the document

Upload: merry-wilkinson

Post on 17-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

  • Slide 1
  • CSI 3125, Preliminaries, page 1 Polymorphism, Virtual function
  • Slide 2
  • CSI 3125, Preliminaries, page 2 Polymorphism Polymorphism gives different meanings or functions to the operators or functions. A single function usage or an operator functioning in many ways can be called polymorphism. Polymorphism refers to codes, operations or objects that behave differently in different contexts. Eg 1 + 10 The above refers to integer addition. The same + operator can be used with different meanings with strings: "Exforsys" + "Training A single operator + behaves differently in different contexts such as integer, or strings referring the concept of polymorphism.
  • Slide 3
  • CSI 3125, Preliminaries, page 3 Polymorphism Types of Polymorphism: 1. Compile time polymorphism (Also called as static binding) 2. Run time polymorphism. (Also called as late binding.)
  • Slide 4
  • CSI 3125, Preliminaries, page 4 Polymorphism Types of Polymorphism: 1. Compile time polymorphism (Also called as static binding) 2. Run time polymorphism. (Also called as late binding.)
  • Slide 5
  • CSI 3125, Preliminaries, page 5 Polymorphism compile time it is called function overloading or Operator overloading. For example, a program can consist of two functions where one can perform integer addition and other can perform addition of floating point numbers but the name of the functions can be same such as add. The function add() is said to be overloaded. Two or more functions can have same name but their parameter list should be different either in terms of parameters or their data types. The compiler will select the right function depending on the type of parameters passed at compile time. If function binding is done during compile time its called compile time polymorphism. The code gets generated to call that particular function during compile time itself
  • Slide 6
  • CSI 3125, Preliminaries, page 6 Polymorphism Run time polymorphism is it doesnt bother about which function to call for that corresponding function call. The binding of that function happens during run time. Virtual functions makes use of this runtime polymorphism concept.
  • Slide 7
  • 7 Pointers A pointer is a variable that points to or references a memory location in which data is stored. Each memory cell in the computer has an address that can be used to access that location so a pointer variable points to a memory location, can access and change the contents of this memory location via the pointer.
  • Slide 8
  • 8 Pointers Pointer declaration: A pointer is a variable that contains the memory location of another variable. The syntax is as shown below. Start by specifying the type of data stored in the location identified by the pointer. The asterisk tells the compiler that you are creating a pointer variable. Finally you give the name of the variable. data type * variable name; Example: int *ptr; float *string;
  • Slide 9
  • 9 Pointers Reference operator (&) Once we declare a pointer variable we can point it to something that we can do this by assigning to the pointer the address of the variable. example: ptr=# This places the address where num is stores into the variable ptr. If num is stored in memory 21260 address then the variable ptr has the value 21260.
  • Slide 10
  • 10 Pointers /* A program to illustrate pointer declaration*/ class A { int s,*ptr; public: void show() { s=10; ptr=&s; couta[i]; } cout