0% found this document useful (0 votes)
34 views

C++ Lecture 1

C++ is an object-oriented programming language developed in 1979 by Bjarne Stroustrup. It is a faster language compared to Java and Python. The basic syntax of a C++ program includes header files, namespaces, and the main function. The compiler processes source code by separating it into tokens such as keywords, identifiers, constants, strings, and operators. Keywords are reserved words with special meanings, while identifiers name variables, functions, and other user-defined items.

Uploaded by

Mahdi Last
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

C++ Lecture 1

C++ is an object-oriented programming language developed in 1979 by Bjarne Stroustrup. It is a faster language compared to Java and Python. The basic syntax of a C++ program includes header files, namespaces, and the main function. The compiler processes source code by separating it into tokens such as keywords, identifiers, constants, strings, and operators. Keywords are reserved words with special meanings, while identifiers name variables, functions, and other user-defined items.

Uploaded by

Mahdi Last
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

C++ PROGRAMMING

Lecture 1

Dr. Hayder Kareem


2023-2024
Lecture 1: C++ Programming Dr. Hayder Kareem

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.

Basic Syntax of a C++ Program:

{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

❖ asm: To declare that a block of code is to be passed to the assembler.


❖ auto: A storage class specifier that is used to define objects in a block.
❖ break: Terminates a switch statement or a loop.
❖ case: Used specifically within a switch statement to specify a match for the
statement’s expression.
❖ catch: Specifies actions taken when an exception occurs.
❖ char: Fundamental data type that defines character objects.
❖ class: To declare a user-defined type that encapsulates data members and
operations or member functions.
❖ const: To define objects whose value will not alter throughout the lifetime of
program execution.
❖ continue:- Transfers control to the start of a loop.
❖ default:- Handles expression values in a switch statement that are not handled
by case.
❖ delete: Memory deallocation operator.
❖ do: indicate the start of a do-while statement in which the sub-statement is
executed repeatedly until the value of the expression is logical-false.
❖ double: Fundamental data type used to define a floating-point number.
❖ else: Used specifically in an if-else statement.
❖ enum: To declare a user-defined enumeration data type.
❖ extern: An identifier specified as an extern has an external linkage to the
block.
❖ float:- Fundamental data type used to define a floating-point number.
❖ for: Indicates the start of a statement to achieve repetitive control.
❖ friend: A class or operation whose implementation can access the private data
members of a class.
❖ goto: Transfer control to a specified label.
{5}
Lecture 1: C++ Programming Dr. Hayder Kareem

❖ if: Indicate the start of an if statement to achieve selective control.


❖ inline: A function specifier that indicates to the compiler that inline
substitution of the function body is to be preferred to the usual function call
implementation.
❖ int: Fundamental data type used to define integer objects.
❖ long: A data type modifier that defines a 32-bit int or an extended double.
❖ new: Memory allocation operator.
❖ operator: Overloads a c++ operator with a new declaration.
❖ private: Declares class members which are not visible outside the class.
❖ protected: Declares class members which are private except to derived
classes
❖ public: Declares class members who are visible outside the class.
❖ register: A storage class specifier that is an auto specifier, but which also
indicates to the compiler that an object will be frequently used and should
therefore be kept in a register.
❖ return: Returns an object to a function’s caller.
❖ short: A data type modifier that defines a 16-bit int number.
❖ signed: A data type modifier that indicates an object’s sign is to be stored in
the high-order bit.
❖ sizeof: Returns the size of an object in bytes.
❖ static: The lifetime of an object-defined static exists throughout the lifetime
of program execution.
❖ struct: To declare new types that encapsulate both data and member
functions.
❖ switch: This keyword is used in the “Switch statement”.
❖ template: parameterized or generic type.
❖ this: A class pointer points to an object or instance of the class.
{6}
Lecture 1: C++ Programming Dr. Hayder Kareem

❖ throw: Generate an exception.


❖ try: Indicates the start of a block of exception handlers.
❖ typedef: Synonym for another integral or user-defined type.
❖ union: Similar to a structure, struct, in that it can hold different types of data,
but a union can hold only one of its members at a given time.
❖ unsigned: A data type modifier that indicates the high-order bit is to be used
for an object.
❖ virtual: A function specifier that declares a member function of a class that
will be redefined by a derived class.
❖ void: Absent of a type or function parameter list.
❖ volatile: Define an object which may vary in value in a way that is
undetectable to the compiler.
❖ while: Start of a while statement and end of a do-while statement.

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:

5. C++ Data Types:

Data Types in C++ are Mainly Divided into 3 Types:

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

• Double 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

3. Abstract or User-Defined Data Types: Abstract or User-Defined data types are


defined by the user itself. Like, defining a class in C++ or a structure. C++
provides the following user-defined datatypes:

• Class

• Structure

• Union

• Enumeration

• Typedef defined Datatype

{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

Rules For Declaring Variable

• The name of the variable contains letters, digits, and underscores.


• The name of the variable is case sensitive (ex Arr and arr both are different
variables).
• The name of the variable does not contain any whitespace and special
characters (ex #,$,%,*, etc).
• All the variable names must begin with a letter of the alphabet or an
underscore (_).
• We cannot used C++ keyword (ex:float, double, class) as a variable name.

{11}

You might also like