Advertisement
Guest User

C++: Having troubles with functions and dynamic memory alloc

a guest
Mar 19th, 2012
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. /*
  2.  
  3. ×××××××××××××××
  4. Datamembers:
  5. -mysiLength
  6. -mysiWidth
  7. -mysiArea
  8. _______________
  9. +constructor
  10. +mysiArea getter
  11. +toString
  12. ×××××××××××××××
  13.  
  14. */
  15.  
  16. #include <iostream>
  17.  
  18. using namespace std;
  19.  
  20. #include <string>
  21. #include <sstream>
  22. #include <algorithm>
  23.  
  24. class Rectangle
  25. {
  26. private:
  27.     short mysiLength;
  28.     short mysiWidth;
  29.     long myliArea;
  30.     short abs(short siArg)
  31.     {
  32.         if(siArg<0)
  33.             siArg=-siArg;
  34.  
  35.         return siArg;
  36.     }
  37.  
  38. public:
  39.     Rectangle(short siLength=0, short siWidth=0)
  40.     {
  41.         mysiLength=abs(siLength);
  42.         mysiWidth=abs(siWidth);
  43.         myliArea=mysiWidth*mysiLength;
  44.     }
  45.  
  46.     long getArea()
  47.     {
  48.         return myliArea;
  49.     }
  50.  
  51.     string toString()
  52.     {
  53.         stringstream ss;
  54.  
  55.         ss << myliArea << "=" << mysiLength << "*" << mysiWidth << endl;
  56.  
  57.         return ss.str();
  58.     }
  59. };
  60.  
  61. void io(short*, short*);
  62. void manage_Rectangle_arr(string, Rectangle * *, short*);
  63.  
  64. int main()
  65. {
  66.     Rectangle * * arr;
  67.     short siArrayL=1;
  68.  
  69.     manage_Rectangle_arr("make array", arr, &siArrayL);
  70.     //manage_Rectangle_arr("fill array", arr, &siArrayL);
  71.     //manage_Rectangle_arr("get areas", arr, &siArrayL);
  72.     //manage_Rectangle_arr("empty contents", arr, &siArrayL);
  73.     manage_Rectangle_arr("delete array", arr, &siArrayL);
  74. }
  75.  
  76. void io(short* siArg0, short*siArg1)
  77. {
  78.     cout << "Length: ";
  79.     cin >> *siArg0;
  80.     cout << "Width: ";
  81.     cin >> *siArg1;
  82. }
  83.  
  84. void manage_Rectangle_arr(string strCommand, Rectangle * *arr, short *siArrayL)
  85. {
  86.     if(strCommand=="make array")
  87.     arr = new Rectangle * [ *siArrayL];
  88.  
  89.     if(strCommand=="fill array")
  90.     for(short s=0; s< *siArrayL; ++s)
  91.     arr[s]= new Rectangle(1, 1);
  92.  
  93.     if(strCommand=="get areas")
  94.     for(short s=0; s< *siArrayL; ++s)
  95.     cout << arr[s]->getArea();
  96.  
  97.     if(strCommand=="empty contents")
  98.     for(short s=0; s< *siArrayL; ++s)
  99.     delete arr[s];
  100.  
  101.     if(strCommand=="delete array")
  102.     delete [] arr;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement