SlideShare ist ein Scribd-Unternehmen logo
1 von 39
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

Weitere ähnliche Inhalte

Was ist angesagt?

Inheritance in c++ part1
Inheritance in c++ part1Inheritance in c++ part1
Inheritance in c++ part1Mirza Hussain
 
Java 8 - CJ
Java 8 - CJJava 8 - CJ
Java 8 - CJSunil OS
 
Friend function
Friend functionFriend function
Friend functionzindadili
 
JAVA Variables and Operators
JAVA Variables and OperatorsJAVA Variables and Operators
JAVA Variables and OperatorsSunil OS
 
Hierarchical inheritance
Hierarchical inheritanceHierarchical inheritance
Hierarchical inheritanceShrija Madhu
 
Access specifier
Access specifierAccess specifier
Access specifierzindadili
 
Virtual base class
Virtual base classVirtual base class
Virtual base classTech_MX
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented ProgrammingGamindu Udayanga
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File HandlingSunil OS
 
Python part2 v1
Python part2 v1Python part2 v1
Python part2 v1Sunil OS
 
Collection v3
Collection v3Collection v3
Collection v3Sunil OS
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++LPU
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)Ritika Sharma
 

Was ist angesagt? (20)

C++
C++C++
C++
 
Inheritance in c++ part1
Inheritance in c++ part1Inheritance in c++ part1
Inheritance in c++ part1
 
inheritance
inheritanceinheritance
inheritance
 
Java 8 - CJ
Java 8 - CJJava 8 - CJ
Java 8 - CJ
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Friend function
Friend functionFriend function
Friend function
 
PDBC
PDBCPDBC
PDBC
 
JAVA Variables and Operators
JAVA Variables and OperatorsJAVA Variables and Operators
JAVA Variables and Operators
 
Hierarchical inheritance
Hierarchical inheritanceHierarchical inheritance
Hierarchical inheritance
 
Access specifier
Access specifierAccess specifier
Access specifier
 
7.data types in c#
7.data types in c#7.data types in c#
7.data types in c#
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 
JDBC
JDBCJDBC
JDBC
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented Programming
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
 
Python part2 v1
Python part2 v1Python part2 v1
Python part2 v1
 
Class or Object
Class or ObjectClass or Object
Class or Object
 
Collection v3
Collection v3Collection v3
Collection v3
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 

Andere mochten auch

Andere mochten auch (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++
 

Ähnlich wie 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 ProcessingMeghaj 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.pptxssuser3cbb4c
 
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.pdfyamew16788
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxShashiShash2
 
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-ee01083101premrings
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th StudyChris Ohk
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Languagesatvirsandhu9
 
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
 
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 overloadingkinan keshkeh
 

Ähnlich wie 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
 

Mehr von Mohammed Sikander (20)

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
 
Signal
SignalSignal
Signal
 
File management
File managementFile management
File management
 
Java arrays
Java    arraysJava    arrays
Java arrays
 
Java strings
Java   stringsJava   strings
Java strings
 
Java notes 1 - operators control-flow
Java notes   1 - operators control-flowJava notes   1 - operators control-flow
Java notes 1 - operators control-flow
 
Questions typedef and macros
Questions typedef and macrosQuestions typedef and macros
Questions typedef and macros
 
Pointer level 2
Pointer   level 2Pointer   level 2
Pointer level 2
 

Kürzlich hochgeladen

8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...software pro Development
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...kalichargn70th171
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 

Kürzlich hochgeladen (20)

8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 

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

Hinweis der Redaktion

  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; }