+ All Categories
Home > Documents > Pointerto Class

Pointerto Class

Date post: 05-Apr-2018
Category:
Upload: abhishek-modi
View: 222 times
Download: 0 times
Share this document with a friend

of 24

Transcript
  • 7/31/2019 Pointerto Class

    1/24

    Pointers

    10/02/11 & 14/02/11

  • 7/31/2019 Pointerto Class

    2/24

    Simple Program

    #include

    int main()

    {

    int var1 = 11;

    int var2 = 37;

    int var3 = 55;

    cout

  • 7/31/2019 Pointerto Class

    3/24

    Pointers

    Its a variable which holds the memory

    address of another variable

    The address of a variable is the address of the

    first byte of the memory block allocated to the

    variable.

    We can dig out the memory address of a

    variable by using address-of-operator( & ) also

    called reference operator.

  • 7/31/2019 Pointerto Class

    4/24

    #include

    int main()

    {int var1 = 11;

    int var2 = 37;

    int var3 = 55;

    cout

  • 7/31/2019 Pointerto Class

    5/24

    Declaration of Pointers

    By mentioning the type of the variable, followed byindirection operator, also called dereference operator(*), and the name of the pointer variable.

    The illustration of pointer declaration for int variablesare given below :

    int m, n ; // m and n are integer variables

    int *ptr = &n ;//ptr is pointer to any integer number

    int* ptr = &m ; //ptr now points to m.int* ptr = 0 ; // now ptr is initialized to 0.

  • 7/31/2019 Pointerto Class

    6/24

    Pointers to different data type

    float PI = 3.14159 ; float PIpf = Π

    float *pf ; points to

    char chchar ch = A ; points to

    pc = &ch;

    char *pc ;

    double D = 4328.6543 double D

    pd = &D;

    double *pd ; points to

    pf 3.14159

    pc A

    pd 4328.6543

  • 7/31/2019 Pointerto Class

    7/24

    Indirection or Dereference Operator

    The value of variable may be obtained from itspointer by using indirection operator also calleddereference operator(*).

    It is called indirection because it obtains the valueindirectly.

    An application of dereference operator is :

    int n = 60, *ptrn ;

    ptrn = &n ;

    *ptrn = 60 ; //use of dereference operator.

  • 7/31/2019 Pointerto Class

    8/24

    example

    #include

    using namespace std;

    int main(){ int n = 60;

    double B = 8.56 ;

    int* ptrn = &n;double *pB = &B;

    cout

  • 7/31/2019 Pointerto Class

    9/24

    Contd..

    cout

  • 7/31/2019 Pointerto Class

    10/24

    Expected output

    The expected output is :

    n = 60, B = 8.56

    The address of n is = 0012FF7C , ptrn = 0012FF7C*ptrn = 60, *pB = 8.56

    The address of B = 0012FF74 pB = 0012FF74

    Address of ptrn = 0012FF70

  • 7/31/2019 Pointerto Class

    11/24

    Pointers in Class

  • 7/31/2019 Pointerto Class

    12/24

    Declaration

    List L1 ;

    List *ptr;

    ptr = &(List) L1;The last line may be written as

    ptr = &List(L1) ;

  • 7/31/2019 Pointerto Class

    13/24

    Example

    #includeclass List

    {

    private :

    int x,y ;public :

    void Setdata(int a, int b)

    {

    cout a;

    coutb;

  • 7/31/2019 Pointerto Class

    14/24

    Contd..

    x = a;y = b;

    }

    void Display1()

    {

    cout

  • 7/31/2019 Pointerto Class

    15/24

    Contd..

    void Display3()

    {

    cout

  • 7/31/2019 Pointerto Class

    16/24

    Contd..

    void main()

    {

    List L1;

    List *ptr;

    ptr = &(List) L1;int i , j ;

    (*ptr).Setdata(i, j);

    ptr -> Display1();

    ptr -> Display2();(*ptr) .Display3();

    }

  • 7/31/2019 Pointerto Class

    17/24

  • 7/31/2019 Pointerto Class

    18/24

    Contd..

    void Setprice(int a, int b, int c)

    {

    x = a, y = b, z = c;

    }

    void Display2(){

    cout

  • 7/31/2019 Pointerto Class

    19/24

    Contd..

    void main()

    {

    List L1;

    List *ptr;

    ptr = &(List) L1;

    ptr -> Setprice(6,8,10);

    ptr -> Display1();

    cout

  • 7/31/2019 Pointerto Class

    20/24

    Contd..

    ptr -> Setprice(32,27,38) ;

    ptr -> Display1();

    cout

  • 7/31/2019 Pointerto Class

    21/24

    Pointer to objects of a class

    Let L1 be an object of class List, the pointer to

    L1 be declared and assigned as:

    List *ptr = &L1;

  • 7/31/2019 Pointerto Class

    22/24

    #include

    class List

    {

    private :int x, y, z ;

    public :

    List (int a, int b, int c) {

    x = a, y = b, z = c;

    }

  • 7/31/2019 Pointerto Class

    23/24

    Contd..

    void Display()

    {

    cout

  • 7/31/2019 Pointerto Class

    24/24

    Contd..

    void main()

    {

    int n = 0;List L1(12, 15, 27);

    cout


Recommended