Wednesday, February 16, 2011

Pointer within a class


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();        }

1 comment:

  1. Hard Rock Hotel Casino & Spa - Mapyro
    Hard Rock Hotel Casino & Spa is an 춘천 출장마사지 entertainment, 정읍 출장안마 gaming, dining and gaming destination in Gary, IN with 수원 출장안마 30000 square feet 구미 출장안마 of 수원 출장샵 gaming

    ReplyDelete