fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. typedef float PixelValue;
  6.  
  7. class IImageData{
  8. public:
  9. virtual PixelValue getData(int index) = 0;
  10. };
  11.  
  12. class ImageData8 : public IImageData{
  13. vector<unsigned char> buffer_;
  14. public:
  15. ImageData8():
  16. buffer_(10,128)
  17. {}
  18. virtual PixelValue getData(int index){
  19. return (float)buffer_[index] / 256.0f;
  20. }
  21. };
  22.  
  23. class ImageData16 : public IImageData{
  24. vector<unsigned short> buffer_;
  25. public:
  26. ImageData16():
  27. buffer_(10,65535)
  28. {}
  29.  
  30. virtual PixelValue getData(int index){
  31. return (float)buffer_[index] / 65535.0f;
  32. }
  33. };
  34.  
  35. int main() {
  36. // your code goes here
  37. ImageData8 i8;
  38. ImageData16 i16;
  39. vector<IImageData*> images;
  40. images.resize(2);
  41. images[0] = &i8;
  42. images[1] = &i16;
  43.  
  44. for(auto img : images){
  45. cout << img->getData(0) <<endl;
  46. }
  47. return 0;
  48. }
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
0.5
1