I have been working through C++. I am struggling to get my head round the member initialization, particularly one line of code. Consider the following program;
#include <iostream>
#include <cstdint>
class RGBA
{
private:
uint8_t m_red = 0;
uint8_t m_green = 0;
uint8_t m_blue = 0;
uint8_t m_alpha = 255;
public:
***RGBA(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha = 255):
m_red(red), m_green(green), m_blue(blue), m_alpha(alpha)***
{
}
void print()
{
std::cout << "r = " << static_cast<int>(m_red) << " g = " << static_cast<int>(m_green) << " b = " << static_cast<int>(m_blue) << " a = " << static_cast<int>(m_alpha) << '\n';
}
};
I have bolded the line of code I am having problems with ( the first declaration in 'public' My question is, why do I have to declare alpha to be 255 when I dont have to declare red green or blue as 0, whereas I have to declare them all in the private section.
Could someone please explain this to me, and even better explain this concept of member initialization. Thank you, any help is much appreciated.
int main()
{
RGBA teal(0, 127, 127);
teal.print();
return 0;
}