Introduction to C++

C++ Grammar and Features

C++ is a general-purpose programming language that was developed in the early 1980s by Bjarne Stroustrup. C++ is a powerful language that is used to create a wide variety of software, including operating systems, compilers, and applications.

C++ is a statically typed language, which means that the types of variables and expressions must be known at compile time. This helps to prevent errors and makes the code more readable and maintainable.

C++ has a number of advanced features that make it a powerful language. These features include:

  • Templates: Templates are a powerful feature that allows you to create generic code that can be used with different types of data.
  • C++11 new features: C++11 introduced a number of new features, including lambda expressions, move semantics, and rvalue references.
  • STL: The STL is a library of C++ classes and functions that provide a wide range of functionality, such as containers, iterators, and algorithms.
  • Lambda expressions: Lambda expressions are a new feature in C++11 that allows you to create anonymous functions.
  • Move semantics: Move semantics are a new feature in C++11 that allows you to move objects instead of copying them. This can improve performance in some cases.
  • Rvalue references: Rvalue references are a new feature in C++11 that allows you to take the address of an object that is not an lvalue. This can be useful for passing objects by value to functions.

Examples

Here are some examples of C++ code that demonstrates the use of the above features:

// This is a template function that takes any type of data as input and returns its sum.
template <typename T>
T sum(T a, T b) {
  return a + b;
}

// This is a C++11 lambda expression that prints the name and age of a person.
auto print_person = [](const struct person *person) {
  printf("Name: %s\n", person->name);
  printf("Age: %d\n", person->age);
};

// This is a C++11 move constructor that moves the contents of one `person` object to another.
person::person(person &&other) {
  name = std::move(other.name);
  age = std::move(other.age);
}

// This is a C++11 rvalue reference to a `person` object.
person &&rperson = person();

// This is a C++11 `std::vector` container that stores `person` objects.
std::vector<person> people;

// This is a C++11 `std::algorithm` that sorts the `people` container by name.
std::sort(people.begin(), people.end(), [](const person &a, const person &b) {
  return a.name < b.name;
});

// This is the main function of the program.
int main() {
  // Create a person and initialize its name and age.
  struct person person = {"John Doe", 30};

  // Print the person's name and age.
  print_person(&person);

  // Calculate the sum of 10 and 20.
  int sum = sum<int>(10, 20);

  // Print the sum.
  printf("The sum of 10 and 20 is %d.\n", sum);

  // Create a vector of people.
  people.push_back(person);

  // Sort the people by name.
  std::sort(people.begin(), people.end());

  // Print the names of the people in the vector.
  for (auto person : people) {
    printf("Name: %s\n", person.name);
  }

  // Return 0 to indicate success.
  return 0;
}

This is just a small example of the many things that can be done with C++. For more information, please consult a C++ programming tutorial or reference manual.