Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

1

Module V
Programing with C++
5

What is C++
C++ is a programming language.
• A standardized, intermediate-level, general-purpose,
object-oriented, and compiled language.
• Created by Bjarne Stroustrup (1983) as an extension to
a C programming language.
• Still, C++ evolved to be a completely different
programming language.
• C++ is accompanied by a set of functions and
containers called the C++ Standard-Library.

73
5

• C++ is governed by the ISO C++ standard. C++ Standards


• There are multiple ISO C++ standards listed here in
chronological order:
• C++03, C++11, C++14, C++17, C++20 and the
upcoming C++23 standard.
• Every C++ standard starting with the C++11 onwards is
referred to as “Modern C++.”

74
5

• C++ programs are usually a collection of C++ code


C++ Compilers
spread across one or multiple source files.
• The C++ compiler compiles these files and turns them
into object files.
• Object files are linked together by a linker to create an
executable file or a library.
• At the time of the writing, some of the more popular
C++ compilers are:
– The g++ frontend (as part of the GCC)
– Visual C++ (as part of the Visual Studio IDE)
– Clang (as part of the LLVM)

75
5

• Code editor (or IDE) with C++ extension C++ Dev tools
• Visual Studio Code for this Course
• Compiler (for generating a binary executable)
• Mingw (gcc),
• Clang llvm (Low level virtual machine )(Clang++)
• Additionally you can use
• Msvc (Microsoft visual studio)

76
5

• Code editor (or IDE) with C++ extension C++ Dev tools
• Visual Studio Code for this Course
• Compiler (for generating a binary executable)
• Mingw (gcc compiler), - Main compiler for this course
• Clang llvm (Low level virtual machine )(Clang++
compiler)
• Additionally you can use
• Msvc (Microsoft visual studio)

77
5

Installing Compilers
• Install the latest version of a compiler always
• Visit https://winlibs.com to download both gcc & Clang
compilers

78
5

• Scroll to downloads -> releases


• Choose one with the “Latest” flag depending on your OS Installing Compilers
• Choose one with both Mingw and LLVM

79
5

• Locate the file you downloaded


• Extract it to obtain a “mingw64’’ folder Installing Compilers
• Copy and paste in it in your windows primary disk
• i.e local disk (C)

80
5

Setting up the system environment variables Installing Compilers


• Browse the “mingw64” folder to “mingw64\bin”
• Copy this path and add it to your path environment
variables
• By pressing your windows start button, and typing
“env” followed by “enter” key

81
5

Installing Compilers

82
5

Installing Compilers

83
5

Installing Compilers

84
5

Installing Compilers

85
5

Installing Compilers

86
5

• Run “cmd” on your windows command line to check if Installing Compilers


compilers are successfully installed
• Run “g++ --version” for gcc
• And “clang++ --version” for clang

87
5

Setting up Vs Code
• Visit https://code.visualstudio.com to download the latest
Visual studio code editor of your OS

88
5

Setting up Vs Code
• Run the downloaded file and open it after the
installation has completed

89
5

• Vs Code by default does not support C++


• We use a C++ extension to enable this feature Setting up Vs Code
• On the left toolbar click the extensions icon
• Type C++ in the search field

90
5

• Install the C++ extension Setting up Vs Code

91
5

• Open Vs Code
• Create a “source.cpp” file and copy this code as a test Connecting Vs Code
code with the compilers

92
5

• Go to Terminal -> Configure tasks


• Vs code should find all the installed compilers Connecting Vs Code
with the compilers

93
5

• Click a compiler of your choice(g++ for this cas)


• A “.vscode” folder with a “tasks.json” will be created Configuring compilers
with configurations that you may or may not need to
edit (label )
• You may need to add “-std=c++20” in the “args” line to
tell the compiler to use C++ 20 (latest version)

94
5

• Read more configuring compilers by browsing to the Configuring compilers


C++ extension and scroll to the “Overview and
tutorials” section and follow the appropriate links

95
5

Configuring compilers
• For example you can read on how to modify the
tasks.json file for gcc and clang compilers

96
5

• Open a new terminal in Vs Code


• Go to terminal again ensuring the file you want to compile is
Testing compilers
selected and click “Run task”
• The process to build your program will begin and should display
a message “Build finished successfully in the terminal,
• Otherwise repeat the process above

97
5

• On the terminal, press “enter” key


• Now a “.exe” file should be created in your files Testing compilers
• Run this on the terminal using “./” followed by the
“filename.exe” (i.e ./program.exe)
• For this case it should return a zero in the terminal
• If that is the case then we are good to go.
• We can now start programming C++ programs
• Apart from Mvsc compiler the procedures for setting up gcc and
clang compilers are the same

98
5

Testing compilers
• Lastly you need to configure one more last thing to make your
programing easy
• Press Ctr + Shift + p and choose “C++: edit configuration”;
• Set the configs to the compiler of your choice and a
“c_cpp_properties.json”.
• Cool! We are all set and we have a template file

99
5

Testing compilers

100
5

• The function main is the main program entry point, the start First C++ Program
of our program.
• When we run our executable, the code inside the main
function body gets executed.
• A function is of type int and returns a result to the system int main() {}
• The reserved name main is a function name.
• It is followed by a list of parameters inside the parentheses ()
followed by a function body marked with braces {}.
• Braces marking the beginning and the end of a function body
• can also be on separate lines:

101
5

• Now we are ready to get the first glimpse at our “Hello


World” program First C++ Program
• The following program is the simplest “Hello World”
example. It prints out “Hello World” in the terminal
int main() {}
#include <iostream>
int main()
{
std::cout << "Hello World.";
}

102
5

Explanation
• The #include <iostream> statement includes the iostream First C++ Program
header into our source file via the #include directive.
• The iostream header is part of the standard library.
• We need its inclusion to use the std::cout object,
• also known as a standard-output stream.
int main() {}
• The << operator inserts our Hello World string literal into
that output stream.
• String literal is enclosed in double quotes “”.
• The ; marks the end of the statement.
• Statements are pieces of the C++program that get executed.
• Statements must end with a semicolon ; in C++.

103
5

• The std is the standard-library namespace and :: is the


scope resolution operator. First C++ Program
• Object cout is inside the std namespace, and to access it, we
need to prepend the call with the std::.
• We will get more familiar with all of these as we go on in the
course, especially the std:: part.
int main() {}
• In a nutshell, the std::cout << is the natural way of
outputting data to the standard output/terminal in C++.
• We can output multiple string literals by separating them
with multiple << operators:

104

You might also like