Pointer within a class:
An example which asks for number of seven subjects and according to number of subjects reserves memory at run time. Then, it asks for marks for each subject and finally, calculates percentage of the student.
i.e.
class X
{ private:
float *ptr; int num;
public:
void get_numbers()
{ cout<<"Enter a number of subjects"<<endl;
cin>>num; }
void get_marks();
void calc_display();
} ;
void X::get_marks()
{ cout<<"Enter marks of "<< num<<" subjects"<<endl;
ptr=new float[num];
for (int i=0;i<num;i++)
cin>>*(ptr+i);
}
void X::calc_display()
{ float sum=0, per;
for(int j=0;j<num;j++)
sum=sum+*(ptr+j);
per=sum/num;
cout<<"\nThe percentage is :"<<per;
delete [] ptr; }
void main()
{ X x;
x.get_numbers();
x.get_marks();
x.calc_display(); }