Hi!I'm working on a project at university and I need your help!I have to make a class Image that will represent the image's pixels either using th RGB scale or the gray one. The existing classes are RGBpixel and graypixel.I think I have to do it using templates,so generic functions such as getPixel and setPixel that will impose the value of a pixel at particular coordinates. Can you tell me how I create array of objects in the class "Image" and then the generic functions?Do I have to use pointers to the objects?I'm confunsed..:((if you have an idea,it would be really helpful!

Depending on the problem use a template parameter. But the trick is to use a common base class.

Imagen this:

class pixelBase 
{
  // ... Stuff ...
  virtual void setPixel();
};

template<int pixelType>
class pixelItem : public pixelBase
{
    virtual void setPixel(...); 
};

This doesn't always give you the simplest architecture and you have to be careful that you are not better off with an abstract base class and two/more specialization classes.

I think that from your initial comment you are likely to not want the template solution but that is a guess.

Finally: What you do is have pointers to the base class e.g. pixelBase* displayArea[NX][NY]; Also I am assuming you know all the usual stuff about virtual destructors etc. If you are unsure, post some code and we normally review it. (sort of).

You're question (to me) isn't very clear.

Can you show us what you have done so far (including a better explanation of what you think has to be done) and point out with what exactly you are having problems.

Thank you for your quick answers!So, since I wasnt so clear, I'll try to explain it better. I have to represent an image with pixels either in Rgb scale or Gray scale. Certainly the professor would like us to use templates. So, I'm thinking a class "Image" that will take as parameters in the costructor function the dimensions of the image(also the prof has mentioned that). So then, there are also the classes "RGBpixel" and "GrayPixel", each one of them describes the pixel's color. The first has private the "_r","_g" and "_b" variables and the second one the "_gray". We have to use generic functions "getPixel" and "setPixel" that according to the type of description will return the variables needed for the color. So, these generic functions will be a part of the class "image"??And then there has to be a function getImagePixels in the class Image that will return an array of Pixels. But I dont know.This has to return array of objects of type "RGBpixel" or "GrayPixel" so also this is gonna be a template. And I have to use 2-dimension array??How do I do it in C++?If you can,even a general architecture of the problem would be helpful. or some kind of code...thanks!:D...(life is difficult when you don't know c++:(..)

Ok from your reply [paragraphs please!!] You haven't been that clear.
So I am going to guess what the problem is and what is in my opinion the optimal solution.

First : The difference between a a couple of template instances e.g.

class Image
{
   public:

        void setPixel(const int,const int,const RGB&);
        void setPixe(const int,const int,const Grey&); 
};

and

class Image
{
   public:

       template<typename T>
        void setPixel(const int,const int,const T&);
};

is relatively little. Both are resolved in the same way (assuming that you have instances, were T is RGB and T is Grey).

However, templates can be used for a return value.

class Image
{
        template<typename T>
        T getPixel(const int,const int) const;
};

but this is not acceptable C++

class Image           // BAD CODE:
{
    Grey getPixel(const int,const int) const;
    RGB getPixel(const int,const int) const; 
 };

So back to your problem, in producing an array you want the pixel type to be completely runtime specified, maybe in the array half are grey/ half are RGB (e.g. adding two images together). This is polymorphism. Produce a base class Pixel and specialize down to the RGB/Grey types using public inheritence. Make the destructors virtual and any other polymorphic functions, e.g. get/set/display etc.

You might have this

class Pixel
{ 
    public:
     virtual ~Pixel();
    virtual void display(Screen&) const =0;      // abstract class
};
class RGB : public Pixel
{
    public: 
       virtual ~RGB();
       virtual void display(Screen&)  const;
};
class Grey : public Pixel
{
public:
    virtual ~Grey();
    virtual void display(Screen&) const; 
};

Obviously the classes need fleshing out and maybe a different architecture is good for your problem.

Then the only area for templates would then be in the Image class, you would have something like this

class Image
{
   private:    
        Image** Array;    . 

   public:

        template<typename T>
        T** getImageMap()  const; 

        template<typename T>
        T getPixelValue(const int,const int) const; 
};

Image** Array may well be a better managed array boost::multi_array/ or a matrix class.

getImageMap would convert the Image storeage in Array into the required pixel form.
Great care MUST be taken about the getImageMap memory management (i.e who manages.) and a smart pointer of some sort or an managed class (boost::multi_array) is likely to be a good solution.

Likewize getPixelValue would return a pixel in the given form.

Note:: In the code useage you have to specify the template return type like this:

Image A;

// .. stuff
RGB p=A.getPixelValue<RGB>(4,5);

This does not work since the compiler is not allowed to look past the determination point (=) since a cast etc could be used.

// NOT c++
Image A;

// .. Stuff
RGB p=A.getPixelValue(4,5);         // Ill defined 

The key to these problems is always separation. Write the Image separately from the pixel part.

Hope that helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.