Read more about that in Chapter 12 of our book. The Simpsons is one of the oldest and most beloved cartoon TV shows since 1987, and, unless you’ve been living in a cave, you probably know it. In the opening credits of each chapter, Bart Simpson, as the troublemaker he is, repeatedly writes a sentence on a chalkboard, in the old “write it 100 times” punishment. The thing is, each episode features a different sentence Bat writes on the chalkboard, such as “I will not fake seizures”, “Organ transplants are best left to professionals”, “The principal’s toupee is not a Frisbee”, and – well.. you get the idea.
source – wikipedia
And why are we telling you all this? well, throughout this book, we talked about templates more than a few times, and by now, you should have a pretty good idea about the general concept of templates, and that we use them to store whole part of a changing set of instructions – it can be a formula, a routine, or a function of any kind, as we can fill the changeable parts later.
we can draw an analogy between The Simpson’s opening sequence and the combination of C++ templates and concepts. Templates and concepts serve as frameworks that allow us to create different instances based on a common structure, just like The Simpsons opening follows a consistent structure that sets the stage for the episodes that follow.
Similar to templates, the core components of The Simpsons opening, such as the catchy theme song, the couch gag, and Lisa’s saxophone solo, remain unchanged, representing the constants or invariant parts. They form the foundational structure, just as templates provide a fixed foundation for creating various instances.
However, within this template, there are dynamic or variable elements that change with each episode. For instance, the chalkboard gag presents a different witty phrase written by Bart in every episode, adding a unique touch and personalization. This variability aligns with the customizable parts of a template, demonstrating the adaptability of concepts that allow different types to satisfy the requirements. The Simpsons opening is renowned for its clever references, cultural satire, and irreverent humor, showcasing the creative flexibility that concepts offer. Just as concepts empower developers to define requirements and ensure various types meet those criteria, the Simpsons opening incorporates humor and social commentary, ensuring its freshness and relevance with each iteration. For these reasons, we have selected the Simpsons opening sequence as an analogy to the combination of C++ templates and concepts. It combines constant elements that establish a recognizable structure with variable elements that provide unique and evolving content. This analogy underscores the significance of templates and concepts in programming, emphasizing their role in creating adaptable and flexible code structures.
if (bartActivities[randomIndex] == “writing on the chalkboard”) { openingElements[0] += ” – ” + GetRandomElement(chalkboardOptions); } }
private: std::vector<std::string> openingElements; std::vector<std::string> bartActivities = { “writing on the chalkboard”, “skateboarding”, “pranking Moe’s Tavern”, “skipping school”, “eating his homework” };
std::vector<std::string> chalkboardOptions = { “\”I will not skateboard in the hallway\””, “\”I will not throw spitballs in class\””, “\”I will not prank call Moe’s Tavern\””, “\”I will not skip school\””, “\”I will not eat my homework\”” };
int main() { // Introduction std::cout << “Welcome to the Simpsons Opening Script Generator!\n”; std::cout << “Press ‘Enter’ to view a Hollywood script style random Simpsons opening script, or ‘q’ to quit.\n”;
bool quit = false;
while (!quit) { // Wait for user input std::string input; std::getline(std::cin, input);
if (input == “q”) { quit = true; } else { // #H SimpsonsOpening opening; opening.addOpeningElement(“Bart”); opening.addOpeningElement(“Lisa playing the saxophone”); opening.addOpeningElement(“Maggie being scanned at the grocery store”);
opening.randomizeBartActivities();
ExecuteOpeningSequence(opening);
std::cout << “\nPress Enter to view another Simpsons opening, or ‘q’ to quit.\n”; } }
std::cout << “Thank you for using the Simpsons Opening Viewer!\n”;
return 0; } #A Concept to check if a type has the member function `open` #B Concept to check if a type has the member function `close` #C Concept to check if a type satisfies the requirements of being an opening sequence #D Function template to execute the opening sequence of a given type #E Example class representing the opening sequence of The Simpsons #F Function template for randomizing Bart’s activities and chalkboard writings #G Utility function to get a random element from a vector #H Generate a random Simpsons opening
Founder of Secured Globe, Inc. Angel investor and Entrepreneur. https://www.securedglobe.com. Author of the book https://manning.com/books/learning-c-plus-plus
Reblogged this on Michael Haephrati – Personal Blog.