C++ Lecture 1
C++ Lecture 1
Lecture 1
1. C++ Definition:
C++ is a general-purpose, object-oriented programming language. It was
developed in 1979 by Bjarne Stroustrup at AT & T Bell Laboratory. It is a high-
level programming language & advanced version of C programming language. As
Compared to other programming languages like Java, and Python, it is the fastest
programming language.
2. What is Syntax?
Syntax refers to the rules and regulations for writing statements in a programming
language. They can also be viewed as the grammatical rules defining the structure
of a programming language.The C++ language also has its syntax for the
functionalities it provides. Different statements have different syntax specifying
their usage but C++ programs also have basic syntax rules that are followed
throughout all the programs.
{1}
Lecture 1: C++ Programming Dr. Hayder Kareem
➢ Header File:
The header files contain the definition of the functions and macros we are
using in our program. They are defined on the top of the C++ program. In line
#1, we used the #include <iostream> statement to tell the compiler to include
an iostream header file library which stores the definition of the cin and cout
methods that we have used for input and output. #include is a preprocessor
directive using which we import header files.
Syntax:
#include <Library-name >
➢ Namespace:
A namespace in C++ is used to provide a scope or a region where we define
identifiers. It is used to avoid name conflicts between two identifiers as only
unique names can be used as identifiers. In line #2, we have used the using
namespace std statement for specifying that we will be the standard
namespace where all the standard library functions are defined.
Syntax:
Using namespace std;
➢ Main Function:
In line #3, we defined the main function as int main(). The main function is
the most important part of any C++ program. The program execution always
starts from the main function. All the other functions are called from the main
function. In C++, the main function is required to return some value indicating
the execution status.
{2}
Lecture 1: C++ Programming Dr. Hayder Kareem
Syntax:
int main()
{ ….. code
return 0;
}
Example1: {
3. Token
When the compiler is processing the source code of a C++ program, each group
of characters separated by white space is called a token. Tokens are the smallest
individual units in a program. A C++ program is written using tokens. It has the
following tokens:
➢ Keywords
➢ Identifiers
➢ Constants
➢ Strings
➢ Operators
{3}
Lecture 1: C++ Programming Dr. Hayder Kareem
Keywords:
Keywords (also known as reserved words) have special meanings to the C++
compiler and are always written or typed in short(lower) cases. Keywords are
words that the language uses for a special purpose, such as void, int, public, etc.
It can’t be used for a variable name or function name or any other identifiers. The
total count of reserved keywords is 95. Below is the table for some commonly
used C++ keywords.
C++ Keyword
asm double new switch
auto else operator template
break enum private this
case extern protected throw
catch float public try
char for register typedef
class friend return union
const goto short unsigned
continue if signed virtual
default inline sizeof void
delete int static volatile
do long struct while
{4}
Lecture 1: C++ Programming Dr. Hayder Kareem
Identifier:
Identifiers refer to the name of variables, functions, arrays, classes, etc. created
by the programmer. They are the fundamental requirement of any language.
Rules for naming identifiers:
• Identifier names cannot start with a digit or any special character.
• A keyword cannot be used as an identifier name.
• Only alphabetic characters, digits, and underscores are permitted.
• The upper case and lower-case letters are distinct. i.e., A and a are different in
C++.
• In C++, all the variables must be declared before use.
{7}
Lecture 1: C++ Programming Dr. Hayder Kareem
4. Comments:
Comments can be used to explain C++ code, and to make it more readable. It
can also be used to prevent execution when testing alternative code. Comments
can be singled-lined or multi-lined.
Single Line Comments:
Single-line comments start with two forward slashes (//).
Multi-Line Comments:
Multi-line comments start with /* and ends with */.
Any text between /* and */ will be ignored by the compiler:
1. Primitive Data Types: These data types are built-in or predefined data types
and can be used directly by the user to declare variables. example: int, char, float,
bool, etc. Primitive data types available in C++ are:
• Integer.
• Character
• Boolean
• Floating Point
{8}
Lecture 1: C++ Programming Dr. Hayder Kareem
• Valueless or Void
• Wide Character
2. Derived Data Types: Derived data types that are derived from the primitive or
built-in datatypes are referred to as Derived Data Types. These can be of four types
namely:
• Function
• Array
• Pointer
• Reference
• Class
• Structure
• Union
• Enumeration
{9}
Lecture 1: C++ Programming Dr. Hayder Kareem
6. C++ Variables:
Variables in C++ is a name given to a memory location. It is the basic unit of storage
in a program .
• The value stored in a variable can be changed during program execution.
• A variable is only a name given to a memory location, all the operations done
on the variable effects that memory location.
• In C++, all the variables must be declared before use.
{10}
Lecture 1: C++ Programming Dr. Hayder Kareem
{11}