This is a program that uses classes, together with parameter constructors.
#include <string>
class Ball {
private:
std:: string m_color;
double m_radius;
public:
Ball(const std::string& color, double radius)
{
m_color = color;
m_radius = radius;
}
void print()
{
std::cout << "color: " << m_color << std::endl;
std::cout << "radius: " << m_radius;
}
};
int main()
{
Ball blueTwenty{ "blue", 20.0 };
blueTwenty.print();
return 0;
}
Why do we have to add const
before our first parameter? I see that the double-type parameter doesn't need one. Also, why do we need the first address of the parameter (I guess this is the reason for including &
)? Could anyone explain the logic behind?