i have one problem, function sort and dynamic array , program dont run, error, can you help me, find error and fix it. thanks very much.
Code:
class MyIntArray{
public:
MyIntArray(int N);
~MyIntArray();
int operator [](int i);
MyIntArray &operator=(const MyIntArray &A);
void sort();
private:
int *ptr;
int len;
};
Code:
#include"MyIntArray.h"
#include"iostream"
#include "string"
int N =10;
//using namespace std;
MyIntArray::MyIntArray(int N){ // allocate array of N integers
ptr=new int[N];
}
MyIntArray::~MyIntArray(){
delete ptr;
}
int MyIntArray::operator [](int i){
return ptr[i];
}
MyIntArray &MyIntArray::operator =(const MyIntArray &A){
// len=(int)strlen(ptr.);
//A=new int[N];
for(int i=0;i<N;i++){
A[i]=ptr[i];
}
return (*this);
}
void MyIntArray:Sort(){
int tmp;
for(i=0;i<len;i++){
for(int j=i;j<N;j++){
if(ptr[j]>ptr[j+1]){
tmp=ptr[j];
ptr[j]=ptr[j+1];
ptr[j+1]=tmp;
}
}
}
}
Code:
MyIntArray IArr(5); // allocate array of 5 integers
for (int i=0; i<5 i++)
cin >> IArr[i]; // input value from user
cout << “before sorting:” << endl;
cout << IArr << endl;
MyIntArray IAS(1);
IAS = IArr;
IAS.Sort();
cout << “after sorting:” << endl;
cout << IAS << endl;