Skip to content

Instantly share code, notes, and snippets.

@kenpower
Created January 25, 2024 15:12
Show Gist options
  • Save kenpower/00296654f99b7b98975981c67546a2d2 to your computer and use it in GitHub Desktop.
Save kenpower/00296654f99b7b98975981c67546a2d2 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <memory>
#include <string>
// Abstract base class (interface)
class Chatbot {
public:
virtual void hello() = 0;
virtual void goodbye() = 0;
};
// English implementation
class EnglishChatbot : public Chatbot {
public:
void hello() override {
std::cout << "Hello!" << std::endl;
}
void goodbye() override {
std::cout << "Goodbye!" << std::endl;
}
};
// French implementation
class FrenchChatbot : public Chatbot {
public:
void hello() override {
std::cout << "Bonjour!" << std::endl;
}
void goodbye() override {
std::cout << "Au revoir!" << std::endl;
}
};
// Spanish implementation
class SpanishChatbot : public Chatbot {
public:
void hello() override {
std::cout << "¡Hola!" << std::endl;
}
void goodbye() override {
std::cout << "¡Adiós!" << std::endl;
}
};
int main() {
std::unique_ptr<Chatbot> chatbot;
std::string language;
std::cout << "Which language do you want? (English/French/Spanish): ";
std::cin >> language;
if (language == "English") {
chatbot = std::make_unique<EnglishChatbot>();
}
else if (language == "French") {
chatbot = std::make_unique<FrenchChatbot>();
}
else if (language == "Spanish") {
chatbot = std::make_unique<SpanishChatbot>();
}
else {
std::cout << "Unknown language. Using English as default." << std::endl;
chatbot = std::make_unique<EnglishChatbot>();
}
chatbot->hello();
chatbot->goodbye();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment