Mega Code Archive

 
Categories / C++ Tutorial / Class
 

Use class pointer and class array together

#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