SlideShare a Scribd company logo
C++ Language Basics
Objectives
In this chapter you will learn about
•History of C++
•Some drawbacks of C
•A simple C++ program
•Input and Output operators
•Variable declaration
•bool Datatype
•Typecasting
•The Reference –‘&’operator
C++
1979 - First Release It was called C with Classes
1983 Renamed to C++
1998 ANSI C++
2003 STL was added
2011 C++ 11
2014 C++14
2017 C++17
 C++ is a superset of C language.
 It supports everything what is there in C
and provides extra features.
Before we start with C++,
lets see some C questions
int a ;
int a ;
int main( )
{
printf("a = %d " , a);
}
int a = 5;
int a = 10;
int main( )
{
printf("a = %d " , a);
}
 An external declaration for an object is a definition if it has
an initializer.
 An external object declaration that does not have an
initializer, and does not contain the extern specifier, is a
tentative definition.
 If a definition for an object appears in a translation unit, any
tentative definitions are treated merely as redundant
declarations.
 If no definition for the object appears in the translation unit,
all its tentative definitions become a single definition with
initializer 0.
int main( )
{
const int x ;
printf("x = %d n",x);
}
C: It is not compulsory to initialize const;
C++: Constants should be initialized in C++.
int main( )
{
const int x = 5;
printf("Enter a number : ");
scanf(" %d" , &x);
printf("x = %d n",x);
}
 Scanf cannot verify if the argument is
const variable or not.
 It allows us to modify the value of const
variable.
#include <iostream>
int main( )
{
std::cout << "Hello World";
return 0;
}
#include <iostream>
int main( )
{
int num1 = 0;
int num2 = 0;
std::cout << "Enter two numbers :";
std::cin >> num1;
std::cin >> num2;
int sum = num1 + num2;
std::cout << sum;
return 0;
}
#include <iostream>
using namespace std;
int main( )
{
int num1 = 0;
int num2 = 0;
cout << "Enter two numbers :";
cin >> num1 >> num2;
int sum = num1 + num2;
cout << “Sum = “ << sum;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int regno;
char name[20];
cout << “Enter your reg no. :“ ;
cin >> regno;
cout << "Enter your name :“ ;
cin >> name;
cout << "REG NO : " << regno << endl ;
cout << "Name :" << name << endl;
return 0;
}
 The ‘cout’is an object of the class ‘ostream’
 It inserts data into the console output stream
 << - called the Insertion Operator or put to operator, It
directs the contents of the variable on its right to the
object on its left.
 The ‘cin’is an object of the class ‘istream’
 It extracts data from the console input stream
 >> - called the Extraction or get from operator, it takes
the value from the stream object on its left and places it
in the variable on its right.
 Use <iostream> instead of using <iostream.h>
 The <iostream> file provides support for all
console-related input –output stream based
operations
 using namespace std;
 ‘std’is known as the standard namespace and is
pre-defined
#include <iostream>
using namespace std;
int main( )
{
const int x = 5;
cout << "Enter a number : ";
cin >> x;
cout << "x =" << x;
}
#include <stdio.h>
int main( )
{
const int x = 5;
printf("Enter a number : “);
scanf(“ %d”,&x);
printf("x = %d” , x);
}
 C allows a variable to be declared only at the
beginning of a function (or to be more precise,
beginning of a block)
 C++ supports ‘anywhere declaration’ –before
the first use of the variable
int x;
cin>>x;
int y;
cin>>y;
int res = x + y;
cout<<res<<endl;
for(int i=0;i<10;++i)
cout<<i<<endl;
 “bool” is a new C++ data type
 It is used like a flag for signifying occurrence of
some condition
 Takes only two values defined by two new
keywords
• true – 1
• false - 0
bool powerof2(int
num)
{ // if only 1 bit is set
if(!(num & num-1))
return true;
else
return false;
}
bool search(int a[],int n,int key)
{
for(int i = 0 ; i < n ; i++)
if(a[i] == key)
return true;
return false;
}
 The following types of type-casting are supported
in C++
 double x = 10.5;
 int y1 = (int) x; // c -style casting
 cout<<y1<<endl;
 int y2 = int(x); // c++ -style casting(function style)
 cout<<y2<<endl;
 int y3 = static_cast<int>(x); //C++ 98
void update(int *a)
{
*++a;
printf("Update %d " , *a);
}
int main( )
{
int a = 5;
update(&a);
printf("Main %d n" , a);
}
void update(int a)
{
++a;
printf("Update %d " , a);
}
int main( )
{
int a = 5;
update(a);
printf("Main %d n" , a);
}
void myswap(int *a,int *b)
{
int *temp = a;
a = b;
b = temp;
printf("myswap a=%d b = %d n" , *a , *b);
}
int main( )
{
int a = 5 , b = 10;
myswap(&a , & b);
printf("Main a=%d b = %d n" , a , b);
}
 The previous two code snippets showed
us how easily we can go wrong with
pointer.
 The Reference ‘&’operator is used in C++ to
create aliases to other existing variables /
objects
TYPE &refName= varName;
int x = 10;
int &y = x;
cout<<x<<‘ ‘<<y<<endl;
++y;
cout<<x<<‘ ‘<<y<<endl;
10
x
y
int x = 10;
int y = x;
cout<<x<<‘ ‘<<y<<endl;
++y;
cout<<x<<‘ ‘<<y<<endl;
10
x
y
int x = 10;
int &y = x;
cout<<x<<‘ ‘<<y<<endl;
++y;
cout<<x<<‘ ‘<<y<<endl;
10
10
x
y
 A reference should always be initialized.
 A reference cannot refer to constant
literals. It can only refer to
objects(variables).
• int &reference;
• int &reference = 5;
• int a = 5, b = 10;
• int &reference = a + b;
• Once a reference is created, we can refer to that location using
either of the names.
• & is only used to create a reference.
• To refer to value of x using reference, we do not use &
void myswap(int a,int b)
{
a = a ^ b;
b = a ^ b;
a = a ^ b;
}
int main()
{
int a = 10,b = 20;
myswap(a,b);
cout<<a<<'t'<<b<<endl;
return 0;
}
void myswap(int *a,int *b)
{
*a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
}
int main()
{
int a = 10,b = 20;
myswap(&a,&b);
cout<<a<<'t'<<b<<endl;
return 0;
}
Call by Value Call by Address
void myswap(int a,int b)
{
a = a ^ b;
b = a ^ b;
a = a ^ b;
}
int main()
{
int a = 10,b = 20;
myswap(a,b);
cout<<a<<'t'<<b<<endl;
return 0;
}
void myswap(int &a,int &b)
{
a = a ^ b;
b = a ^ b;
a = a ^ b;
}
int main()
{
int a = 10,b = 20;
myswap(a,b);
cout<<a<<'t'<<b<<endl;
return 0;
}
Call by Value Call by Reference
 The function call is made in the same
manner as Call By Value
• Whether a reference is taken for the actual
arguments
OR
• a new variable is created and the value is copied
is made transparent to the invoker of the function
 Reference should always be initialised,
pointers need not be initialised.
 No NULL reference - A reference must always
refer to some object
 Pointers may be reassigned to refer to different
objects. A reference, however, always refers to
the object with which it is initialized
CPP Language Basics - Reference

More Related Content

What's hot

Collection v3
Collection v3Collection v3
Collection v3
Sunil OS
 
Java 8 Default Methods
Java 8 Default MethodsJava 8 Default Methods
Java 8 Default Methods
Haim Michael
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
Mario Fusco
 
Clean code and Code Smells
Clean code and Code SmellsClean code and Code Smells
Clean code and Code Smells
Mario Sangiorgio
 
Java 8 - CJ
Java 8 - CJJava 8 - CJ
Java 8 - CJ
Sunil OS
 
7 rules of simple and maintainable code
7 rules of simple and maintainable code7 rules of simple and maintainable code
7 rules of simple and maintainable code
Geshan Manandhar
 
Java Basics V3
Java Basics V3Java Basics V3
Java Basics V3
Sunil OS
 
JDBC
JDBCJDBC
JDBC
Sunil OS
 
OOP V3.1
OOP V3.1OOP V3.1
OOP V3.1
Sunil OS
 
Implicit parameters, when to use them (or not)!
Implicit parameters, when to use them (or not)!Implicit parameters, when to use them (or not)!
Implicit parameters, when to use them (or not)!
Julien Truffaut
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
Ahmed Shawky El-faky
 
Exception Handling
Exception HandlingException Handling
Exception Handling
Sunil OS
 
[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...
[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...
[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...
Seok-joon Yun
 
C++ Function
C++ FunctionC++ Function
C++ FunctionHajar
 
JAVA Variables and Operators
JAVA Variables and OperatorsJAVA Variables and Operators
JAVA Variables and Operators
Sunil OS
 
Java collections notes
Java collections notesJava collections notes
Java collections notes
Surendar Meesala
 
Async programming and python
Async programming and pythonAsync programming and python
Async programming and python
Chetan Giridhar
 
Friend function in c++
Friend function in c++Friend function in c++
Friend function in c++
Jagrati Mehra
 
Introduction to advanced python
Introduction to advanced pythonIntroduction to advanced python
Introduction to advanced python
Charles-Axel Dein
 
Understanding storage class using nm
Understanding storage class using nmUnderstanding storage class using nm
Understanding storage class using nm
mohamed sikander
 

What's hot (20)

Collection v3
Collection v3Collection v3
Collection v3
 
Java 8 Default Methods
Java 8 Default MethodsJava 8 Default Methods
Java 8 Default Methods
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
Clean code and Code Smells
Clean code and Code SmellsClean code and Code Smells
Clean code and Code Smells
 
Java 8 - CJ
Java 8 - CJJava 8 - CJ
Java 8 - CJ
 
7 rules of simple and maintainable code
7 rules of simple and maintainable code7 rules of simple and maintainable code
7 rules of simple and maintainable code
 
Java Basics V3
Java Basics V3Java Basics V3
Java Basics V3
 
JDBC
JDBCJDBC
JDBC
 
OOP V3.1
OOP V3.1OOP V3.1
OOP V3.1
 
Implicit parameters, when to use them (or not)!
Implicit parameters, when to use them (or not)!Implicit parameters, when to use them (or not)!
Implicit parameters, when to use them (or not)!
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...
[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...
[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
JAVA Variables and Operators
JAVA Variables and OperatorsJAVA Variables and Operators
JAVA Variables and Operators
 
Java collections notes
Java collections notesJava collections notes
Java collections notes
 
Async programming and python
Async programming and pythonAsync programming and python
Async programming and python
 
Friend function in c++
Friend function in c++Friend function in c++
Friend function in c++
 
Introduction to advanced python
Introduction to advanced pythonIntroduction to advanced python
Introduction to advanced python
 
Understanding storage class using nm
Understanding storage class using nmUnderstanding storage class using nm
Understanding storage class using nm
 

Viewers also liked

INGENIERIA GEOGRAFICA Y AMBIENTAL
INGENIERIA GEOGRAFICA Y AMBIENTALINGENIERIA GEOGRAFICA Y AMBIENTAL
INGENIERIA GEOGRAFICA Y AMBIENTAL
Mateo47
 
El Hombre Y Su Comunidad (M.I)
El Hombre Y Su Comunidad  (M.I)El Hombre Y Su Comunidad  (M.I)
El Hombre Y Su Comunidad (M.I)
Tabernáculo De Adoración Adonay
 
Mercurial Quick Tutorial
Mercurial Quick TutorialMercurial Quick Tutorial
Mercurial Quick Tutorial
晟 沈
 
La tua europa
La tua europaLa tua europa
La tua europa
Luigi A. Dell'Aquila
 
Growing a Data Pipeline for Analytics
Growing a Data Pipeline for AnalyticsGrowing a Data Pipeline for Analytics
Growing a Data Pipeline for Analytics
Roberto Agostino Vitillo
 
Java and Java platforms
Java and Java platformsJava and Java platforms
Java and Java platforms
Ilio Catallo
 
Desafío no. 32
Desafío no. 32Desafío no. 32
EVALUACION EDUCACION FISICA
EVALUACION EDUCACION FISICAEVALUACION EDUCACION FISICA
EVALUACION EDUCACION FISICA
cursocecam07
 
Managing the Maturity of Tourism Destinations: the Challenge of Governance an...
Managing the Maturity of Tourism Destinations: the Challenge of Governance an...Managing the Maturity of Tourism Destinations: the Challenge of Governance an...
Managing the Maturity of Tourism Destinations: the Challenge of Governance an...
FEST
 
Kti efta mayasari
Kti efta mayasariKti efta mayasari
Kti efta mayasari
KTIeftamayasari
 
Understanding and using while in c++
Understanding and using while in c++Understanding and using while in c++
Understanding and using while in c++
MOHANA ALMUQATI
 
Grade 10 flowcharting
Grade 10  flowchartingGrade 10  flowcharting
Grade 10 flowcharting
Rafael Balderosa
 
Evaluacion escala de rango 2016 2017
Evaluacion escala de rango 2016 2017Evaluacion escala de rango 2016 2017
Evaluacion escala de rango 2016 2017
Jesus Contreras Baez
 
C++ basics
C++ basicsC++ basics
C++ basics
husnara mohammad
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 

Viewers also liked (18)

INGENIERIA GEOGRAFICA Y AMBIENTAL
INGENIERIA GEOGRAFICA Y AMBIENTALINGENIERIA GEOGRAFICA Y AMBIENTAL
INGENIERIA GEOGRAFICA Y AMBIENTAL
 
El Hombre Y Su Comunidad (M.I)
El Hombre Y Su Comunidad  (M.I)El Hombre Y Su Comunidad  (M.I)
El Hombre Y Su Comunidad (M.I)
 
12 SQL
12 SQL12 SQL
12 SQL
 
Mercurial Quick Tutorial
Mercurial Quick TutorialMercurial Quick Tutorial
Mercurial Quick Tutorial
 
La tua europa
La tua europaLa tua europa
La tua europa
 
Growing a Data Pipeline for Analytics
Growing a Data Pipeline for AnalyticsGrowing a Data Pipeline for Analytics
Growing a Data Pipeline for Analytics
 
Java and Java platforms
Java and Java platformsJava and Java platforms
Java and Java platforms
 
Desafío no. 32
Desafío no. 32Desafío no. 32
Desafío no. 32
 
EVALUACION EDUCACION FISICA
EVALUACION EDUCACION FISICAEVALUACION EDUCACION FISICA
EVALUACION EDUCACION FISICA
 
Managing the Maturity of Tourism Destinations: the Challenge of Governance an...
Managing the Maturity of Tourism Destinations: the Challenge of Governance an...Managing the Maturity of Tourism Destinations: the Challenge of Governance an...
Managing the Maturity of Tourism Destinations: the Challenge of Governance an...
 
Kti efta mayasari
Kti efta mayasariKti efta mayasari
Kti efta mayasari
 
ch 3
ch 3ch 3
ch 3
 
Understanding and using while in c++
Understanding and using while in c++Understanding and using while in c++
Understanding and using while in c++
 
Grade 10 flowcharting
Grade 10  flowchartingGrade 10  flowcharting
Grade 10 flowcharting
 
Evaluacion escala de rango 2016 2017
Evaluacion escala de rango 2016 2017Evaluacion escala de rango 2016 2017
Evaluacion escala de rango 2016 2017
 
C++ basics
C++ basicsC++ basics
C++ basics
 
Materiali lapidei artificiali 4 - Architettura romana
Materiali lapidei artificiali 4 - Architettura romanaMateriali lapidei artificiali 4 - Architettura romana
Materiali lapidei artificiali 4 - Architettura romana
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 

Similar to CPP Language Basics - Reference

Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Meghaj Mallick
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
ssuser3cbb4c
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
WaheedAnwar20
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
yamew16788
 
C++ practical
C++ practicalC++ practical
C++ practical
Rahul juneja
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
ShashiShash2
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
Abubakar524802
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
premrings
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
TarekHemdan3
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
Chris Ohk
 
C++ Language
C++ LanguageC++ Language
C++ Language
Syed Zaid Irshad
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
satvirsandhu9
 
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays 2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
kinan keshkeh
 
C++11 & C++14
C++11 & C++14C++11 & C++14
C++11 & C++14
CyberPlusIndia
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
kinan keshkeh
 
Lập trình C
Lập trình CLập trình C
Lập trình C
Viet NguyenHoang
 

Similar to CPP Language Basics - Reference (20)

Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
 
C++ practical
C++ practicalC++ practical
C++ practical
 
Lecture 3 c++
Lecture 3 c++Lecture 3 c++
Lecture 3 c++
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
 
CP 04.pptx
CP 04.pptxCP 04.pptx
CP 04.pptx
 
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays 2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
 
C++11 & C++14
C++11 & C++14C++11 & C++14
C++11 & C++14
 
C++ file
C++ fileC++ file
C++ file
 
C++ file
C++ fileC++ file
C++ file
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 

More from Mohammed Sikander

Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Mohammed Sikander
 
Python_Regular Expression
Python_Regular ExpressionPython_Regular Expression
Python_Regular Expression
Mohammed Sikander
 
Modern_CPP-Range-Based For Loop.pptx
Modern_CPP-Range-Based For Loop.pptxModern_CPP-Range-Based For Loop.pptx
Modern_CPP-Range-Based For Loop.pptx
Mohammed Sikander
 
Modern_cpp_auto.pdf
Modern_cpp_auto.pdfModern_cpp_auto.pdf
Modern_cpp_auto.pdf
Mohammed Sikander
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
Mohammed Sikander
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
Mohammed Sikander
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
Mohammed Sikander
 
Python tuple
Python   tuplePython   tuple
Python tuple
Mohammed Sikander
 
Python strings
Python stringsPython strings
Python strings
Mohammed Sikander
 
Python set
Python setPython set
Python set
Mohammed Sikander
 
Python list
Python listPython list
Python list
Mohammed Sikander
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
Mohammed Sikander
 
Introduction to Python
Introduction to Python  Introduction to Python
Introduction to Python
Mohammed Sikander
 
Pointer basics
Pointer basicsPointer basics
Pointer basics
Mohammed Sikander
 
Pipe
PipePipe
Signal
SignalSignal
File management
File managementFile management
File management
Mohammed Sikander
 
Java arrays
Java    arraysJava    arrays
Java arrays
Mohammed Sikander
 
Java strings
Java   stringsJava   strings
Java strings
Mohammed Sikander
 

More from Mohammed Sikander (20)

Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Python_Regular Expression
Python_Regular ExpressionPython_Regular Expression
Python_Regular Expression
 
Modern_CPP-Range-Based For Loop.pptx
Modern_CPP-Range-Based For Loop.pptxModern_CPP-Range-Based For Loop.pptx
Modern_CPP-Range-Based For Loop.pptx
 
Modern_cpp_auto.pdf
Modern_cpp_auto.pdfModern_cpp_auto.pdf
Modern_cpp_auto.pdf
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Python tuple
Python   tuplePython   tuple
Python tuple
 
Python strings
Python stringsPython strings
Python strings
 
Python set
Python setPython set
Python set
 
Python list
Python listPython list
Python list
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
Introduction to Python
Introduction to Python  Introduction to Python
Introduction to Python
 
Pointer basics
Pointer basicsPointer basics
Pointer basics
 
Pipe
PipePipe
Pipe
 
Signal
SignalSignal
Signal
 
File management
File managementFile management
File management
 
Java arrays
Java    arraysJava    arrays
Java arrays
 
Java strings
Java   stringsJava   strings
Java strings
 

Recently uploaded

Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Hivelance Technology
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
Jelle | Nordend
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
Sharepoint Designs
 
Software Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdfSoftware Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdf
MayankTawar1
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
Peter Caitens
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
Why React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdfWhy React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdf
ayushiqss
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
XfilesPro
 

Recently uploaded (20)

Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
 
Software Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdfSoftware Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdf
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Why React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdfWhy React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdf
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 

CPP Language Basics - Reference

  • 2. Objectives In this chapter you will learn about •History of C++ •Some drawbacks of C •A simple C++ program •Input and Output operators •Variable declaration •bool Datatype •Typecasting •The Reference –‘&’operator
  • 3. C++ 1979 - First Release It was called C with Classes 1983 Renamed to C++ 1998 ANSI C++ 2003 STL was added 2011 C++ 11 2014 C++14 2017 C++17
  • 4.  C++ is a superset of C language.  It supports everything what is there in C and provides extra features.
  • 5. Before we start with C++, lets see some C questions
  • 6. int a ; int a ; int main( ) { printf("a = %d " , a); } int a = 5; int a = 10; int main( ) { printf("a = %d " , a); }
  • 7.  An external declaration for an object is a definition if it has an initializer.  An external object declaration that does not have an initializer, and does not contain the extern specifier, is a tentative definition.  If a definition for an object appears in a translation unit, any tentative definitions are treated merely as redundant declarations.  If no definition for the object appears in the translation unit, all its tentative definitions become a single definition with initializer 0.
  • 8. int main( ) { const int x ; printf("x = %d n",x); } C: It is not compulsory to initialize const; C++: Constants should be initialized in C++.
  • 9. int main( ) { const int x = 5; printf("Enter a number : "); scanf(" %d" , &x); printf("x = %d n",x); }
  • 10.  Scanf cannot verify if the argument is const variable or not.  It allows us to modify the value of const variable.
  • 11. #include <iostream> int main( ) { std::cout << "Hello World"; return 0; }
  • 12. #include <iostream> int main( ) { int num1 = 0; int num2 = 0; std::cout << "Enter two numbers :"; std::cin >> num1; std::cin >> num2; int sum = num1 + num2; std::cout << sum; return 0; }
  • 13. #include <iostream> using namespace std; int main( ) { int num1 = 0; int num2 = 0; cout << "Enter two numbers :"; cin >> num1 >> num2; int sum = num1 + num2; cout << “Sum = “ << sum; return 0; }
  • 14. #include <iostream> using namespace std; int main() { int regno; char name[20]; cout << “Enter your reg no. :“ ; cin >> regno; cout << "Enter your name :“ ; cin >> name; cout << "REG NO : " << regno << endl ; cout << "Name :" << name << endl; return 0; }
  • 15.  The ‘cout’is an object of the class ‘ostream’  It inserts data into the console output stream  << - called the Insertion Operator or put to operator, It directs the contents of the variable on its right to the object on its left.  The ‘cin’is an object of the class ‘istream’  It extracts data from the console input stream  >> - called the Extraction or get from operator, it takes the value from the stream object on its left and places it in the variable on its right.
  • 16.  Use <iostream> instead of using <iostream.h>  The <iostream> file provides support for all console-related input –output stream based operations  using namespace std;  ‘std’is known as the standard namespace and is pre-defined
  • 17. #include <iostream> using namespace std; int main( ) { const int x = 5; cout << "Enter a number : "; cin >> x; cout << "x =" << x; } #include <stdio.h> int main( ) { const int x = 5; printf("Enter a number : “); scanf(“ %d”,&x); printf("x = %d” , x); }
  • 18.  C allows a variable to be declared only at the beginning of a function (or to be more precise, beginning of a block)  C++ supports ‘anywhere declaration’ –before the first use of the variable int x; cin>>x; int y; cin>>y; int res = x + y; cout<<res<<endl; for(int i=0;i<10;++i) cout<<i<<endl;
  • 19.  “bool” is a new C++ data type  It is used like a flag for signifying occurrence of some condition  Takes only two values defined by two new keywords • true – 1 • false - 0 bool powerof2(int num) { // if only 1 bit is set if(!(num & num-1)) return true; else return false; } bool search(int a[],int n,int key) { for(int i = 0 ; i < n ; i++) if(a[i] == key) return true; return false; }
  • 20.  The following types of type-casting are supported in C++  double x = 10.5;  int y1 = (int) x; // c -style casting  cout<<y1<<endl;  int y2 = int(x); // c++ -style casting(function style)  cout<<y2<<endl;  int y3 = static_cast<int>(x); //C++ 98
  • 21. void update(int *a) { *++a; printf("Update %d " , *a); } int main( ) { int a = 5; update(&a); printf("Main %d n" , a); } void update(int a) { ++a; printf("Update %d " , a); } int main( ) { int a = 5; update(a); printf("Main %d n" , a); }
  • 22. void myswap(int *a,int *b) { int *temp = a; a = b; b = temp; printf("myswap a=%d b = %d n" , *a , *b); } int main( ) { int a = 5 , b = 10; myswap(&a , & b); printf("Main a=%d b = %d n" , a , b); }
  • 23.  The previous two code snippets showed us how easily we can go wrong with pointer.
  • 24.  The Reference ‘&’operator is used in C++ to create aliases to other existing variables / objects TYPE &refName= varName; int x = 10; int &y = x; cout<<x<<‘ ‘<<y<<endl; ++y; cout<<x<<‘ ‘<<y<<endl; 10 x y
  • 25. int x = 10; int y = x; cout<<x<<‘ ‘<<y<<endl; ++y; cout<<x<<‘ ‘<<y<<endl; 10 x y int x = 10; int &y = x; cout<<x<<‘ ‘<<y<<endl; ++y; cout<<x<<‘ ‘<<y<<endl; 10 10 x y
  • 26.  A reference should always be initialized.  A reference cannot refer to constant literals. It can only refer to objects(variables). • int &reference; • int &reference = 5; • int a = 5, b = 10; • int &reference = a + b;
  • 27. • Once a reference is created, we can refer to that location using either of the names. • & is only used to create a reference. • To refer to value of x using reference, we do not use &
  • 28.
  • 29.
  • 30.
  • 31.
  • 32. void myswap(int a,int b) { a = a ^ b; b = a ^ b; a = a ^ b; } int main() { int a = 10,b = 20; myswap(a,b); cout<<a<<'t'<<b<<endl; return 0; } void myswap(int *a,int *b) { *a = *a ^ *b; *b = *a ^ *b; *a = *a ^ *b; } int main() { int a = 10,b = 20; myswap(&a,&b); cout<<a<<'t'<<b<<endl; return 0; } Call by Value Call by Address
  • 33. void myswap(int a,int b) { a = a ^ b; b = a ^ b; a = a ^ b; } int main() { int a = 10,b = 20; myswap(a,b); cout<<a<<'t'<<b<<endl; return 0; } void myswap(int &a,int &b) { a = a ^ b; b = a ^ b; a = a ^ b; } int main() { int a = 10,b = 20; myswap(a,b); cout<<a<<'t'<<b<<endl; return 0; } Call by Value Call by Reference
  • 34.  The function call is made in the same manner as Call By Value • Whether a reference is taken for the actual arguments OR • a new variable is created and the value is copied is made transparent to the invoker of the function
  • 35.
  • 36.
  • 37.
  • 38.  Reference should always be initialised, pointers need not be initialised.  No NULL reference - A reference must always refer to some object  Pointers may be reassigned to refer to different objects. A reference, however, always refers to the object with which it is initialized

Editor's Notes

  1. Try this in C and C++
  2. Program : 1_IO.cpp
  3. cin&amp;gt;&amp;gt;regno;No Need for format specifier or address of(&amp;) operator
  4. Basic information about namespace to be added Namespace.cpp
  5. 2_bool.cpp int main() { bool a = -5; cout &amp;lt;&amp;lt;&amp;quot;a = &amp;quot;&amp;lt;&amp;lt;a;//Print 1 return 0; }
  6. typecasting.cpp
  7. 3_reference.cpp
  8. 3_reference.cpp
  9. #include &amp;lt;iostream&amp;gt; using namespace std; C Style of Pass by Reference void update(int *x) { *x = *x + 3; cout &amp;lt;&amp;lt;&amp;quot;In update x = &amp;quot; &amp;lt;&amp;lt; *x &amp;lt;&amp;lt; endl; } int main( ){ int x = 5; update( &amp;x ); cout &amp;lt;&amp;lt;&amp;quot;In update x = &amp;quot; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; endl; }
  10. 4_swapref.cpp
  11. #include &amp;lt;iostream&amp;gt; using namespace std; //Constant References? int main() { int x = 5; const int &amp;a = x; cout &amp;lt;&amp;lt; x &amp;lt;&amp;lt;&amp;quot; &amp;quot; &amp;lt;&amp;lt; a &amp;lt;&amp;lt; endl; a = 10; //Invalid. x = 20; cout &amp;lt;&amp;lt; x &amp;lt;&amp;lt;&amp;quot; &amp;quot; &amp;lt;&amp;lt; a &amp;lt;&amp;lt; endl; return 0; }
  12. #include &amp;lt;iostream&amp;gt; using namespace std; //Constant References? int main() { const int x = 5; int &amp;a = x; //Error const int &amp;b = x; //Valid return 0; }