Pointers to Objects in C++


Pointers to Objects

A variable that holds an address value is called  a pointer variable or simply pointer.
Pointer can point to objects as well as to simple data types and arrays.
sometimes we dont know, at the time that we write the program , how many objects we want to creat. when this is the case we can use new to creat  objects while the program is running. new returns a pointer to an unnamed objects. lets see the example of student that wiil clear your idea about this topic



#include <iostream>
#include <string>
using namespace std;
class student
{
private:
            int rollno;
            string name;
public:
            student():rollno(0),name("")
            {}
            student(int r, string n): rollno(r),name (n)
            {}
            void get()
            {
                        cout<<"enter roll no";
                        cin>>rollno;
                        cout<<"enter  name";
                        cin>>name;
            }
            void print()
            {
                        cout<<"roll no is "<<rollno;
                        cout<<"name is "<<name;
            }
};
void main ()
{
            student *ps=new student;
            (*ps).get();
            (*ps).print();
            delete ps;
}