400 likes | 534 Views
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.
E N D
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 // 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; }
Program Output The address of x is 0x8f05 The size of x is 2 bytes The value in x is 25
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.
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
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; }
x 25 ptr 0x7e00 Address of x: 0x7e00 Program Output The value in x is 25 The address of x is 0x7e00
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; }
Program Output Here is the value in x, printed twice: 25 25 Once again, here is the value in x: 100 100
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.
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++; }
Program continues cout << "\nThe numbers in set backwards are:\n"; for (index = 0; index < 8; index++) { nums--; cout << *nums << " "; } }
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
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();
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;}
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.
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.
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
e.g #include<iostream> class parent{ private: Public: Void show() { Cout<<“Parent Class”<<endl; } };
Class child: public parent { public: Void show() { cout<<“Child class”<<endl; } }; void message(parent& pobj) { pobj.show(); }
int main(intargc, char *argv[ ]) { child obj; Message(obj); System(“PAUSE”); } Output: parentclass
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.
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.
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.
e.g #include<iostream> class parent{ private: public: virtual void show() { cout<<“Parent Class”<<endl; } };
Class child: public parent { public: Void show() { cout<<“Child class”<<endl; } }; void message(parent& obj) { obj.show(); }
int main(intargc, char *argv[ ]) { child obj; Message(obj); System(“PAUSE”); } Output: Child class
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)
Program1 without using virtual function #include<iostream> class parent{ private: public: void output(){ cout<<“parent Class”<<endl; } };
Class child1:public parent{ Public: Void output(){ Cout<<“child1”<<endl; } };
Class child2:public parent{ Public: Void output(){ Cout<<“child2”<<endl; } };
int main(intargc, char *argv[ ]) { parent *pt; child1 c1; child2 c2; pt=&c1; pt->output(); pt=&c2; pt->output(); System(“PAUSE”); }
Output • Parent class • Parent class
Program2 with the use of virtual function #include<iostream> class parent{ private: public: virtual void output()=0{ cout<<“parent Class”<<endl; } };
Class child1:public parent{ Public: Void output(){ Cout<<“child1”<<endl; } };
Class child2:public parent{ Public: Void output(){ Cout<<“child2”<<endl; } };
int main(intargc, char *argv[ ]) { parent *pt; child1 c1; child2 c2; pt=&c1; pt->output(); pt=&c2; pt->output(); System(“PAUSE”); }
Output • Child1 class • Child2 class