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 &
CPP Language Basics - Reference
CPP Language Basics - Reference
CPP Language Basics - Reference
CPP Language Basics - Reference
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
CPP Language Basics - Reference
CPP Language Basics - Reference
CPP Language Basics - Reference
 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
Ad

Recommended

Implementing String
Implementing String
MohammedSadiq211406
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Mohammed Sikander
 
Operator Overloading in C++
Operator Overloading in C++
Mohammed Sikander
 
Functions in C++
Functions in C++
Mohammed Sikander
 
User Defined Functions
User Defined Functions
Praveen M Jigajinni
 
Python in 30 minutes!
Python in 30 minutes!
Fariz Darari
 
Functions in c language
Functions in c language
tanmaymodi4
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructors
Praveen M Jigajinni
 
Python Sequence | Python Lists | Python Sets & Dictionary | Python Strings | ...
Python Sequence | Python Lists | Python Sets & Dictionary | Python Strings | ...
Edureka!
 
4. THREE DIMENSIONAL DISPLAY METHODS
4. THREE DIMENSIONAL DISPLAY METHODS
SanthiNivas
 
Graphics in C programming
Graphics in C programming
Kamal Acharya
 
Js: master prototypes
Js: master prototypes
Barak Drechsler
 
Line clipping
Line clipping
Ankit Garg
 
interface in c#
interface in c#
Deepti Pillai
 
Static Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Introduction to graphics programming in c
Introduction to graphics programming in c
baabtra.com - No. 1 supplier of quality freshers
 
Constructor
Constructor
poonamchopra7975
 
Recursion in c
Recursion in c
Saket Pathak
 
RECURSION IN C
RECURSION IN C
v_jk
 
java interface and packages
java interface and packages
VINOTH R
 
SQLite database in android
SQLite database in android
Gourav Kumar Saini
 
C++ Files and Streams
C++ Files and Streams
Ahmed Farag
 
Managing console input and output
Managing console input and output
gourav kottawar
 
Recursion in C++
Recursion in C++
Maliha Mehr
 
visible surface detection
visible surface detection
Balakumaran Arunachalam
 
array of object pointer in c++
array of object pointer in c++
Arpita Patel
 
Inheritance in c++
Inheritance in c++
Paumil Patel
 
Python Modules
Python Modules
Nitin Reddy Katkam
 
INGENIERIA GEOGRAFICA Y AMBIENTAL
INGENIERIA GEOGRAFICA Y AMBIENTAL
Mateo47
 
El Hombre Y Su Comunidad (M.I)
El Hombre Y Su Comunidad (M.I)
Tabernáculo De Adoración Adonay
 

More Related Content

What's hot (20)

Python Sequence | Python Lists | Python Sets & Dictionary | Python Strings | ...
Python Sequence | Python Lists | Python Sets & Dictionary | Python Strings | ...
Edureka!
 
4. THREE DIMENSIONAL DISPLAY METHODS
4. THREE DIMENSIONAL DISPLAY METHODS
SanthiNivas
 
Graphics in C programming
Graphics in C programming
Kamal Acharya
 
Js: master prototypes
Js: master prototypes
Barak Drechsler
 
Line clipping
Line clipping
Ankit Garg
 
interface in c#
interface in c#
Deepti Pillai
 
Static Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Introduction to graphics programming in c
Introduction to graphics programming in c
baabtra.com - No. 1 supplier of quality freshers
 
Constructor
Constructor
poonamchopra7975
 
Recursion in c
Recursion in c
Saket Pathak
 
RECURSION IN C
RECURSION IN C
v_jk
 
java interface and packages
java interface and packages
VINOTH R
 
SQLite database in android
SQLite database in android
Gourav Kumar Saini
 
C++ Files and Streams
C++ Files and Streams
Ahmed Farag
 
Managing console input and output
Managing console input and output
gourav kottawar
 
Recursion in C++
Recursion in C++
Maliha Mehr
 
visible surface detection
visible surface detection
Balakumaran Arunachalam
 
array of object pointer in c++
array of object pointer in c++
Arpita Patel
 
Inheritance in c++
Inheritance in c++
Paumil Patel
 
Python Modules
Python Modules
Nitin Reddy Katkam
 
Python Sequence | Python Lists | Python Sets & Dictionary | Python Strings | ...
Python Sequence | Python Lists | Python Sets & Dictionary | Python Strings | ...
Edureka!
 
4. THREE DIMENSIONAL DISPLAY METHODS
4. THREE DIMENSIONAL DISPLAY METHODS
SanthiNivas
 
Graphics in C programming
Graphics in C programming
Kamal Acharya
 
Static Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
RECURSION IN C
RECURSION IN C
v_jk
 
java interface and packages
java interface and packages
VINOTH R
 
C++ Files and Streams
C++ Files and Streams
Ahmed Farag
 
Managing console input and output
Managing console input and output
gourav kottawar
 
Recursion in C++
Recursion in C++
Maliha Mehr
 
array of object pointer in c++
array of object pointer in c++
Arpita Patel
 
Inheritance in c++
Inheritance in c++
Paumil Patel
 

Viewers also liked (18)

INGENIERIA GEOGRAFICA Y AMBIENTAL
INGENIERIA GEOGRAFICA Y AMBIENTAL
Mateo47
 
El Hombre Y Su Comunidad (M.I)
El Hombre Y Su Comunidad (M.I)
Tabernáculo De Adoración Adonay
 
12 SQL
12 SQL
Praveen M Jigajinni
 
Mercurial Quick Tutorial
Mercurial Quick Tutorial
晟 沈
 
La tua europa
La tua europa
Luigi A. Dell'Aquila
 
Growing a Data Pipeline for Analytics
Growing a Data Pipeline for Analytics
Roberto Agostino Vitillo
 
Java and Java platforms
Java and Java platforms
Ilio Catallo
 
Desafío no. 32
Desafío no. 32
Tabernáculo De Adoración Adonay
 
EVALUACION 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...
FEST
 
Kti efta mayasari
Kti efta mayasari
KTIeftamayasari
 
ch 3
ch 3
Uzair Javed
 
Understanding and using while in c++
Understanding and using while in c++
MOHANA ALMUQATI
 
Grade 10 flowcharting
Grade 10 flowcharting
Rafael Balderosa
 
Evaluacion escala de rango 2016 2017
Evaluacion escala de rango 2016 2017
Jesus Contreras Baez
 
C++ basics
C++ basics
husnara mohammad
 
Materiali lapidei artificiali 4 - Architettura romana
Materiali lapidei artificiali 4 - Architettura romana
STORIA DEI MATERIALI. Materiale didattico
 
Class and object in C++
Class and object in C++
rprajat007
 
Ad

Similar to CPP Language Basics - Reference (20)

CPP-overviews notes variable data types notes
CPP-overviews notes variable data types notes
SukhpreetSingh519414
 
cppt-170218053903 (1).pptx
cppt-170218053903 (1).pptx
WatchDog13
 
c++ introduction, array, pointers included.pptx
c++ introduction, array, pointers included.pptx
fn723290
 
Introduction to c++
Introduction to c++
somu rajesh
 
C++ Overview PPT
C++ Overview PPT
Thooyavan Venkatachalam
 
C++
C++
VishalMishra313
 
cppt-170218053903.pptxhjkjhjjjjjjjjjjjjjjj
cppt-170218053903.pptxhjkjhjjjjjjjjjjjjjjj
supriyaharlapur1
 
C++ Language
C++ Language
Syed Zaid Irshad
 
C_plus_plus
C_plus_plus
Ralph Weber
 
C++ Introduction
C++ Introduction
parmsidhu
 
C++ theory
C++ theory
Shyam Khant
 
C++ L01-Variables
C++ L01-Variables
Mohammad Shaker
 
Presentation on C++ Programming Language
Presentation on C++ Programming Language
satvirsandhu9
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++
Rasan Samarasinghe
 
C++ overview
C++ overview
Prem Ranjan
 
BASIC C++ PROGRAMMING
BASIC C++ PROGRAMMING
gufranresearcher
 
Chapter02-S11.ppt
Chapter02-S11.ppt
GhulamHussain638563
 
C cpluplus 2
C cpluplus 2
sanya6900
 
Chapter 2 Introduction to C++
Chapter 2 Introduction to C++
GhulamHussain142878
 
Introduction to C++ lecture ************
Introduction to C++ lecture ************
Emad Helal
 
CPP-overviews notes variable data types notes
CPP-overviews notes variable data types notes
SukhpreetSingh519414
 
cppt-170218053903 (1).pptx
cppt-170218053903 (1).pptx
WatchDog13
 
c++ introduction, array, pointers included.pptx
c++ introduction, array, pointers included.pptx
fn723290
 
Introduction to c++
Introduction to c++
somu rajesh
 
cppt-170218053903.pptxhjkjhjjjjjjjjjjjjjjj
cppt-170218053903.pptxhjkjhjjjjjjjjjjjjjjj
supriyaharlapur1
 
C++ Introduction
C++ Introduction
parmsidhu
 
Presentation on C++ Programming Language
Presentation on C++ Programming Language
satvirsandhu9
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++
Rasan Samarasinghe
 
C cpluplus 2
C cpluplus 2
sanya6900
 
Introduction to C++ lecture ************
Introduction to C++ lecture ************
Emad Helal
 
Ad

More from Mohammed Sikander (20)

Strings in C - covers string functions
Strings in C - covers string functions
Mohammed Sikander
 
Smart Pointers, Modern Memory Management Techniques
Smart Pointers, Modern Memory Management Techniques
Mohammed Sikander
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Python_Regular Expression
Python_Regular Expression
Mohammed Sikander
 
Modern_CPP-Range-Based For Loop.pptx
Modern_CPP-Range-Based For Loop.pptx
Mohammed Sikander
 
Modern_cpp_auto.pdf
Modern_cpp_auto.pdf
Mohammed Sikander
 
Python Functions
Python Functions
Mohammed Sikander
 
Python dictionary
Python dictionary
Mohammed Sikander
 
Python exception handling
Python exception handling
Mohammed Sikander
 
Python tuple
Python tuple
Mohammed Sikander
 
Python strings
Python strings
Mohammed Sikander
 
Python set
Python set
Mohammed Sikander
 
Python list
Python list
Mohammed Sikander
 
Python Flow Control
Python Flow Control
Mohammed Sikander
 
Introduction to Python
Introduction to Python
Mohammed Sikander
 
Pointer basics
Pointer basics
Mohammed Sikander
 
Pipe
Pipe
Mohammed Sikander
 
Signal
Signal
Mohammed Sikander
 
File management
File management
Mohammed Sikander
 
Java arrays
Java arrays
Mohammed Sikander
 

Recently uploaded (20)

Heat Treatment Process Automation in India
Heat Treatment Process Automation in India
Reckers Mechatronics
 
Azure AI Foundry: The AI app and agent factory
Azure AI Foundry: The AI app and agent factory
Maxim Salnikov
 
Microsoft-365-Administrator-s-Guide1.pdf
Microsoft-365-Administrator-s-Guide1.pdf
mazharatknl
 
Sysinfo OST to PST Converter Infographic
Sysinfo OST to PST Converter Infographic
SysInfo Tools
 
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
Maharshi Mallela
 
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
Shane Coughlan
 
Best Practice for LLM Serving in the Cloud
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
 
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
Hassan Abid
 
HYBRIDIZATION OF ALKANES AND ALKENES ...
HYBRIDIZATION OF ALKANES AND ALKENES ...
karishmaduhijod1
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
2nd Sight Lab
 
ElectraSuite_Prsentation(online voting system).pptx
ElectraSuite_Prsentation(online voting system).pptx
mrsinankhan01
 
declaration of Variables and constants.pptx
declaration of Variables and constants.pptx
meemee7378
 
Zoneranker’s Digital marketing solutions
Zoneranker’s Digital marketing solutions
reenashriee
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Simplify Task, Team, and Project Management with Orangescrum Work
Simplify Task, Team, and Project Management with Orangescrum Work
Orangescrum
 
arctitecture application system design os dsa
arctitecture application system design os dsa
za241967
 
University Campus Navigation for All - Peak of Data & AI
University Campus Navigation for All - Peak of Data & AI
Safe Software
 
Key Challenges in Troubleshooting Customer On-Premise Applications
Key Challenges in Troubleshooting Customer On-Premise Applications
Tier1 app
 
Canva Pro Crack Free Download 2025-FREE LATEST
Canva Pro Crack Free Download 2025-FREE LATEST
grete1122g
 
Heat Treatment Process Automation in India
Heat Treatment Process Automation in India
Reckers Mechatronics
 
Azure AI Foundry: The AI app and agent factory
Azure AI Foundry: The AI app and agent factory
Maxim Salnikov
 
Microsoft-365-Administrator-s-Guide1.pdf
Microsoft-365-Administrator-s-Guide1.pdf
mazharatknl
 
Sysinfo OST to PST Converter Infographic
Sysinfo OST to PST Converter Infographic
SysInfo Tools
 
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
Maharshi Mallela
 
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
Shane Coughlan
 
Best Practice for LLM Serving in the Cloud
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
 
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
Hassan Abid
 
HYBRIDIZATION OF ALKANES AND ALKENES ...
HYBRIDIZATION OF ALKANES AND ALKENES ...
karishmaduhijod1
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
2nd Sight Lab
 
ElectraSuite_Prsentation(online voting system).pptx
ElectraSuite_Prsentation(online voting system).pptx
mrsinankhan01
 
declaration of Variables and constants.pptx
declaration of Variables and constants.pptx
meemee7378
 
Zoneranker’s Digital marketing solutions
Zoneranker’s Digital marketing solutions
reenashriee
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Simplify Task, Team, and Project Management with Orangescrum Work
Simplify Task, Team, and Project Management with Orangescrum Work
Orangescrum
 
arctitecture application system design os dsa
arctitecture application system design os dsa
za241967
 
University Campus Navigation for All - Peak of Data & AI
University Campus Navigation for All - Peak of Data & AI
Safe Software
 
Key Challenges in Troubleshooting Customer On-Premise Applications
Key Challenges in Troubleshooting Customer On-Premise Applications
Tier1 app
 
Canva Pro Crack Free Download 2025-FREE LATEST
Canva Pro Crack Free Download 2025-FREE LATEST
grete1122g
 

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 &
  • 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
  • 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

  • #7: Try this in C and C++
  • #15: Program : 1_IO.cpp
  • #16: cin&amp;gt;&amp;gt;regno;No Need for format specifier or address of(&amp;) operator
  • #17: Basic information about namespace to be added Namespace.cpp
  • #20: 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; }
  • #21: typecasting.cpp
  • #25: 3_reference.cpp
  • #26: 3_reference.cpp
  • #31: #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; }
  • #34: 4_swapref.cpp
  • #37: #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; }
  • #38: #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; }