#include <string>
#include <vector>
#include <iostream>
class Letter {
public:
Letter(std::string from, std::string to); // Constructor
void add_line(std::string line); // Add a line of text to the body of the letter
std::string get_text(); // Get the entire text of the letter
private:
std::string sender;
std::string recipient;
std::vector<std::string> body;
};
// Implementation of the constructor and methods
Letter::Letter(std::string from, std::string to) : sender(from), recipient(to) {}
void Letter::add_line(std::string line) {
body.push_back(line);
}
std::string Letter::get_text() {
// Concatenate strings correctly
std::string text = "Dear " + recipient + ":\n\n";
for (const auto& line : body) {
text += line + "\n";
}
text += "\nSincerely,\n\n" + sender;
return text;
}
int main() {
Letter letter("Mary", "John");
letter.add_line("I am sorry we must part.");
letter.add_line("I wish you all the best.");
std::cout << letter.get_text() << std::endl;
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: