My Blog List

Saturday, December 21, 2019

Function overloding in cpp.

 Function overloading in cpp.
       In this tutorial we learn how to overload the function in c++.
Firstly,

 What is mean by function overloading?

Ans:Function overloading is the feature of cpp language.It means that the two or more function having same name.It can be one of the example of Polymorphism.The function name is same but the return type and parameter given to the function is different.

Syntax:
     function-return-type function name(parameter)
    declare another function as same name but different parameter
 
For Example:
      suppose we have a function for calculate addition of two number and three number.
     int add(int num1,num2);
and another is,
     int add(int num1,int num2 int num3);

Here following code shows a area function overloading concept to calculate the different area for different shape. 

#include<iostream.h>
#include<conio.h>
int area(int s);
int area(int l,int b);
double area(double bs,double hg);
double area(double r);
int main()
{
    clrscr();
    cout<<"\nArea of square="<<area(5)<<endl;
    cout<<"\nArea of Rectangle="<<area(10,5)<<endl;
    cout<<"\nArea of Triangle="<<area(4.0,10.0)<<endl;
    cout<<"\nArea of circle="<<area(6.0);
    getch();
    return(0);
}
int area(int s)
{
    return(s*s);
}
int area(int l,int b)
{
    return(l*b);
}
double area(double bs,double hg)
{
    return(bs*hg/2);
}
double area(double r)
{
    return(3.14*r*r);
}


OUTPUT:



Monday, December 16, 2019

Reverse number using cpp.

Reverse number using cpp.

        In this tutorial we learn how to print reverse number of any digit using cpp programming language.
A number contained any digit then by following technique we convert its into reverse order.
        suppose we take a 3 digit integer number and we want to print its into the reverse order then,the number is mod,add reminder and divide operation until number is 0.If number reach to 0 then terminate the execution.

example:

      number is 123
step 1:
       //here we taking a mod of number i.e 3
       mod=123%10
step 2:
       //here we add the reminder into mod that multiplied by 10
       reminder=reminder*10+mod
step 3:
       //finally number dividation.
       number=number/10

code:

#include<iostream.h>
#include<conio.h>
void main()
{
    int num,mod,reminder=0,i;
    clrscr();
    cout<<"\nEnter any 3 digit number:";
    cin>>num;
    while(num!=0)
    {
        mod=num%10;
        reminder=reminder*10+mod;
        num=num/10;
    }
    cout<<"\nReverse number:"<<reminder;
    getch();
}


Output:




You may also like:
Dynamic Memory Management in cpp.
Manipulators in cpp. 
This is the program constructor overloding in cpp 

Sunday, December 8, 2019

Dynamic Memory Management in CPP

Dynamic Memory Management in cpp.

In this tutorial we learn Dynamic Memory Management operator in cpp.This operator use to real time memory of the variable.Memory not allocated statically.This operator use only for Memory allocation and deallocation.

1)New :
      This operator is use to newly allocate memory of any data type.

syntax:
    pointer_variable=new data-type;

Here we use pointer-variable to allocate memory.new is the keyword and followed that data-type of data to store.The data type could be use any built-in data type.It also int user defined data type like structure and class.

Example:
        p=new student;

2)delete
        This operator use to deallocate the memory.It free up the memory that as already allocate.

syntax:
       delete variable-name;

This syntax use to delete the allocate memory.

example:
     delete p;

here p is the object of class when this statement is executed then the memory for this object free up.

code: 
    #include<iostream.h>
    #include<conio.h>
    class student
    {
         char name[20];
         int rno;
         float avg;
         public:
               void getdata();
               void putdata();
    };
    void student::getdata()
    {
         cout<<"\nEnter the name of student:";
         cin>>name;
         cout<<"\nEnter the roll number of student:";
         cin>>rno;
         cout<<"\nEnter average of student:";
         cin>>avg;
    }
    void student::putdata()
    {
        cout<<"\n"<<name<<"\t"<<rno<<"\t"<<avg;
    }
    int main()
    {
        int n,i;
        student *p,*q;
        clrscr();
        cout<<"\nEnter number of student:";
        cin>>n;
        p=new student[n];
        q=p;
        for(i=1;i<=n;i++)
        {
            cout<<"\nEnter "<<i<<" Student information";
            p->getdata();
            p++;
        }
        p=q;
        cout<<"\nStudent information:";
        cout<<"\nName\tRoll no\tAverage";
        for(i=1;i<=n;i++)
        {
            p->putdata();
            p++;
        }
        delete p;
        getch();
        return(0);
  } 


OUTPUT:




 


 

 
 

Manipulators in cpp

Manipulators in cpp.

In this tutorial we learn Manipulators in cpp.These are the function that helps to modify the input/output stream.It does not change any value or data of the variable but it only modify the data as programmer wants.

Types of Manipulators:

1)Manipulator without argument:
  • endl:This manipulator defined in ostream.It used to instead of using \n.It is use to enter the new line.
  • ws:This manipulator use to ignore the all whitespaces in the line.It is defined in istream.
  • ends:It includes the null character into the ostream.It is belonging in ostream.
  • flush:It is also in ostream.It flushes output stream in the line.
 2)Manipulators with Arguments: 

  • setw (val): It is used to sets the field width in output stream.
  • setfill (x): It is used to fill the character ‘x’ on output stream.
  • setprecision (val): It sets val as the new value for the precision of floating-point values. 
code:

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
int main()
{
    int m1,m2;
    double f=3.14159;
    clrscr();
    cout<<"\nEnter two number:";
    cin>>m1>>m2;
    cout<<"\nDisplay the number:";
    cout<<"\nN1:="<<m1;
    cout<<"\nN2:="<<m2;
    cout<<"\nN1="<<setw(8)<<m1<<endl;
    cout<<"\nN2="<<setw(5)<<m2<<endl;
    cout<<setfill('x')<<setw(10);
    cout<<77<<endl;
    cout<<setprecision(4)<<f;
    getch();
    return(0);


OUTPUT:

 

Friday, December 6, 2019

Binary search using cpp.

Binary Search using cpp.

In this tutorial we learn how to search element from given array.This technique is best technique for searching element in large amount of data is given.This techniques is based on the Divide and Conquer.But main condition for this technique is that the data given input is sorted.This approach divide the array in two part and then search the element where the small by middle element or greater.


Code:


#include<iostream.h>
#include<conio.h>
void main()
{
    int n,i,arr[50],search,first,last,middle;
    clrscr();
    cout<<"\nEnter total number of element:";
    cin>>n;
    cout<<"\nEnter the element:";
    for(i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    cout<<"\nEnter searchable element:";
    cin>>search;
    first=0;
    last=n-1;
    middle=(first+last)/2;
    while(first<=last)
    {
        if(arr[middle]<search)
        {
            first=middle+1;
        }
        else if(arr[middle]==search)
        {
            cout<<search<<" found at position:"<<middle+1<<"\n";
            break;
        }
        else
        {
            last=middle-1;
        }
        middle=(first+last)/2;
    }
    if(first>last)
    {
        cout<<"\nElement not found:";
    }
    getch();
}





OUTPUT:



for video demonstration follow:

https://youtu.be/qFpjuqX1gAQ

Tuesday, December 3, 2019

linear search using cpp.

Linear search using cpp.

In this tutorial we learn how to search element in linear searching technology.Linear search is a technique searching element in linear order.This technique checks each element of the list until a match is found.

Complexity:

worst complexity: O(n)
Average complexity: O(n)
Space complexity: O(1)

Code:

#include<iostream.h>
#include<conio.h>
void main()
{
    int arr[10],i,num,n,c=0,pos;
    clrscr();
    cout<<"\nEnter the size of array:";
    cin>>n;
    cout<<"\nEnter the element:";
    for(i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    cout<<"\nEnter searchable number:";
    cin>>num;
    for(i=0;i<n;i++)
    {
        if(arr[i]==num)
        {
            c=1;
            pos=i+1;
            break;
        }

    }
    if(c==0)
    {
        cout<<"\nNumber is not found;";
    }
    else
    {
        cout<<num<<"\nFound at position"<<pos;
    }
    getch();
}


OUTPUT:



 

Multiple Inheritance in C++.

     In this tutorial Multiple Inheritance in c++. On the basis of Inheritance concept the another type of inheritance is the Multiple I...