I'm trying to overload operator<<
in several subclasses.
I have a superclass called Question, which has an enumerated value type, and a string question.
The subclasses of this class are TextQuestion, ChoiceQuestion, BoolQuestion and ScaleQuestion. TextQuestion has no additional data fields. ChoiceQuestion has a vector of strings, to store the multiple choice possibilities. BoolQuestion has no additional data fields. ScaleQuestion has two int-values, low_ and high_, for the scale.
class Question {
public:
enum Type{TEXT, CHOICE, BOOL, SCALE};
Question():
type_(), question_() {}
Question(Type type, std::string question):
type_(type), question_(question) {}
friend std::ostream& operator<<(std::ostream& out, const Question& q);
virtual void print(std::ostream& out) const;
virtual ~Question();
private:
Type type_;
std::string question_;
};
class TextQuestion: public Question {
public:
TextQuestion():
Question() {}
TextQuestion(Type type, std::string question):
Question(type, question) {}
void print(std::ostream& out) const;
virtual ~TextQuestion();
};
class ChoiceQuestion: public Question {
public:
ChoiceQuestion():
Question(), choices_() {}
ChoiceQuestion(Type type, std::string question, std::vector<std::string> choices):
Question(type, question), choices_(choices) {}
void print(std::ostream& out) const;
virtual ~ChoiceQuestion();
private:
std::vector<std::string> choices_;
};
class BoolQuestion: public Question {
public:
BoolQuestion():
Question() {}
BoolQuestion(Type type, std::string question):
Question(type, question) {}
void print(std::ostream& out) const;
virtual ~BoolQuestion();
};
class ScaleQuestion: public Question {
public:
ScaleQuestion():
Question(), low_(), high_() {}
ScaleQuestion(Type type, std::string question, int low = 0, int high = 0):
Question(type, question), low_(low), high_(high) {}
void print(std::ostream& out) const;
virtual ~ScaleQuestion();
private:
int low_, high_;
};
Now, I'm trying to overload operator<< for all these subclasses and I tried using this example
So I made a virtual print function in the superclass, overloaded the print function in each subclass and the operator<<
in the superclass calls the print-function.
std::ostream& operator<<(std::ostream& out, const Question& q) {
q.print(out);
return out;
}
void Question::print(std::ostream& out) const {
std::string type;
switch(type_) {
case Question::TEXT:
type = "TEXT";
break;
case Question::CHOICE:
type = "CHOICE";
break;
case Question::BOOL:
type = "BOOL";
break;
case Question::SCALE:
type = "SCALE";
break;
}
out << type << " " << question_;
}
void TextQuestion::print(std::ostream& out) const {
Question::print(out);
}
void ChoiceQuestion::print(std::ostream& out) const {
Question::print(out);
out << std::endl;
int size(get_choices_size());
for (int i = 0; i < size; ++i) {
out << choices_[i] << std::endl;
}
}
void BoolQuestion::print(std::ostream& out) const {
Question::print(out);
}
void ScaleQuestion::print(std::ostream& out) const {
Question::print(out);
out << " " << low_ << " " << high_;
}
I did it exactly like in the example, but when I output my Questions, it always uses the baseclass and outputs only the type and the question. The compiler never uses the subclasses.