I wrote a program like this
#include <iostream> using namespace std;
class Fruit { private:
string m_name;
string m_color; public:
Fruit() = default;
Fruit(const string& name, const string& color)
: m_name{name}, m_color{color}
{
}
const string& getName() const{
return m_name;
}
const string& getColor() const{
return m_color;
} };
class Apple : public Fruit { private:
double m_fiber; public:
Apple() = default;
Apple(const string& name, const string& color, double fiber)
: Fruit{name, color}, m_fiber{fiber}
{
}
const double getFiber() const{
return m_fiber;
}
print(){
cout << "Apple(" << getName() << ", " << getColor() << ", " << getFiber() << ")\n";
} };
int main() {
const Apple a{ "Red delicious", "red", 4.2 };
a.print(); }
This code doesn't compile. However, if I remove the word "const" inside int main() it will work. Why is that?
Please login or Register to submit your answer