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

PCBasedControl Section2

Uploaded by

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

PCBasedControl Section2

Uploaded by

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

Introduction to C++

Programming Language
INTRODUCTION OF PC BASED CONTROL

Eng. Esraa Adel


C++ Data Types
While writing program in any language, you need to use various variables to store various
information. Variables are nothing but reserved memory locations to store values. This means
that when you create a variable you reserve some space in memory.

You may like to store information of various data types like character, wide character, integer,
floating point, double floating point, boolean etc. Based on the data type of a variable, the
operating system allocates memory and decides what can be stored in the reserved memory.
Primitive Built-in Types
C++ offers the programmer a rich assortment of built-in as well as user defined data types.
Following table lists down seven basic C++ data types −
The following table shows the
variable type, how much memory it
takes to store the value in memory,
and what is maximum and
minimum value which can be
stored in such type of variables.
Following is the example, which will produce correct size of various data types on your computer.
This example uses endl, which inserts a new-line character after every line and << operator is being
used to pass multiple values out to the screen. We are also using sizeof() operator to get size of
various data types.
When the above code is compiled and executed, it produces the following result which can vary from
machine to machine
Derived Data Types
Data types which are obtained from pre-defined data types in C++ are known as Derived Data
Types. These can be classified into four categories, namely
1. Function
A function is the simplest form of user-defined data type. It includes a return type, a function
name and input parameters.
Syntax
Example
2. Array
An array is a series of elements of same data type. Elements of an array are stored in contiguous
memory locations in the storage.
Syntax
Example
3. Pointer
A pointer is a reference to an element defined previously. The value of the pointer returns the
address location of the element which is associated with it
Syntax
Example
User-Defined Data Types
Data types which are defined by the user intuitively without using any pre-defined data types
are known as User-Defined Data Types.
1. Class
A class is a defined in Object Oriented Programming as a custom data type which is used to construct
an object. It is the framework of an object, and it can include constructors, methods and OOP
concepts like Polymorphism, Inheritance, etc.
2. Structure (struct)
In structure data type, the user can introduce multiple primitive data types inside the struct
body.
C++ Numeric Data Types
int Data Type
The int data type is short for integer, which takes numeric values from -231 to (231-1). It takes 4
Bytes (i.e., 32 bits) in the memory.
Syntax

float Data Type


The float data type is used for floating-point elements, which are numbers that are followed by a
decimal part. This data type takes 4 Bytes (i.e., 32 bits) of memory.
Syntax
double Data Type
The double data type is used to store floating-point elements with double precision as
compared to float. This data type takes 8 Bytes (i.e., 64 bits) of memory

Syntax
C++ Character (char) Data Type
The character (char) data type in C++ stands for alphanumeric values, which can be a wide range
of characters. These may include alphabets like 'a', 'b', and 'c', numeric values like '1', '2', and '3',
symbols like '#', '$', and '&', and many more.

The character data type takes 1 Byte (i.e., 8 bits) of memory space to store characters. In C++,
the keyword "char" is used to declare a character variable.
Syntax
C++ Boolean (bool) Data Type
The bool data type in C++ stands for Boolean values, which are True and False. In C++, 1 stands
for True whereas 0 stands for False. The keyword "bool" is used to declare a Boolean data type.
The addition of bool data type is a one of the newer features of C++ language.
Values of Boolean (bool) Data Type
▪ True or 1
▪ False or 0

Syntax
C++ Variables and Types
C++ Variable

A variable provides us with named storage that our programs can manipulate. Each variable in C++ has a specific
type, which determines the size and layout of the variable's memory; the range of values that can be stored
within that memory; and the set of operations that can be applied to the variable.

The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either
a letter or an underscore. Upper and lowercase letters are distinct because C++ is case-sensitive.

Some other rules for variable naming conventions in C++


▪ Keywords cannot be used as variable names.
▪ The variable name cannot contain spaces.
▪ Hyphen (-) cannot be used within the variable names.
▪ Variable names must not start with special characters and numbers. It should be either an uppercase or lowercase character
or an underscore (_).
Example of Valid Variable Names

Example of Invalid Variable Names


Variable Scope in C++
Local Variables
Variables that are declared inside a function or block are local variables. They can be used only
by statements that are inside that function or block of code. Local variables are not known to
functions outside their own.
Global Variables
Global variables are defined outside of all the functions, usually on top of the program. The
global variables will hold their value throughout the life-time of your program.

A global variable can be accessed by any function. That is, a global variable is available for use
throughout your entire program after its declaration.
Local and Global Variables with Same
Names
Accessing Global Variable
You can access a global variable when there is a local variable with the same name by using the
SRO (Scope Resolution Operator) :: before the name of that variable.
C++ Declare Multiple Variables
Initialize Multiple Variables
Operators in C++
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations.
C++ is rich in built-in operators and provide the following types of operators

▪ Arithmetic Operators
▪ Relational Operators
▪ Logical Operators
▪ Assignment Operators
Arithmetic Operators
Arithmetic operators in C++ are the basic operators, which are used for basic mathematical or
arithmetical operations on operands. These operators are essential for performing calculations
and manipulating data within a program.
Relational Operators
Relational operators are used to compare two values or expressions. These operators return a
boolean value − true if the comparison is correct, and else false.

They are essential for making decisions and controlling the flow of a program based on
conditions.
Logical Operators
Logical operators are used to perform logical operations on boolean values (true or false). These
operators are essential for controlling the flow of a program based on conditions. There are
three primary logical operators in C++ as mentioned below
Assignment Operators
Assignment operators are used to assign values to variables. These operators allow you to set or
update the value stored in a variable.

You might also like