Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

PCBasedControl Section1

Uploaded by

dev.esraa23
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

PCBasedControl Section1

Uploaded by

dev.esraa23
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Introduction to C++

Programming Language
INTRODUCTION OF PC BASED CONTROL

Eng. Esraa Adel


C++ Environment Setup
 to set up your environment for C++, you need to have the following two
software on your computer:
 Text Editor
This will be used to type your program. Examples of few editors include Windows Notepad,
Visual Studio Code … etc.
The files you create with your editor are called source files and for C++ they typically are
named with the extension .cpp, .cp, or .c.
C++ Environment Setup
C++ Compiler
 This is an actual C++ compiler, which will be used to compile your source code into final executable
program.
 Most frequently used and free available compiler is GNU C/C++ compiler
 Steps to download:
 Download MSYS from https://www.msys2.org/
 pacman –Syu
 pacman –Su
 pacman –Ss gcc
 pacman –S mingw-w64-x86_64-gcc
 pacman –Ss gdb
 pacman –S mingw-w64-x86_64-gdb
C++ Program Structure
The basic structure of a C++ program consists of the following parts:

Header file inclusion section: This is the section where we include all required header files
whose functions we are going to use in the program.
Namespace section: This is the section where we use the namespace.
The main() section: In this section, we write our main code. The main() function is an entry
point of any C++ programming code from where the program's execution starts.
Example
#include <iostream>
using namespace std;
// main() is where program execution begins.
int main()
{
cout << "Hello World"; // prints Hello World
return 0;
}
The C++ language defines several headers, which contain information that is either necessary
or useful to your program. For this program, the header <iostream> is needed.
The line using namespace std; tells the compiler to use the std namespace. Namespaces are a
relatively recent addition to C++.
The next line '// main() is where program execution begins.' is a single-line comment available
in C++. Single-line comments begin with // and stop at the end of the line.
The line int main() is the main function where program execution begins.
The next line cout << "Hello World"; causes the message "Hello World" to be displayed on the
screen.
The next line return 0; terminates main() function and causes it to return the value 0 to the
calling process.
Compile and Execute C++
Program
Open a text editor and add the code as above.
Save the file as: hello.cpp
Open a command prompt and go to the directory where you saved the file.
Type 'g++ hello.cpp' and press enter to compile your code. If there are no errors in your code
the command prompt will take you to the next line and would generate a.out executable file.
Now, type 'a.out' to run your program.
You will be able to see ' Hello World ' printed on the window.
Semicolons and Blocks in C++
In C++, the semicolon is a statement terminator. That is, each individual statement must be ended with a
semicolon. It indicates the end of one logical entity.

For example, following are three different statements


x = y;
y = y + 1;
add(x, y);

A block is a set of logically connected statements that are surrounded by opening and closing braces. For
example
{
cout << "Hello World"; // prints Hello World
return 0;
}
C++ Identifiers
A C++ identifier is a name used to identify a variable, function, class, module, or any other user-
defined item. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by
zero or more letters, underscores, and digits (0 to 9).

C++ does not allow punctuation characters such as @, $, and % within identifiers. C++ is a case-
sensitive programming language.
Here are some examples of acceptable identifiers −

mohd zara abc move _name a_123


Myname50 _temp j a23b9 retVal
C++ Keywords
The following list shows the reserved words in C++. These reserved words may not be used as
constant or variable or any other identifier names.
C++ Comments
Program comments are explanatory statements that you can include in the C++ code. These
comments help anyone reading the source code.
Types of C++ Comments:
C++ Single-line Comments

A single-line comment starts with //, extending to the end of the line. These comments can last
only till the end of the line, and the next line leads to a new comment.
// Text to be commented
C++ Multi-line Comments

/* This is a comment */
/* C++ comments can also
span multiple lines
*/
Comments within Statements
#include <iostream>
using namespace std;
int main()
{
cout << "This line" /*what is this*/ << " contains a comment" << endl;
return 0;
}
C++ "Hello, World!" Program
Printing "Hello, World!" is the first program in C++. Here, this prints "Hello, World" on the console (output screen). To
start learning C++, it is the first step to print sometime on the screen.
// First C++ program
#include<iostream>
using namespace std;
int main()
{
cout << "Hello, World!";
return 0;
}
Output : This program will print "Hello, World!" on the output screen. The output will be −
Hello, World!
Parts of C++ "Hello, World!"
Program
Comment Section (// First C++ program)

Comments are used to specify a textual line that is not supposed to be executed when we
compile the code. The compiler ignores the line, and proceeds to the next line. These are used
for better readability and explanation of code in the comments section.

This is the comment −

// First C++ program


Preprocessor Directive (#include <iostream>)

The #include is known as a pre-processor directive in C++. It is used to include header files
with specific methods and elements. Multiple #include statements are used to apply different
header files in the program. The iostream is the header file that defines functions and
operations related to the input/output stream.

The statement is used in the program is −

#include <iostream>
Namespace (using namespace std;)

Namespaces are used to differentiate code blocks with the same method names. In this
program, the using namespace std; is used to set the namespace as standard for users to apply
all standard methods in programs.

Here is the code statement used in the program −

using namespace std;


The main() Function (int main(){...})

The main() function is the default starting point of any C++ program. It is compulsory for any C+
+ program to have a main function. The program logics are written inside the main program.
The main function body is enclosed inside parenthesis ({}).

The main() function part is −

int main() {
cout << "Hello, World!";
return 0;
}
Printing Statement (cout)

The print/output statement is cout followed by "<<" operator. This is used to print the given
parameters specified in the statement on the screen. We can also print multiple elements in a
single cout block.

The print statement is −

cout << "Hello, World!";


Return Statement (return 0;)

The return statement is also known as the exit statement. It is used to exit from the
corresponding function. The "return 0" is the default statement to exit from the main program.

Here is the return statement used in the program −

return 0;

You might also like