1 / 40

Pointers

Pointers. Getting the address of a Variable. The address operator (&) returns the memory address of a variable. Figure1. letter. number. amount. 1200. 1201. 1203. Program 1.

delu
Download Presentation

Pointers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pointers

  2. Getting the address of a Variable • The address operator (&) returns the memory address of a variable. Figure1 letter number amount 1200 1201 1203

  3. Program 1 // This program uses the & operator to determine a variable’s address and the sizeof operator to determine its size. #include <iostream.h> void main(void) { int x = 25; cout << "The address of x is " << &x << endl; cout << "The size of x is " << sizeof(x) << " bytes\n"; cout << "The value in x is " << x << endl; }

  4. Program Output The address of x is 0x8f05 The size of x is 2 bytes The value in x is 25

  5. Pointer Variables • Pointer variables, which are often just called pointers, are designed to hold memory addresses. • With pointer variables you can indirectly manipulate data stored in other variables.

  6. Pointers are useful for the following: • Working with memory locations that regular variables don’t give you access to • Working with strings and arrays • Creating new variables in memory while the program is running • Creating arbitrarily-sized lists of values in memory

  7. Program 2 // This program stores the address of a variable in a pointer. #include <iostream.h> void main(void) { int x = 25; int *ptr; ptr = &x; // Store the address of x in ptr cout << "The value in x is " << x << endl; cout << "The address of x is " << ptr << endl; }

  8. x 25 ptr 0x7e00 Address of x: 0x7e00 Program Output The value in x is 25 The address of x is 0x7e00

  9. Program 3 // This program demonstrates the use of the indirection // operator. #include <iostream.h> void main(void) { int x = 25; int *ptr; ptr = &x; // Store the address of x in ptr cout << "Here is the value in x, printed twice:\n"; cout << x << " " << *ptr << endl; *ptr = 100; cout << "Once again, here is the value in x:\n"; cout << x << " " << *ptr << endl; }

  10. Program Output Here is the value in x, printed twice: 25 25 Once again, here is the value in x: 100 100

  11. Pointer Arithmetic • Some mathematical operations may be performed on pointers. • The ++ and – operators may be used to increment or decrement a pointer variable. • An integer may be added to or subtracted from a pointer variable. This may be performed with the +, - +=, or -= operators. • A pointer may be subtracted from another pointer.

  12. Program 4 // This program uses a pointer to display the contents // of an integer array. #include <iostream.h> void main(void) { int set[8] = {5, 10, 15, 20, 25, 30, 35, 40}; int *nums, index; nums = set; cout << "The numbers in set are:\n"; for (index = 0; index < 8; index++) { cout << *nums << " "; nums++; }

  13. Program continues cout << "\nThe numbers in set backwards are:\n"; for (index = 0; index < 8; index++) { nums--; cout << *nums << " "; } }

  14. Program Output The numbers in set are: 5 10 15 20 25 30 35 40 The numbers in set backwards are: 40 35 30 25 20 15 10 5

  15. Pointers to Objects • Question arises that how we access pointers to objects. • In normal scenarios a class object access the class functions members using the dot (.) membership-access operator • But when a class have pointer objects than we use an equivalent but more concise approach is furnished by the membership-access operator, which consists of a hyphen and a greater-than sign • the -> operator works with pointers to objects in just the same way that the . operator works with objects • objptr->getdist();

  16. Program 5 #include <iostream>using namespace std;class myclass {int i;public:myclass(int j) {      i = j;   }intgetvalue() {      return i;   }};int main(){myclassobj(88), *objPointer;objectPointer = &obj; cout << objPointer->getvalue();  return 0;}

  17. PolyMorphism

  18. Polymorphism • Polymorphism is another most important feature of object oriented programing. • In polymorphism, the member functions with the same name are defined in each derived class and also in the base class. • Polymorphism is used to keep the interface of base class to its derived classes.

  19. Poly means many and morphism means form. Polymorphism, therefore, is the ability for objects of different classes related by inheritance to response differently to the same function call. • Polymorphism achieved by means of virtual functions. • It is reduced possible by the fact that one pointer to base class object may also point to any object of its derived class.

  20. Static Binding/Early Binding • Early binding refers to matching process of a function call with its correct function definition at compile time. • By default c++ performs static binding • Static binding is also known as Early binding

  21. e.g #include<iostream> class parent{ private: Public: Void show() { Cout<<“Parent Class”<<endl; } };

  22. Class child: public parent { public: Void show() { cout<<“Child class”<<endl; } }; void message(parent& pobj) { pobj.show(); }

  23. int main(intargc, char *argv[ ]) { child obj; Message(obj); System(“PAUSE”); } Output: parentclass

  24. Dynamic/ Late Binding • Dynamic binding refers to matching process of a function call with its correct function definition at run time. • Dynamic binding is also known as Late binding. • If programmer desires to instruct the C++ compiler to use dynamic binding for a specific function then type a keyword virtual at the start of function definition.

  25. Virtual Function • Virtual function is a member function which you can redefined for other derived classes, and can ensure that the compiler will call the redefined virtual function for an object of the corresponding derived class even if you call that function with a pointer or reference to a base class of the object.

  26. A class that declares or inherits a virtual function is called polymorphic class. • In C++ virtual function is defined by using a keyword virtual at start of function definition.

  27. e.g #include<iostream> class parent{ private: public: virtual void show() { cout<<“Parent Class”<<endl; } };

  28. Class child: public parent { public: Void show() { cout<<“Child class”<<endl; } }; void message(parent& obj) { obj.show(); }

  29. int main(intargc, char *argv[ ]) { child obj; Message(obj); System(“PAUSE”); } Output: Child class

  30. Difference b/w virtual and non virtual member function • Virtual functions are resolved dynamically (means at run time) • Non virtual functions resolved statically(means at compile time)

  31. Program1 without using virtual function #include<iostream> class parent{ private: public: void output(){ cout<<“parent Class”<<endl; } };

  32. Class child1:public parent{ Public: Void output(){ Cout<<“child1”<<endl; } };

  33. Class child2:public parent{ Public: Void output(){ Cout<<“child2”<<endl; } };

  34. int main(intargc, char *argv[ ]) { parent *pt; child1 c1; child2 c2; pt=&c1; pt->output(); pt=&c2; pt->output(); System(“PAUSE”); }

  35. Output • Parent class • Parent class

  36. Program2 with the use of virtual function #include<iostream> class parent{ private: public: virtual void output()=0{ cout<<“parent Class”<<endl; } };

  37. Class child1:public parent{ Public: Void output(){ Cout<<“child1”<<endl; } };

  38. Class child2:public parent{ Public: Void output(){ Cout<<“child2”<<endl; } };

  39. int main(intargc, char *argv[ ]) { parent *pt; child1 c1; child2 c2; pt=&c1; pt->output(); pt=&c2; pt->output(); System(“PAUSE”); }

  40. Output • Child1 class • Child2 class

More Related