jeudi 26 mars 2015

how to extract dynamic array data from classes

I am trying to extract an array of data from a class. This is not all the code, but the "class num" takes in an array of data and now I want to extract this array data into my main function. But I am getting some weird numbers as output in the main function. (new to programming)



#include<iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
class measure
{
public:
virtual ~measure(){}
virtual double*extract()=0;
};
class num:public measure
{
protected:
int rows;
double *data;
public:
num(){rows=0; data=0;} //Intialise objects.
~num(){delete[] data;}
double* extract();
};


So far for the extraction part, I have something like this:



double* num::extract()
{
double *temp_arr;
temp_arr = new double [rows];
for (int j = 0; j <rows;j++)
{
temp_arr[j] = data[j];
}
return temp_arr;
}


And in the main, I have something like this:



num a;
double *b;
b = new double [rows];
b = a.extract();
for (int i= 0; i <rows; i++)
{
cout<<b[i]<<endl;
}

Aucun commentaire:

Enregistrer un commentaire