[Solved] How do I print out this string from within my class? [closed]


You can use operator-overloading and friend function.

Also you will need to create an object of type ColorPicker because Color is instance variable.

Note: Since Color is of string type hence, keep it as 1D array.

Following is working code. You can see it working here:

#include <iostream>
#include <string>
using namespace std;

class ColorPicker {
public:

        string Color[7] = { "red" , "orange", "yellow", "green", "blue", "indigo", "violet" };

        friend  ostream& operator << (ostream& out, const ColorPicker& ob)
        {
            for(int i=0; i<7;i++)
            {
                out<< (ob.Color[i])<<" | ";
            }
            return out;
        }

};

int main()
{
    ColorPicker ob;
    cout <<ob<<endl;
    system("pause"); 
    return 0;
}

solved How do I print out this string from within my class? [closed]