Grok all the things

grok (v): to understand (something) intuitively.

C++

๐Ÿ‘ถ ย Children (ELI5)

Ahoy, young adventurers! Are you ready to embark on a marvelous journey through the enchanted land of C++? Imagine a world of wizards and warriors, where spells unfold into powerful actions, and where you have the power to create entire worlds. C++ is a lot like that, only instead of spells, we have code!

Let's dive into the magical world of C++, uncovering its history and learning how to write our very own spells, I mean, codes.

๐Ÿง™ The Origin of C++: A Tale as Old as Time

Once upon a time, in the mystical land of computer science, a wise wizard named Bjarne Stroustrup crafted a new programming language. This happened in the early 1980s, when Bjarne was working on his Ph.D. thesis. He was searching for a language that was powerful yet simple. He wanted something with the performance of C but with better support for high-level abstractions.

So, guess what? He decided to create his own! And so, C++ was born.

"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do shoot yourself in the foot, you blow off your whole leg." - Bjarne Stroustrup

Isn't that something? Thanks to Bjarne's ingenuity, we now have one of the most popular programming languages in the world!

๐Ÿ“š C++ Spells... Err, Syntax & Variables

So, my young spellcasters, what do we need to cast spells in C++? Just like casting a spell requires knowing the right incantations, writing code in C++ requires knowing the correct syntax.

Before we start writing code, let's understand what variables are. They're like the magical ingredients we use in our spells! Variables store data that we can use and modify throughout our code. Now let's start by casting our first spells โ€” I mean, writing our first piece of code!

#include <iostream>

int main() {
  int apples = 5;
  std::cout << "I have " << apples << " apples!" << std::endl;
  return 0;
}

Whoa! What magic did we just create? Let's break it down:

  1. #include <iostream>: This is like opening the book of spells needed for our incantations. In this case, it's the input and output library in C++.
  2. int main(): This is the starting point of our enchanted journey, where we begin casting our spells.
  3. int apples = 5;: Here, we define a variable (our magical ingredient) called apples. We store the number 5 inside apples.
  4. std::cout << "I have " << apples << " apples!" << std::endl;: This is the incantation that displays our message on the screen. It tells us, "I have 5 apples!"
  5. return 0;: And finally, we end our spell, signaling that everything went well.

๐Ÿงช Mixing Magical Potions: Functions & Control Statements

In the world of C++, we can mix various magical potions (also known as functions) to perform specific actions. Let's create a simple function that adds two numbers together. This will be our potion recipe!

int addNumbers(int a, int b) {
  int sum = a + b;
  return sum;
}

And how do we use this potion? Let's go back to our main() function and try it out:

#include <iostream>

int addNumbers(int a, int b); // We declare our function here!

int main() {
  int result = addNumbers(3, 4);
  std::cout << "The result is: " << result << std::endl;
  return 0;
}

Voilร ! The spell has been cast, and the result is displayed: "The result is: 7"

But wait, there's more! Sometimes, we need to make decisions in our magical world of C++. This is where control statements enter the scene. They help us navigate through different paths based on certain conditions. It's like choosing between taking the road to the left or the one to the right!

There are three main types:

  1. if-else: Choose between two paths.
  2. switch-case: Choose between multiple paths.
  3. Loops: Repeat a path as many times as we need.

Let's see an if-else statement in action:

#include <iostream>
#include <string>

int main() {
  std::string favoriteFruit;

  std::cout << "Enter your favorite fruit: ";
  std::cin >> favoriteFruit;

  if (favoriteFruit == "apple") {
    std::cout << "Apples are awesome! ๐Ÿ" << std::endl;
  } else {
    std::cout << "Oh, I like " << favoriteFruit << " too! ๐Ÿ‡" << std::endl;
  }

  return 0;
}

In this magical quest, we ask the user for their favorite fruit and response accordingly!

๐ŸงŠ C++ Constructors: Building Magical Items

In C++, we can also create our very own magical items called objects. Objects are instances of classes, which are like blueprints for creating these items. They can have properties (data) and behaviors (functions). Let's build a simple class called Wizard!

class Wizard {
public:
  std::string name;
  int age;

  Wizard(std::string wizardName, int wizardAge) {
    name = wizardName;
    age = wizardAge;
  }

  void introduce() {
    std::cout << "I am " << name << ", a " << age << " years old wizard!" << std::endl;
  }
};

Now, let's create a wizard named Merlin and make him introduce himself:

#include <iostream>
#include <string>

class Wizard { /*...*/ }; // Our Wizard class goes here!

int main() {
  Wizard merlin("Merlin", 123);
  merlin.introduce();
  return 0;
}

Amazing! When we run this enchanting code, we get: "I am Merlin, a 123 years old wizard!"

๐Ÿ”ฎ Our Magical Journey Comes to an End

Bravo, young explorers! We've just completed a fascinating adventure through the realm of C++. Though our journey ends here, there are many more wonders to discover in this enchanted world. I encourage you to continue practicing your spells and create magical code in C++!

Remember that with great power comes great responsibility, so wield your newfound knowledge wisely and always use it for good. And never forget the wise words of Bjarne Stroustrup:

"There are only two kinds of languages: the ones people complain about and the ones nobody uses."

May your journey into programming be filled with joy and discovery! Farewell, young wizards!

Grok.foo is a collection of articles on a variety of technology and programming articles assembled by James Padolsey. Enjoy! And please share! And if you feel like you can donate here so I can create more free content for you.