08448380779 Call Girls In Friends Colony Women Seeking Men
Pointers,virtual functions and polymorphism cpp
1.
2. POLYMORPHISM
POINTERS
POINTERS TO OBJECT
THIS POINTER
POINTERS TO DERIVED CLASSES
VIRTUAL FUNCTIONS
PURE VIRUAL FUNCTIONS
3. Polymorphism is one of the crucial features of OOP.It simply means one
name, multiple forms. We have already seen how the concept of
polymorphism is implemented using the overload functions and
operators.
EXAMPLE:
Class A
{
Int x;
Public:
Void show() {……}//show in base class
};
Class B:public A
{
Int y;
Public:
Void show() {….}//show in derives class
};
5. A pointer is a derived data type that refers to another data variable by
storing the variable is memory address rather than data declaring and
initialing pointers.
DECLARING AND INITIALIZING POINTER
We can declare a pointer variable similar to other variables in C++.
MANIPULATION OF POINTER
We can manipulate a pointer with the indirection operator . I e., which is
also known as deference operator.
Syntax: data_ type * pointer _variable
Syntax:*pointer _ variable
6. POINTER EXPRESSION AND POINTER
ARITHMETIC
There are a substantial number of arithmetic operations that be Performed
with pointers.
o A pointer can be incremented(++)(or)decrement(--).
o Any integer can be added to or subtracted from a pointer.
o One pointer can be subtracted from another .
Example:
int a[6]; k
int*aptr;
aptr=&a[0];
USING POINTERS WITH ARRAYS AND STRINGS
Pointers is one of the efficient to access elements of an
array. pointers are useful to allocate arrays dynamically.
I .e, we can decide the array size at run time.
7. We can declare the pointers to array as follows
int*nptr ;
nptr =number[0]; (or) nptr=number;
ARRAY OF POINTER
The array of pointers represents a collection of address. by declaring array of
pointers we can save a subtantial amount of memory space.
POINTERS AND STRINGS
C++ also allows us to handle the special kind of arrays, I e, strings with pointers.
we known that a string is one dimensional array of character, which start with
the index “0” and end with the null character in C++.
Syntax: Int*in array[10]
Syntax: char num[]=”one”;
Const char *number=”one”;
8. POINTERS TO FUNCTIONS
The pointer to function is known as callback function, we can use these
function pointers to refer to a function. using function pointers ,we can allow
a C++ program to select a function dynamically at run time.
POINTER USING C++:
#include<iostream.h>
#include<conio.h>
Void main( )
{
Int a,*ptr1;
Ptr1=&a;
Cout<<“The address of a:”<<ptr1<<“/n”;
Getch( );
}
Syntax: data_type(*function_ name);
9. As started earlier ,a pointer can point to an object created by a
class item x; when item is and x is an object defined to be of
type item. similarly we can define a pointer.
Example:
class item
{
Int code;Float price;
Public:
Void getdata(int a, float b)
{Code=a; price=b;}
Void show(void)
{cout<<“code:”<<code<<“/n”;
Cout<<“price:”<<price<<“/n”;}
};
item*it_ ptr;
10. Example:
#include< iostream. h>
#include<conio.h>
Class test
{private:
Int x;
Public:
Void set x(int x)
{this->x=x;}
void print(){ cout<<“x=“};
int main()
{
Test obj;
Int x=20;
Obj. setx(x);
Obj . print();
Return 0;}
This this pointer is a constant
pointer that holds the memory
address of the current object
,The this pointer is not
available in static member
functions can be called
without any object. static
member functions can be
called with class name.
OUTPUT: X=20
11. All derived classes inherit properties from the common
base class. Pointers can be declared to the point base or
derived class. pointers to objects of the base class are type
compatible with pointers to objects of derived class .A
base class pointer can point to objects of both the baseand
derived class.
B*cptr; //pointer to class b type variable
B b; / /base object
D d; //derived object
Cptr=&b; //cptr points to object b
Cptr=&d; //cptr points to object d
12. A virtual function is a member function that you expect to
Be refined in derived classes. When you refer to a derived
Class objects using a pointer or a reference to the base
class, you can call a virtual function for that object and
execute the derived class's version of the function.
13.
14. Pure virtual function is also known as abstract
function. A class with at least one Pure virtual
function or abstract function is called abstract
class. We can’t create an member function
of abstract class will be invoked by derived class
object.