Write a C++ Program using Class and Object concept.
Function Type :- No Return Type No arguments
Perform the operations on array. Create three functions for
the following operations.
• 1
st Function to take an array.
• 2
nd Function to print an array.
• 3
rd Funciton to sort array elements and print them.
#include <iostream>
template<typename T>
class MyArray
{
private:
T* arr = nullptr;
int size = 0;
public:
void st()
{
std::cout << "Enter size of array : ";
std::cin >> size;
arr = new T[size];
for (int i = 0; i < size; i++)
{
std::cout<<i + 1 << ".Enter value : ";
std::cin >> arr[i];
}
}
void nd()
{
for (int i = 0; i < size; i++)
{
std::cout << arr[i] << " ";
}
std::cout<<std::endl;
}
void rd()
{
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
if (arr[j ] > arr[j+1])
{
std::swap(arr[j], arr[j + 1]);
}
}
}
nd();
}
};
int main()
{
MyArray<int> arr;
arr.st();
std::cout << "Array values : \n";
arr.nd();
std::cout << "Array values after sort : \n";
arr.rd();
system("pause");
return 0;
}
Comments
Leave a comment