Saturday, 4 February 2023

Defining functions inside class and demonstration (how to use it in C++).....(eg. input, output, merge, linear sort, bubble sort, sequential sort, binary sort)

 

#include<iostream>   //put <iostream.h> to compile in TurboC++

#include<conio.h>


using namespace std;  //this (namespace std) only work in modern compiler not in Turb0C++


class Dragon

{ /*this is a class named as Dragon which has following functions

these functions can be used anywhere by using the class name within Same Script

or in Same Directory.

Note: To use this in any other directory you need to import this file*/

int *a;

int n;

public:

Dragon(int t)    // constructor

{

n=t;

a=new int[n];

}

void getdata();      //function to take input

void putdata();      //function to print output data

void mergobj(Dragon &,Dragon &);     // function merge two objects

void lsort();        //function of linear sorting

void bubsort();      //function of bubble sorting

void seqser();       //function of sequential search

void biser();        //function of binary search

};


void Dragon::getdata()     //Defining the Function followed by Class name

{

cout<<"\n enter "<<n<<" values: ";

for(int i=0;i<n;i++)

cin>>a[i];

}


void Dragon::putdata()     //Defining the Function followed by Class name

{

cout<<"\n Puting "<<n<<" Values: ";

for(int i=0;i<n;i++)

cout<<" "<<a[i];

}


void Dragon::mergobj(Dragon &t1,Dragon &t2)//Defining the Function followed by Class name

{

int i;

for(i=0;i<t1.n;i++)

a[i]=t1.a[i];

for(i=0;i<t2.n;i++)

a[t1.n+i]=t2.a[i];

}


void Dragon::lsort()     //Defining the Function followed by Class name

{

int i,j,t;

for(i=0;i<n-1;i++)

{

for(j=i+1;j<n;j++)

{

if(*(a+i)>*(a+j))

{

t=*(a+i);

*(a+i)=*(a+j);

*(a+j)=t;

}

}

}

}


void Dragon::bubsort()     //Defining the Function followed by Class name

{

int i,j,t;

for(i=0;i<n-1;i++)

{

for(j=0;j<n-i-1;j++)

{

if(a[j]>a[j+1])

{

t=a[j];

a[j]=a[j+1];

a[j+1]=t;

}

}

}

}


void Dragon::seqser()     //Defining the Function followed by Class name

{

int num,i;

cout<<"\n enter any number to search: ";

cin>>num;

putdata();

for(i=0;i<n;i++)

if(*(a+i)==num)

break;

if(i==n)

{

cout<<"\n that's a wrong number";

return;

}

cout<<"\n your number "<<*(a+i)<<" is in this the array on psition: "

<<i+1;

}


void Dragon::biser()     //Defining the Function followed by Class name

{

int num;

cout<<"\n enter any number to search: ";

cin>>num;

bubsort();

putdata();

int i=n/2,j=0,k=n-1;

while(j<k)

{

if(*(a+i)==num)

{

cout<<"\n found your number "<<a[i]<<"\n on position "<<i+1;

return;

}

if(*(a+i)>num)

k=i-1;

else

j=i+1;

i=(j+k)/2;


}

if(j==k)

cout<<"\n Wrong number";

}


int main() //This is Main function

{

int t1,t2;

//clrscr();

cout<<"\n enter size of the object 1: ";

cin>>t1;

Dragon obj1(t1);

cout<<"\n enter size of object 2: ";

cin>>t2;

Dragon obj2(t2);

Dragon obj3(t1+t2);

obj1.getdata();

obj2.getdata();

obj3.mergobj(obj1,obj2);

cout<<"\n details of obj 1";

obj1.putdata();

cout<<"\n details of obj 2";

obj2.putdata();

cout<<"\n after obj 1, obj 2 the details of obj 3: ";

obj3.putdata();

obj3.lsort();

cout<<"\n details of ob3 after sorting";

obj3.putdata();

obj3.bubsort();

cout<<"\n after sorting in bubble method obj3 is";

obj3.putdata();

cout<<"\nFor Object1 1:";

obj1.seqser();

    cout<<"\nFor Object1 2:";

obj2.biser();

cout<<"\nFor Object1 3 (which is merged from obj1 and obj2):";

obj3.biser();

//getch();

return 0;

}


No comments:

Post a Comment