C++ Pointers to object

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:

item x;

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:

item *it_ptr;

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:

item *it_ptr;

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
{
int code;
float price;
public:
void getdata(int a, float b)
{
code = a;
price = b;
}
void show(void)
{
cout « 'Code : " « code « << "Price: " « price « "\n\n";
}
};

let us declare an item variable x and a pointer ptr to x as follows

item *ptr=&x;

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

ptr->show();

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

item *ptr=new item[10]; // array of 10 objects

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
#include<iostream>
using namespace std;
class item
{
int code;
float price;
public:
void getdata(int a, float b)
{
code=a;
price=b;
}
void show()
{
cout << "Code:" << code << "\n";
cout << "price:" << price << "\n";
}
};
const int size=2;
int main()
{
item *p=new item[size];
item *d=p;
int x,i;
float y;
for(i=0;i < size; i++) {
cout << "input code and price for item" << i+1;
cin >> x >> y;
p->getdata(x,y);
p++;
}
for(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
#include <iostream>
#include<cstring>
using namespace std;
class city
{
protected:
char *name;
int len;
public:
city()
{
len=0;
name=new char[len+1];
}
void getname()
{
char *s;
s=new char[30];
cout << "Enter city name";
cin >> s;
len=strlen(s);
name=new char[len+1];
strcpy(name,s);
}
void printname()
{
cout << name << "\n";
}
};
int main()
{
city *cptr[10]; // array of 10 pointers to cities
int n=1;
int option;
do
{
cptr[n]=new city; // create 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;
}
while(option);
cout << "\n\n";
for(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

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise