Classes and OOP
โ Report an issue with this lessonclass Course {
public:
Course(std::string title, double price) : title_(title), price_(price) {}
std::string describe() const {
return title_ + " -- $" + std::to_string(price_);
}
private:
std::string title_;
double price_;
};
Course c("C++ Mastery", 149.99);
std::cout << c.describe();
The constructor initializer list (: title_(title), ...)
initializes members directly, which is more efficient than assigning
inside the constructor body.
Try it yourself
Exercise: Add a describe() method to Course that returns title + " -- $" + price, then call it.
Expected output:
C++ Mastery -- $149.990000
Run your code and get it working before marking this lesson complete.
// that was the last free lesson
9 more lessons โ including Capstone project: a course catalog manager โ plus a certificate are waiting.
Unlock the full course โ $149.99