I’m going through the basics of C++ on this site. I am imitating the code on the site and actually compile it and execute it. So far, C++ is kind of too much… Here is the example I’m talking about.
#include <iostream>
#include <string>
class Person {
public:
Person() = default;
Person(int age)
: age(age) {
}
Person(int age, std::string name)
: age(age), name(std::move(name)) {
}
int age = 25;
std::string name = "Unknown";
};
int main() {
Person person;
std::cout << person.name << " is " << person.age << "\n";
Person person2 = Person(40);
std::cout << person2.name << " is " << person2.age << "\n";
Person person3 = Person(33, "Johnny");
std::cout << person3.name << " is " << person3.age << "\n";
}
Here is the code in Python that generates the same result.
class Person:
def __init__(self, age=None, name=None):
self.age = age if age is not None else 25
self.name = name if name is not None else "Unknown"
if __name__ == '__main__':
person = Person()
print(f"{person.name} is {person.age}")
person2 = Person(40)
print(f"{person2.name} is {person2.age}")
person3 = Person(33, "Johnny")
print(f"{person3.name} is {person3.age}")
Now that I see the difference, I wonder if I’m going to continue with C++ exploration. C++ still fascinates me, so I might continue a little more. Hmm I don’t really have a motivation to create an application C++ because of the lack of syntax sugar, so…