Blog

Class Conversions in C++

Use of C++ data types involves the implicit applications of type conversion rules. If you use an int variable where the compiler expects a long variable, for example, the compiler invokes one of the type conversion rules to convert the original integer value to the new long format. Such conversions already exists for all pairs […]

Conversion Functions in C++

From Basic to User-Defined To convert from a basic type to a user-defined type, we use a constructor with one argument. These are sometimes called conversion constructors. Example   class  A   {             int  m;       public :               A( )               {                           m=0;               }              A ( int x)               […]

The typeid operator in C++

The typeid operator supports the new C++ runtime type information feature. It’s possible to find out information about an object’s class. It’s supposed to be a certain type of object, but you want to check it to be sure. You can obtain information about the types of unknown objects, such as its class name, using the typeid […]

Casts in C++ (static_cast)

There are four new casting operators. C++ addresses the shortcomings of C-style casts by introducing four new cast operators, static_cast, const_cast, dynamic_cast, and reinterpret_cast. Each one returns an object converted according to the rules of the operator. They use this syntax : cast_operator  <type>  (object) The cast_operator is any one of the four casting operators. […]

Casts in C++ (const_cast)

const_cast is used to cast away the constness of an expression. By using a const_cast, you emphasize that the only thing you want to change through the cast is the constness of something.  This meaning is enforced by compilers. If you try to employ const_cast for anything other than modifying the constness of an expression, […]

Casts in C++ (dynamic_cast)

The dynamic_cast operator casts a base class reference or pointer to a derived class reference or pointer. dynamic_cast is used to perform safe casts down or across an inheritance hierarchy . You can use it only when the base class has virtual functions. Example     class  A  {…};     class B : public A {…}; […]

C++ program to implement Binary Search

This tutorial explains how Binary Search works to find an element’s position in a sorted array. Besides this, you will come to know how to create a simple C++ program to implement binary search. #include<iostream> using namespace std; int binarysearch(int *a,int key,int n); int main( ) { int *arr,n,key; cout<<“Enter how many elements :”; cin>>n; […]

C++ program to implement Linear Search

#include<iostream> using namespace std; int linearsearch(int *array,int key,int n); int main() { int *arr,n,key; cout<<“Enter how many elements :”; cin>>n; arr=new int[n]; cout<<endl<<“Enter elements : “; for(int i=0;i<n;i++) cin>>arr[i]; cout<<endl<<“Enter the element to search : “; cin>>key; int r=linearsearch(arr,key,n); if(r) cout<<endl<<“element found at”<< r+1<<“position”; else cout<<endl<<“Element not found”; return 0; } int linearsearch(int *arr,int key,int […]

C++ program to implement Insertion Sort

#include<iostream> using namespace std; void Insertionsort(int *,int); int main() { int n; int a[20]; cout<<endl<<“Enter how many elements :”; cin >> n; for(int i=0;i<n;i++) cin>>a[i]; Insertionsort(a,n); for(i=0;i<n;i++) cout<<endl<<a[i]; return 0; }; void Insertionsort(int *arr,int n) { int i, j, key; for(j = 1; j < n; j++) { key = arr[j]; for(i = j – […]

C++ program to implement Insertion Sort

#include<iostream> using namespace std; void Insertionsort(int *,int); int main() { int n; int a[20]; cout<<endl<<“Enter how many elements :”; cin >> n; for(int i=0;i<n;i++) cin>>a[i]; Insertionsort(a,n); for(i=0;i<n;i++) cout<<endl<<a[i]; return 0; }; void Insertionsort(int *arr,int n) { int i, j, key; for(j = 1; j < n; j++) { key = arr[j]; for(i = j – […]