Uploaded by Zaid Jaan

C++ Basics

advertisement
C++ Basics
Shahzad Ali Khaskheli
What is C++?
• C++ ( pronounced as cee plus plus ) is a general – purpose
programming language.
• It has imperative, Object oriented and generic programming features,
while also providing facilities for low-level memory manipulation.
When was C++ created?
• C++ was developed by Bjarne stroustrup at Bell Labs since 1979, as an
extension of the C programming language as he wanted an efficient
and flexible language similar to C.
• Which also provided high-level features for program organization.
For what purpose C++ is used?
• C++ is one of the most versatile languages in the world.
• It is used nearly everywhere for everything.
• System programming ( Operating systems, device drivers, database
engines, embedded systems, internet of things etc.
What is C++ program?
• C++ is an object oriented programming (OOP) language, developed by
Bjarne Stroustrup.
• C++ is an extension of C programming language.
• A program is a piece of code that is used to perform a specific task.
First C++ program:
• #include<iostream.h>
• #include<conio.h>
• void main()
•{
• clrscr();
• cout<<“Hello World”;
• getch();
•}
Structure of C++ program:
• The format of writing a program in C++ is called its structure.
• It consists of the following parts:
• Preprocessor directive
• Main function
• Program body
Preprocessor Directive:
• Preprocessor directive is an instruction given to the compiler before
the execution of actual program.
• Preprocessor directive is also known as compiler directive.
• The Preprocessor directive start with “Hash” symbol “#”.
Include preprocessor:
• Include preprocessor directive is used to include header files in the
program.
• Syntax:
• #include<iostream.h>
Header Files:
• Header files contain definition of functions and variables, which are
imported or used into any C++ program by using the preprocessor
#include statement.
• Header file have an extension “.h”, which contains C++ function
declaration and definitions.
Syntax of header files:
• #include<header_file_name>
• Name of header file can also be used in double quoted as follow:
• #include”header_file_name”
Example (Header Files):
• #include<iostream.h>
• The word “iostream” stands for input/output stream.
• This header file contains the definitions of built-in input and output
functions.
main() function:
• The main() function is the starting point of a C++ program.
• Each program must contain main() function.
• If a program does not contain main() function, it can be compiled but
cannot be executed.
Syntax of main() function:
• void main()
•{
• Body of main function
•}
Example (main() function):
• The following example explains the basic structure of C++ program.
• #include<iostream.h>
• void main()
•{
• cout<<“Hello World”;
•}
----- Preprocessor directive
-- main() function
Body of main function
Data type:
• A data type actually describes you data like what form of data you want to
create.
• It may be integer, character, floating point number, string etc.
• In the context of C++, you can make many types of data that are mentioned
above.
Data type (cont…):
• To make integer data type, you type int and then write its name (
called variable name).
• To create floating point data type, you type float and then write its
variable name.
• That is actually the syntax of C++.
• It may differ for other programming languages.
Data type (cont…):
• int – integer, it is of 2 or bytes dependent on machine.
• char – character type ( used to store characters ), consumes one byte of
memory.
• float- used to store floating point numbers, consumes 4 bytes of memory.
• double- used to store high precision floating point numbers, it consumes 8
bytes of memory.
Variables:
• A variable is a named memory location or memory cell.
• It is used to store program’s input data.
• The value of variable may change during the execution of program.
• However, the name of the variable cannot be changed.
How variables created?
• The variables are created in RAM.
• RAM is a temporary memory.
• That’s why the data stored in variables is also temporary.
• The data stored in the variable is automatically removed when program
ends.
Variable declaration:
• The process of specifying the variable name and its type is called variable
declaration.
• Syntax:
• data_type variable_name;
• Example:
• int marks;
• float average;
Variable initialization:
• The process of assigning a value to a variable at the time of declaration in
known as variable initialization.
• The equal “=” sign is used to initialize a variable.
• Variable name is given on the left side and value is given on the right side
of equal sign.
Syntax:
• The syntax of initializing a variable is as follows:
• data_type variable_name=value;
• Example:
• int n=100;
• float z=50.45;
Operators:
• An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations.
• C++ is rich in built-in operators and provides the variety of operators.
• The operators can be categorized as follows:
Unary operator:
• A type of operator that works with one operand in known as unary
operator.
• Example:
• ++ , --
Binary operator:
• A type of operator that works with two operands is known as binary operator.
• Example:
• +,-,*,/,%
• a + b;
• x / y;
Arithmetic operators:
• C++ uses operators to do arithmetic.
• It provides operators for five basic arithmetic calculations: addition ,
subtraction , multiplication , division and taking the modulus.
• Each of these operators uses two values (called operands) to calculate the
final answer.
Arithmetic operators (Example):
• Suppose we have two variables A and B, where A=10 and B=5.
• Arithmetic operation can be used on A and B as follows:
Operations
Result
A+B
15
A–B
5
A*B
50
A/B
2
A%B
0
Relational Operators:
• A relational operators is a programming language construct or operator
that tests or defines some kind of relation between two entities.
• These include numerical equality.
• e.g. 5 = 5
• and inequalities
• e.g 4>3
Assignment operator:
• An operator that assigns a value to a variable is known as assignment operator.
• Syntax:
• Variable = value;
• “=“ is an assignment operator.
• Example:
• a=100;
• c = a + b;
Compound assignment operator:
• An assignment operator that assigns a value to many variables is known as
compound assignment operator.
• Example:
• a = b = 10;
• x = y = z = 50;
Increment operator:
• The increment operator is used to increase the value of a variable by
1.
• It is denoted by the symbol “++”.
• Increment operator can be used as follows:
• Prefix form
• Postfix form
Prefix and Postfix form:
• In prefix form, the increment operator is written before the variable as
follows:
• ++a;
• In postfix form, the increment operator is written after the variable as
follows:
• y++;
Decrement operator:
• The decrement operator is used to decrement the value of a variable
by 1.
• It is denoted by the symbol “- -”
• Decrement operator can also be used in two forms.
• Prefix form: --a
• Postfix form: a--
Download