C++ Basics
C++ Basics
Installation Process
Example Using an IDE
Most IDEs like Visual Studio, Code::Blocks, or CLion have a Build and
Run or Run button that automates the entire process:
1.Clicking "Build" compiles the code and checks for errors.
2.Clicking "Run" runs the last successful build.
3.Clicking "Build and Run" compiles and runs in one step, launching the
program if there are no build errors.
•General Structure of C++ Program.
• Different programming languages have their own format of
coding.
• The basic components of a C++program are:
• Comments or Documentation Section
• Pre-processor Directives (Linker Section):
• Definition
• Global Declaration
• main ( ) function
• Declarations
• Statements
Output Operator:
The statement cout <<”Hello, world” displayed the string with in quotes on the screen.
The identifier cout can be used to display individual characters, strings and even
numbers. It is a predefined object that corresponds to the standard output stream.
Stream just refers to a flow of data and the standard Output stream normally flows to
the screen display. The cout object, whose properties are defined in iostream.h
represents that stream. The insertion operator << also called the ‘put to’ operator
directs the information on its right to the object on its left.
Return Statement:
In C++ main ( ) returns an integer type value to the operating system. Therefore every main ( ) in C++ should
end with a return (0) statement, otherwise a warning or an error might occur.
Input Operator:
The statement cin>> number 1; is an input statement and
causes The program to wait for the user to type in a number.
The number keyed in is placed in the variable number1. The
identifier cin is a predefined object in C++ that corresponds to
the standard input stream. Here this stream represents the key
board.
The operator >> is known as get from operator. It extracts
value from the keyboard and assigns it to the variable on its
right.
comments
Parts of C++
What will be the output
•C++ Character Set:
•Character Set means the valid set of characters that a language can
recognizes.
The character set of C++ includes the following:
C++ Tokens:
The smallest individual unit is known as token.
These elements help us to construct statements, definitions,
declarations, and so on,which in turn helps us to construct
complete program.
Tokens used in C++ are:
Identifier
Reserved Keywords
Constants or Literals
Punctuators
Operators
•C++ Identifier:
•identifiers are the unique names assigned to variables, functions, classes, structs,
or other entities within the program
It contains letters, digits and underscore.
C++ does not allow punctuation characters such as @, $, and % within
•In C++, literals are fixed values written directly in the code. They represent
constant values of various data types, such as integers, floating-point
numbers, characters, strings, and Boolean values
Binary Operators
Example: x = x – 10;
Ternary Operators
Example: x = (a>b) ? a:b;
•Special Operator :
An expression is a combination of opcode and operand.
• Example:
multi-lined.: Multi-line comments start with /* and ends
with */. Example:
Linker Section in C++
•The linker is a program that makes executable files.
The linker resolves linkage issues, such as the use of symbol hash (#) defined in
written in the
• C++ libraries such as iostream, cmath etc
•Library Functions in C++
•Built-in functions for String manipulation are:
Main function section in C++
•The starting point of all C++ programs is the main function.
This function is called by the operating system when your program is executed
by the computer.
int main( )
{
cout << "Hello World" << endl; //Function body section
return 0;
}
{ signifies the start of a block of code, and } signifies the end.
Data Types in C++:
•Data Types can be defined as the set of values, which can be stored in a variable
along
•with the operations that can be performed on those values.
C++ defines several types of data and each type has unique characteristics.
are
• character constants.
When a variable of type char is declared, the compiler converts the character to its
equivalent ASCII code.
A character is allocated only 1 byte (8 bits) of memory space.
The general form of a character declaration is: char variable_list;
Example: char alpha=’a’;
Basic Data Types:float type
•The float type: This represents the number with fractional part i.e. real numbers.
The float type is used to store real numbers.
Number such as 1.8, 4.5, 12e-5 and -9.66 are all floating-point numbers.
Example:
float a=5.5;
Basic Data Types:double type
•The double type: The double and float are very similar.
The float type allows you to store single precision floating point numbers,
while the
• double keyword allows you to store double precision floating point numbers.
Its size is typically 8 bytes of memory space. The range of numbers we
float a=5.5;
Basic Data Types:bool type
•The bool type: The bool type has logical value true or false.
The identifier true has the value 1, and the identifier false has the value 0.
Example:
bool legal_age=true;
The statement legal_age= (age>=21);
assigns the value true if age is greater than or equal to 21 or else it
returns the value false
Basic Data Types:void type
•The void type : The void data type has no values and no operations.
In other words, both the set of values and set of operations are empty.
In this declaration the main function does not return any value.
Example:
void main( )
{
Statements;
}
•Basic Data Types:
The data type specifies the size and type of information the variable
will store.
Derived data types:
•These data types are constructed using simple or fundamental data types.
Derived data types emphasize on structuring of a group of homogeneous (same
type)
• data items.
These data types includes:
Arrays
Functions
Pointers
References
User defined data types:
These data types are constructed by user using simple or fundamental data types.
User defined data types emphasize on structuring of a group of homogeneous
(same
• type) or heterogeneous (different type) data items.
Some user defined data types include
Enumerated
Structure
Union
Class