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

CPP Language Basics - Reference

  • 1.
  • 2.
    Objectives In this chapteryou 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 - FirstRelease 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++ isa superset of C language.  It supports everything what is there in C and provides extra features.
  • 5.
    Before we startwith C++, lets see some C questions
  • 6.
    int a ; inta ; int main( ) { printf("a = %d " , a); } int a = 5; int a = 10; int main( ) { printf("a = %d " , a); }
  • 7.
     An externaldeclaration 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( ) { constint x ; printf("x = %d n",x); } C: It is not compulsory to initialize const; C++: Constants should be initialized in C++.
  • 9.
    int main( ) { constint x = 5; printf("Enter a number : "); scanf(" %d" , &x); printf("x = %d n",x); }
  • 10.
     Scanf cannotverify 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 namespacestd; 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 namespacestd; 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’isan 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 namespacestd; 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 allowsa 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” isa 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 followingtypes 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 previoustwo 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 referenceshould 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 areference 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,intb) { 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,intb) { 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 functioncall 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 shouldalways 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; }