C++

Contents
Introduction
using namespace
Guess Number
Return Type
Multiple Files
Header Files
Strings
getline()
Errors
How to learn C++
C++ related articles

Introduction

C++ reads like C-plus-plus — a compiled, statically typed general-purpose programming language.

Supports programming paradigms such as procedural programming, object-oriented programming, and generalized programming. The language has a rich standard library that includes common containers and algorithms, I/O, regular expressions, support multithreading and other features.

C++ combines the properties of both high-level and low-level languages. In comparison with its predecessor, the C language, the greatest attention is paid to support object-oriented and generalized programming.

C++ is widely used for software development, being one of the most popular programming languages (for more information about the popularity of C++ see - here ). The scope of its application includes the creation of operating systems, a variety of application programs, device drivers, applications for embedded systems, high-performance servers, as well as games.

There are many implementations of the C language++, as free, both commercial and for various platforms. For example, on the x86 platform, this is GCC, Visual C++, Intel C++ Compiler, Embarcadero (Borland) C++ Builder and others. C++ has had a huge impact on others programming languages, primarily Java and C#.

The syntax of C++ is inherited from the C language. One of the principles of development was to maintain compatibility with C. However, C++ is not in the strict sense a superset of C; a set of programs that can be translated equally successfully by both C compilers and C+&plus compilers;, quite large, but does not include all possible C programs.

Website dedicated to the standardization of the language isocpp.org

You can check which functions a particular compiler supports on the website cppreference.com

Compiling

  1. The programmer writes the code and saves it as .cpp files.

    Usually you need to add the connection of libraries or other files to the .cpp file - this is done using #include


  2. First, go through these files through the preprocessor and includes everything you need to include in them.

  3. After the preprocessor, the compiler comes into play.

    The compiler processes these .cpp files and makes them .o files or .obj files.


  4. The linker connects these .o files in .exe file.

Depending on what environment you will be working in, these processes may there will be your own details that you will need to know. Perhaps it is the study of these nuances that will cause the greatest problems. You need to be mentally prepared for this.

For example, gcc it may work strangely if you run the compiler with the gcc command and fine if you run g++

By default, a file is created after compilation a.exe which needs to be run separately. You can configure the behavior of the compiler, for example, using makefile

Visual Studio by default, compiles all .cpp files included in the project.

using namespace std;

A beginner learning C++ may have a question: why there is a bunch of code written before main().

For example, why write using namespace std;

You can not write, but then every time you use cout or, for example, endl will need to be directly specified from which namespace You take them

It will look something like this:

#include <iostream> int main() { std::cout << "Visit HeiHei.ru!" << std::endl << "or TopBicycle.ru" << std::endl << 2.5*4; return 0; }

The :: operator has a special name - the Scope Extension Operator or Scope Resolution Operator

<< - Insertion Operator

If you use several libraries at once, then perhaps it's even better - there is no risk get a name conflict.

But for a beginner to learn C++ it's probably too cumbersome.

using namespace std; allows you to record the same thing without constant writing std::

#include <iostream> using namespace std; int main() { cout << "Visit HeiHei.ru!" << endl << "Or TopBicycle.ru" << endl << 2.5*4; return 0; }

Guess the number

Exercise: write a game in which the user needs to guess a predetermined number.

It is necessary to give hints whether the entered number is greater or less than the hidden one.

Decision:

#include <iostream> using namespace std; int main() { int i; bool keepgoing = true; int answer = 7; while (keepgoing) { cout << "Guess Number "; cin >> i; if (i < answer) { cout << "Your number, " << i << ", is smaller. " << endl; cout << "Try again" << endl; } if (i == answer) { cout << "Correct, congratulations" << endl; keepgoing = false; } if (answer < i) { cout << "Your number, " << i << ", is greater. Try again" << endl; } if (i != answer) { cout << "Retry? 0 for no "; cin >> again; if (again == 0) { keepgoing = false; } } } }

Example of a function that adds two numbers and returns no warnings about errors in types int - double

#include <iostream> using namespace std; double add(double x, double y) { return x + y; } int main() { auto a = add(3, 4); cout << "3 + 4 is " << a; // 7 cout << endl; double b = add(1.3, 4.5); cout << "1.3 + 4.5 это " << b; // 5.8 return 0; }

Function overload

Two different functions can have the same name. This can be done if they have a different number of arguments. Such overloading is considered good practice.

You can study an example of overloading the add() function in the article

«Перегрузка функций C++»

Return Type

As you have already noticed, most functions end with return 0;

If you are writing a function that should not return anything, you need to specify before the function name void

#include <iostream> using namespace std; void test() { return; }

Multiple Files

The file can be divided into several parts. In the one where you call main(), specify the names functions that you have moved to another file and the arguments that they accept.

Main file:

#include <iostream> using namespace std; double add(double x, double y); double add(double a, double b, double c); bool test(bool x); bool test(double x); void test(int y); int main() {

etc

File with functions:

bool test(bool x) { return x; } bool test(double x) { return x > 0; } double add(double x, double y) { return x + y; } double add(double a, double b, double c) { return a + b + c; }

Header Files

Then don't list all the functions that You want to use them in the main file, you can mention them in a special Header file with the extension.h

Then this file will need to be connected to the main one using the #include directive

Main file:

#include <iostream> #include "Functions.h"

File Functions.h

double add(double x, double y); double add(double a, double b, double c); bool test(bool x); bool test(double x); void test(int y);

File Functions.cpp remains unchanged.

Strings

#include <iostream> #include <string> using namespace std; int main() { string name; cout << "who are you?" << endl; cin >> name; string greeting = "Hello, " + name; if (name == "Andrei") { greeting += ", I know you!"; } cout << greeting << endl; int l = greeting.length(); cout << "\"" + greeting + "\" is " << l << " characters long. " << endl; auto space = greeting.find(' ' string beginning = greeting.substr(space + 1); cout << beginning << endl; if (beginning == name) { cout << "expected result. " << endl; } return 0; }

The task is to compare two entered words and determine which one is longer.

using namespace std; int main() { string word1; string word2; cout << "Enter first word" << endl; cin >> word1; cout << "Enter 2nd word" << endl; cin >> word2; if (word1.length() == word2.length()) { cout << "words have the same length"; } if (word1.length() > word2.length()) { cout << "word1 is longer"; } if (word1.length() < word2.length()) { cout << "word2 is longer"; } return 0; }

This code will only work if the word is entered without spaces.

To compare the lengths of strings, use getline()

#include <iostream> #include <string> using namespace std; int main() { string line1; string line2; cout << "Enter first word" << endl; getline(cin , line1); cout << "Enter 2nd word" << endl; getline(cin , line2); if (line1.length() == line2.length()) { cout << "words have the same length"; } if (line1.length() > line2.length()) { cout << "line1 is longer"; } if (line1.length() < line2.length()) { cout << "line2 is longer"; } return 0; }

How to learn C++

If you want it for free, then I recommend the site < ravesli.com .

They translated the official C++ training website into Russian and, it seems to me, the quality has not suffered.

You can find good videos on Russian-language YouTube

If you have the opportunity to buy a paid course, start with something not too expensive.

For those who know English, it may be suitable:

Eduonix . They offer a course for beginners and several similar ones.

On Pluralsight there is a long course on C++ but I didn't really understand who it was designed for. For beginners, there's too much missing. For the experienced there is too much introductory information. It is suitable only as an addition to some other course.

In Russian at OTUS there is a course for beginners and for advanced

Russian educational giants like Geekbrains Netology and Skillbox as far as I know, they do not offer courses on C++ perhaps this is due to a high entry threshold, low hype and the specifics of vacancies.

UPD: now courses on C++ and even C they also appeared on large platforms.

Related Articles
Development на C++
Перегрузка функций
Разбиение кода на части
Вектор. Часть 0
Вектор. Часть 1
Вектор. Часть 2
Указатели
Классы
SFML
Тетрис на C++ с библиотекой SFML2
SDL
Массив Структур
Как узнать тип переменной C++
Решение задач на C++
Как создать пустую строку в C++
Errors C++
Make