Introduction To C++

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 42

INTRODUCTION TO C++

• Bjarne Stroustrup at Bell labs initially developed C++

• It was designed to support the features of C.

• Added to this were features such as classes, inheritance, operator


overloading etc.

• C++ is described as a superset of C


• C++ program – source code

• The file in which you this program is known as source file

• To write any C++ program, we need a text editor, which allows you to create and edit text
files.

• Ater compilation (made error free) , it converts our source code into machine understandable
code(object code).

• C++ programs are saved using file extension .cpp


A SIMPLE C++ PROGRAM
#include<iostream.h>
void main()
{
Cout<<“hello, how are you”;
}
• Here we are going to use a pre-processor directive called #include, this one allow us to include the header
files of the program.. We can use cin, cout, going to help to do some io task. It is needed to support all the
in built io functions.
• Iostream is the name of the file in standard C++ library.
• Main function- for performing some particular task. Every C++ program must include one and only one
main function.
• {}- Contains actual program body, which we can start writing the codefor our program. Whatever the
things which is in the curly brackets consider as the function body
• << - represents C++ output operator. When this statement gets executed, the characters enclosed in quotes
are sent to standard output devide(screen)
DATA TYPES IN C++
• What type of data we are storing
• So the type of data we are storing is called data type.
DATA TYPES
Built in Derived
Integral User defined
floating void array function pointer
VARIABLES
• Variables can be declared anywhere before they are used
• Name given to a memory location.
• Syntax- data type var1, var2;
• Variable name cannot start with a digit
• variables have scope- global and local scope
• Global – If declared outside functions. Can be referenced from anywhere in the
program
• Local – If declared inside a function. Can be referenced only from within that function
#include<iostream.h>
void main()
{
int a=5;
Cout<<“value of a is”<<a;
}
#include<iostream.h>
void main()
{
int a;
cout<<“enter a number \n”;
cin>>a;
cout<<“value of a is”<<a;
}
#include<iostream.h>
void main()
{
int a,b;
cout<<“enter Ist number \n”;
cin>>a;
cout<<“enter II nd number \n”;
cin>>b;
cout<<“value of a is”<<a;
cout<<“value of b is”<<b;
}
CONSTANTS
• Values can’t be changed during execution
• Declared with the “const” keyword
• Syntax – const datatype const_name=value;
• const float pi=3.14;
• Numerical, character, floating point, String
Defined constants
• Appearing at the beginning of the main program, specifies that the identifier will be
replaced by text
• #define PI 3.14 (PI will be replaced by text 3.14)
COMPILER TOKENS

• When a compiler analyses a program, it breaks the program into tokens

• keywords, literals, identifiers, operators, white space includes blanks , horizontal and
vertical tabs, new line
KEYWORDS
• Whose meaning is already defined to the compiler
• Keywords are not allowed to be used as variable names
• break, case, char, const, continue, default, do, double, else, float, for, goto, if, inline, int,
long, private, protected, public, short, sizeof, switch, try, void, while
IDENTIFIERS
•Various data items with symbolic name in c++ is called as identifiers
• Following data items are called as identifiers:-
1.Names of functions
2.Names of arrays
3.Variables
4.Classes
•Keywords cannot be used as identifier
•Only letters, digits and underline characters are valid
• Cannot begin with a digit
LITERALS
• Literal may be character constant, integer constant, floating point constant, string
constant
OPERATORS
• An operator does some operation on operands and this operation is specified by operators
symbol
• Unary operators(single operand) and binary operators(2 operands)
ARITHMETIC OPERATORS
• Used for mathematical calculations
• Unary arithmetic operator – needs only one operand
• Eg: +a,-a
• Binary operator – needs 2 operands
• Eg: a+b
• Ternary operator – needs 3 operands(conditional operator)
#include<iostream.h>
void main()
{
int a=4,b=3;
int c=a+b;
cout<<“sum is”<<c;
}
#include<iostream.h>
void main()
{
int a,b;
cout<<“enter Ist number \n”;
cin>>a;
cout<<“enter II nd number \n”;
cin>>b;
c=a+b;
cout<<“sum is”<<c;
}
INCREMENT AND DECREMENT
OPERATORS
• Increase or decrease the value of a variable by 1
• ++ and - -
• These operators are unary(single operand)
• Two types
• Prefix – first increment/decrement the value and then take this new value for processing
• Eg ++a
• Postfix – Frist take the value and then increment/decrement.
#include<iostream.h>
void main()
{
int a=10;
a++;
cout<<a;
}

• Output- 11
#include<iostream.h>
void main()
{
int a=10;
++a;
cout<<a;
}

• Output- 11
#include<iostream.h>
void main()
{
int a=10;
a--;
cout<<a;
}

• Output- 9
#include<iostream.h>
void main()
{
int a=10;
--a;
cout<<a;
}

• Output- 9
#include<iostream.h>
void main()
{
int a=10;
cout<<++a;
}

• Output- 11
#include<iostream.h>
void main()
{
int a=10;
cout<<a++;
}

• Output- 10
#include<iostream.h>
void main()
{
int a=10;
cout<<a++;
cout<<a;
}

• Output- 10
11
RELATIONAL OPERATORS
• These are used to test the relation between 2 values.
• A relational expression is made up of 2 arithmetic expressions connected by a relational
operator.
• It returns zero when the relation is false while nonzero when it is true.
• < - a<b – returns 1 if a is less than b otherwise zero
• These operators are having lower precedence than arithmetic operators.
LOGICAL OPERATORS
• They combine the results of one or more expression and the resultant is called as logical expression
• After testing the conditions, they return logical status(true or false)
if((a<b)&&(a<c))
{
cout<<“a is smallest”;
}
• Logical NOT operator has higher precedence
• Then logical AND
• Then logical OR
• Lower precedence than arithmetic and relational operators
CONDITIONAL OPERATOR
• It is ternary operator taking 3 arguments.
• Equivalent to if…else control structure
• a?b:c if a is nonzero, result is b, otherwise it is c
• max= ((a>b)?a:b)
ASSIGNMENT OPERATOR
• Symbol ( = )
• value of the right hand operator to be assigned to the left hand operator
• X=5;
• value 5 to be assigned to x
COMPOUND OPERATORS
• Combining assignment with arithmetic operators
• a + = b;
a = a+b
BITWISE OPERATOR
• They are used to manipulate or modify the individual bits of the piece of data
One’s complement operator
Right shift operator
Left shift operator
Bitwise AND operator
OR
XOR
OPERATORS DEALING WITH
STREAMS
• Insertion operator(<<)
• The operator dealing with output stream is called insertion/put to operator
cout<<a;
cout <<“a”;
Extraction operator
• Get from operator
• Takes the value from the keyboard and assigns to the variable on its right
• cin>>a;
• Value entered by the user is assigned to variable a
COMMA OPERATOR
• Multiple expressions can be combined into one expression using the comma operator
• It takes 2 operands
• First evaluates the left operand and then the right operand
• Int m,n;
SIZEOF OPERATOR
• For calculating the size of any data type or data item
• Takes single operand
SCOPE RESOLUTION OPERATOR
• Same variable name can be used to have different meanings in different block
• A variable declared inside a block is said to be local to that block
{
int x=10;
}
{
int x=1;
}
Statements in the second block cannot refer to variable x declared in the first block and viceversa

{
int x=10;
{
int x=1;
}
}
global version of a variable can be accessed from with in the inner block using scope resolution operator
• #include<iostream.h>
int x=10;
void main()
{
int x=5;
cout<<“value of local x is”<<x;
cout<<“value of global x is”<<::x;
}
STREAM MANIPULATION
OPERATOR
• For formatting the data display, manipulator operators are used
• To modify or manipulate the way the data is displayed
• These are present in <iomanip.h> header file
cout<<“a=“<<a<<endl;
cout<<“b=“<<b<<endl;
• Values of a and b are displayed on 2 different lines
COMMENTS
• They are not mandatory
• They are used to make program readable
• Compiler ignores comments
#include<iostream.h>
void main()
{
int a=10;
cout<<“a=“<<a; //display the value of a on screen
}
SCOPE AND VISIBILITY
• The scope or visibility of a variable determines, whether it can be accessed from other
functions
External variables
• Defined outside any block and memory for these is allocated once.
• Even though the external variable is declared more than once, it only has memory allocated
to it once
• Variables can be declared as global variables, available to everything
• External variables/ global variables are defined outside of any functions and retain their
values during the course of a program
STATIC VARIABLES
• Variables that are local to a function
• The scope of the local variables is limited to the code level in which they are declared.
• If they are declared at the beginning of a function its scope is the whole main function.
• Local variable declared in main could not be used in the other function and vice versa
• If it is declared within a function it will be a variable with function scope, if it is declared in
a loop,its scope will be only the loop.
THANK YOU

You might also like