Loading....
Coupon Accepted Successfully!

 

Pointer To Object

Similar to variables, objects also have an address. A pointer can point to a specified object. The following program illustrates this:
 

13.11 Write a program to declare an object and pointer to the class. Invoke the member functions using pointer.

#include<iostream.h>

#include<conio.h>

class Bill

{

int qty;

float price;

float amount;

public :

void getdata (int a, float b, float c)

{

qty=a;

price=b;

amount=c;

}

void show()

{

cout<<“Quantity : ” <<qty <<“\n”;

cout<<“Price : ” <<price <<“\n”;

cout<<“Amount : ” <<amount <<“\n”;

}

};

int main()

{

clrscr();

Bill s;

Bill *ptr =&s;

ptr->getdata(45,10.25,45*10.25);

(*ptr).show();

return 0;

}

OUTPUT

Quantity : 45

Price : 10.25

Amount : 461.25


Explanation:
In the above program, the class Bill contains two float and one int members. The class Bill also contains the member function getdata() and show() to read and display the data. In function main(), s is an object of class Bill, and ptr is a pointer of the same class. The address of object s is assigned to pointer ptr. Using pointer ptr with arrow operator (->) and dot operator (.), members and functions are invoked. The statements used for invoking functions are as given below.
 

ptr->getdata (45,10.25,45*10.25);

(*ptr).show();


Here, both the pointer declarations are valid. In the second statement,
ptr is enclosed in brackets, because the dot operator (.) has higher precedence as compared with the indirection operator (*). The output of the program is as shown above.
 

13.12 Write a program to create dynamically an array of objects of class type. Use new operator.

#include<iostream.h>

#include<conio.h>

class Bill

{

int qty;

float price;

float amount;

public :

void getdata (int a, float b, float c)

{

qty=a;

price=b;

amount=c;

}

void show()

{

cout<<“Quantity : ” <<qty <<“\n”;

cout<<“Price : ” <<price <<“\n”;

cout<<“Amount : ” <<amount <<”\n”;

}

};

int main()

{

clrscr();

Bill *s= new Bill[2];

Bill *d =s;

int x,i;

float y;

for (i=0;i<2;i++)

{

cout<<“\nEnter Quantity and Price : ”;

cin>>x >>y;

s->getdata(x,y,x*y);

s++;

}

for (i=0;i<2;i++)

{

cout<<endl;

d->show();

d++;

}

return 0;

}

OUTPUT

Enter Quantity and Price : 5 5.3

Enter Quantity and Price : 8 9.5

Quantity : 5

Price : 5.3

Amount : 26.5

Quantity : 8

Price : 9.5

Amount : 76


Explanation:
In the above program, the class Bill is similar to that in the previous example. In main(), using new memory allocation operator, the memory required for two objects is allocated to pointer s, that is, 10 bytes. The first for loop accepts the data through the keyboard. Immediately after this, the data are sent to the member function getdata(). The pointer s is incremented. After incrimination, it points to the next memory location of its type. Thus, two records are read through the keyboard. The second for loop is used to display the contents on the screen. Here, the function show() is invoked. The logic used is similar to that used in the first loop. The functions are invoked using pointers, and this has been explained in the previous example.




Test Your Skills Now!
Take a Quiz now
Reviewer Name