Use class pointer and class array together : object pointer « Class « C++ Tutorial






#include "iostream.h"

class MyClass
{
private:
  int Price;
  int Count;
  long Total;
public:
  void Input(int P,int C)
  {
   Price=P;
   Count=C;
  }
  void MyClass::Compute()
  {
    Total=(long) Price*Count;
  }
  void MyClass::Print(){
    cout<<"Price="<<Price<<"  Count="<<Count <<"   Total="<<Total<<"\n";
  }
};

int main()
{
       MyClass  *ob;
       
       ob=new MyClass[6];
       ob[0].Input(5,0);
       ob[1].Input(3,5);
       ob[2].Input(1,0);
       ob[3].Input(5,20);
       ob[4].Input(4,0);
       ob[5].Input(8,5);
       
       for(int i=0;i<6;i++)
       ob[i].Compute();
       for(int i=0;i<6;i++)
          ob[i].Print();
       
       delete ob;
}
Price=5  Count=0   Total=0
Price=3  Count=5   Total=15
Price=1  Count=0   Total=0
Price=5  Count=20   Total=100
Price=4  Count=0   Total=0
Price=8  Count=5   Total=40








9.34.object pointer
9.34.1.Use class pointer and class array together
9.34.2.Use & to get object address
9.34.3.Incrementing and decrementing an object pointer
9.34.4.Pointers as data members
9.34.5.Pointers to Class Members
9.34.6.To use a pointer to the object, you need to use the ->* operator
9.34.7.Use new to allocate memory for a class pointer
9.34.8.Passing References to Objects
9.34.9.normal functions accessed from pointer
9.34.10.Sort person objects using array of pointers