CPP Language Basics - Reference

39
C++ Language Basics

Transcript of CPP Language Basics - Reference

Page 1: CPP Language Basics - Reference

C++ Language Basics

Page 2: CPP Language Basics - Reference

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

Page 3: CPP Language Basics - Reference

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

Page 4: CPP Language Basics - Reference

C++ is a superset of C language. It supports everything what is there

in C and provides extra features.

Page 5: CPP Language Basics - Reference

Before we start with C++, lets see some C questions

Page 6: CPP Language Basics - Reference

int a ;int a ;int main( ){

printf("a = %d " , a);

}

int a = 5;int a = 10;int main( ){

printf("a = %d " , a);}

Page 7: CPP Language Basics - Reference

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.

Page 8: CPP Language Basics - Reference

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++.

Page 9: CPP Language Basics - Reference

int main( ){

const int x = 5;printf("Enter a number : ");scanf(" %d" , &x);printf("x = %d \n",x);

}

Page 10: CPP Language Basics - Reference

Scanf cannot verify if the argument is const variable or not.

It allows us to modify the value of const variable.

Page 11: CPP Language Basics - Reference

#include <iostream>int main( ){ std::cout << "Hello World";

return 0;}

Page 12: CPP Language Basics - Reference

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

Page 13: CPP Language Basics - Reference

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

Page 14: CPP Language Basics - Reference

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

Page 15: CPP Language Basics - Reference

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.

Page 16: CPP Language Basics - Reference

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

Page 17: CPP Language Basics - Reference

#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);

}

Page 18: CPP Language Basics - Reference

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;

Page 19: CPP Language Basics - Reference

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

}

Page 20: CPP Language Basics - Reference

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

Page 21: CPP Language Basics - Reference

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

}

Page 22: CPP Language Basics - Reference

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

}

Page 23: CPP Language Basics - Reference

The previous two code snippets showed us how easily we can go wrong with pointer.

Page 24: CPP Language Basics - Reference

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

Page 25: CPP Language Basics - Reference

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

Page 26: CPP Language Basics - Reference

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;

Page 27: CPP Language Basics - Reference

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

Page 28: CPP Language Basics - Reference
Page 29: CPP Language Basics - Reference
Page 30: CPP Language Basics - Reference
Page 31: CPP Language Basics - Reference
Page 32: 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

Page 33: 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 Reference

Page 34: CPP Language Basics - 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

Page 35: CPP Language Basics - Reference
Page 36: CPP Language Basics - Reference
Page 37: CPP Language Basics - Reference
Page 38: 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

Page 39: CPP Language Basics - Reference