Question 18
Back to Exercise
What is the output of the following C++ program?
#include <iostream>
#include <string>
#include <vector>
class Course {
std::string title_;
std::vector<std::string> students_;
public:
explicit Course(std::string t) : title_(std::move(t)) {}
const std::string& title() const { return title_; }
void enrol(const std::string& s) { if (!s.empty()) students_.push_back(s); }
std::size_t size() const { return students_.size(); }
};
int main() {
Course c("C++ Basics");
c.enrol("Asha");
c.enrol("");
c.enrol("Ravi");
std::string copy = c.title();
copy += " (copy)";
std::cout << c.title() << " | " << copy << " | " << c.size() << '\n';
return 0;
}
- A C++ Basics | C++ Basics (copy) | 2 Correct
- B C++ Basics (copy) | C++ Basics (copy) | 3
- C C++ Basics | C++ Basics (copy) | 3
- D Compilation error
Correct Answer: Option A
The title is read-only; changing a copy does not affect the course. The empty name is rejected by enrol, leaving 2 students.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!