Showing posts with label function pointer and virtual function sort. Show all posts
Showing posts with label function pointer and virtual function sort. Show all posts

Monday, May 9, 2022

Write a program in C++ to sort an array of integer using function pointer in descending order and resort this array in ascending order using virtual function.

 #include <iostream>

using namespace std;
class sur
{
    int arr[10], max, size, i, j;

public:
virtual void sort(int arr[], int size)
    {
       
    }
   
};
class sur2:public sur{
    public:
    int max;
    void sort(int arr[],int size){

        for (int i = 0; i < size; i++)
        {
            for (int j = i + 1; j < size; j++)
            {
                if (arr[j] < arr[i])
                {
                    max=arr[i];
                    arr[i]=arr[j];
                    arr[j]=max;
                }
            }
        }
    }
};
void sort(int arr[], int size)
    {
        int max;
        for (int i = 0; i < size; i++)
        {
            for (int j = i + 1; j < size; j++)
            {
                if (arr[j] > arr[i])
                {
                    max=arr[i];
                    arr[i]=arr[j];
                    arr[j]=max;
                }
            }
        }
    }
int main()
{
    int size;
    cout << "enter array size\n";
    cin >> size;

    int arr[size];
    cout << "enter array\n";
    for (int i = 0; i < size; i++)
    {
        cin >> arr[i];
    }
    sur a;
    void (*fr)(int arr[],int size);
fr=sort;
fr(arr,size);
cout<<"\n array after function pointer sort\n";
for (int i = 0; i < size; i++)
{
    cout<<arr[i]<<" ";
}
sur*obj;
sur2 obj2;
obj=&obj2;
obj->sort(arr,size);
cout<<"\nafter virtual sort\n";
for (int i = 0; i < size; i++)
{
    cout<<arr[i]<<" ";
}


    return 0;
}
//refrence for funtoion pointer
/*#include <iostream>  
using namespace std;  
int add(int a , int b)  
{  
    return a+b;  
}  
int main()  
{  
 int (*funcptr)(int,int);  // function pointer declaration  
 funcptr=add; // funcptr is pointing to the add function  
 int sum=funcptr(5,5);  
 std::cout << "value of sum is :" <<sum<< std::endl;  
  return 0;  
}  */

Software scope

 In software engineering, the software scope refers to the boundaries and limitations of a software project. It defines what the software wi...

Popular Posts