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