CPP Language Basics - Reference
-
Upload
mohammed-sikander -
Category
Software
-
view
39 -
download
2
Transcript of CPP Language Basics - Reference
C++ Language Basics
ObjectivesIn 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 Classes1983 Renamed to C++1998 ANSI C++2003 STL was added2011 C++ 112014 C++142017 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 setif(!(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 / objectsTYPE &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;
10x
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