C++ Pointers to object
We already seen in previous pages how to use pointers to access the class members. As stated earlier ,a pointer can point to an object created by a class. take the example in the below Consider the following statement:
where in the above example item is a class and x is an object defined to be of type item. Similarly we can define pointer it_ptr of type item as follows:
where item is a class and x is an object defined to be of type item. Similarly we can definer pointer it_ptr of type item as follows:
Object pointers are very very useful whenever creating objects at the run time. We also use an object pointer to access public members of an object. Consider an example of a class item defined as follows:
class item
{
code;
price;
:
getdata( a, b)
{
code = a;
price = b;
}
show(void)
{
cout « 'Code : " « code « << "Price: " « price « "\n\n";
}
};
|
let us declare an item variable x and a pointer ptr to x as follows
The pointer ptr is initialized with the address of x
we can refer to the member functions of item in two ways, one by using the dot operator and the object, and another by using the arrow operator and the object pointer.
The statements
x.getdata(100,75.50);
x.show();
are equivalent to
ptr->getdata(100,75.50);
ptr->show();
since *ptr is an alias name of x, for example we can also use the following method
(*ptr).show()
|
The parentheses are need because the dot operator has higher precedence than the indirection operator (*.)
We can also create the object using pointers and new operator as follows
item *ptr=new item;
(*ptr).show()
|
This statement allocates enough memory for the data member in the object structure and assigns the address of the memory space to ptr. Then ptr can be used to refer to the members as shown below
if the class has a constructor with arguments and also does not include an empty constructor, then we must assign the address of the memory space to ptr. Then we must supply the arguments when the object is created
we can also create an array of objects using using pointers. for example the statement
creates the memory for an array of 10 objects of the item class. Remember in such cases of the class contains constructors, by default it must also contain an empty constructor
POINTERS TO OBJECTS
<iostream>
using namespace std;
class item
{
code;
price;
:
getdata( a, b)
{
code=a;
price=b;
}
show()
{
cout << "Code:" << code << "\n";
cout << "price:" << price << "\n";
}
};
size=2;
main()
{
item *p=new item[size];
item *d=p;
x,i;
y;
(i=0;i < size; i++)
{
cout << "input code and price for item" << i+1;
cin >> x >> y;
p->getdata(x,y);
p++;
}
(i=0;i < size;i++)
{
cout << "Item:" << i+1<<"\n";
d->show();
d++;
}
}
|
the output :
input code and price for item1 40 500
input code and price for item2 50 600
Item:1
code:40
price:500
item:2
code:50
price:600
|
In the above program we create dynamically for two objects of equal size. but this may not be the case always
for example the objects of a class that contain character strings would not be of the same size. In such cases, we can define an array of pointers to objects that can be used to access the individual
objects
ARRAY OF POINTERS TO OBJECTS
<iostream>
<cstring>
using namespace std;
class city
{
*name;
len;
:
city()
{
len=0;
name=new char[len+1];
}
getname()
{
*s;
s=new char[30];
cout << "Enter city name";
cin >> s;
len=strlen(s);
name=new char[len+1];
strcpy(name,s);
}
printname()
{
cout << name << "\n";
}
};
main()
{
city *cptr[10];
int n=1;
int option;
do
{
cptr[n]=new city;
cptr[n]->getname();
n++;
cout << "Do you want to enter one more name?\n";
cout << "(Enter 1 for yes 0 for no):";
cin >> option;
}
(option);
cout << "\n\n";
(int i=1;i<=n;i++)
{
cptr[i]->printname();
}
}
|
output:
Enter city name: mumbai
Do you want to enter one more name?
(Enter 1 for yes 0 for no):1
Enter city name:delhi
Do you want to enter one more name?
(Enter 1 for yes 0 for no):1
Enter city name:lucknow
Do you want to enter one more name?
(Enter 1 for yes 0 for no):0
mumbai
delhi
lucknow
|