Question: extend the C++ code below and use it as a starting point __________________________________________________________ #include #include using namespace std; // --------- BaseSig class and methods ------------------------------
extend the C++ code below and use it as a starting point
__________________________________________________________
#include
#include
using namespace std;
// --------- BaseSig class and methods ------------------------------
class BaseSig{
private:
// neither derived classes nor other users
// can access private members
protected: // accessible by derived classes, not by other users.
int length;
int *raw_data;
public:
BaseSig(); // default constructor.
BaseSig(int L); // parametric constructor
virtual ~BaseSig(); // virtual destructor
int getLength() { return length; };
int getRawValue(int pos);
static int numObjects; // static, only one member for the entire hierarchy
virtual void printInfo();
};
int BaseSig::numObjects = 0; // initialize static data member
// Base class constructor
BaseSig::BaseSig(){
length = 0;
raw_data = NULL;
numObjects++;
}
// Base class parametric constructor
// Note that the data array is not being initialized (we could read from a file)
BaseSig::BaseSig(int L){
length = L;
raw_data = new int[L];
if(raw_data == NULL)
cerr << "Error in memory allocation";
numObjects++;
}
// Base class destructor
BaseSig::~BaseSig(){
delete raw_data;
cout << "Goodbye, BaseSig." << endl;
}
int BaseSig::getRawValue(int pos) {
if(pos < 0) // invalid index
return(raw_data[0]);
else if(pos >= length) // invalid index
return(raw_data[length-1]);
else
return(raw_data[pos]);
}
void BaseSig::printInfo() {
cout << " Length: " << length << endl;
}
// ------------------------------------------------------------------
// --------- ExtendSig class and methods ----------------------------
class ExtendSig : public BaseSig{ // ExtendSig is derived from class BaseSig
//BaseSig is a public base class
private:
double average; // add new data members
double *data;
public:
ExtendSig(int L); //derived classes need a new constructor
~ExtendSig();
// define new member functions
double getValue(int pos);
int setValue(int pos, double val);
double getAverage();
// redefine member function. Virtual keyword not needed
void printInfo(); // new standard: explicit "override" keyword can be used
};
// Derived class constructor. Note how the Base constructor is called.
ExtendSig::ExtendSig(int L) : BaseSig(L) {
data = new double[L];
if(data == NULL)
cerr << "Error in memory allocation";
else{
for(int i = 0; i < L; i++)
data[i] = (double)raw_data[i];
average = getAverage();
}
}
// Derived class destructor
ExtendSig::~ExtendSig() {
delete data;
cout << "Goodbye, ExtendSig." << endl;
}
double ExtendSig::getValue(int pos) {
if(pos < 0) // invalid index
return(data[0]);
else if(pos >= length) // invalid index
return(data[length-1]);
else
return(data[pos]);
}
int ExtendSig::setValue(int pos, double val) {
if((pos < 0) || (pos >= length))
return(-1); // invalid index
else {
data[pos] = val;
average = getAverage();
return(0); // success
}
}
double ExtendSig::getAverage() {
if(length == 0)
return(0.0);
else {
double temp = 0.0;
for(int i = 0; i < length; i++)
temp += data[i];
return(temp/(double)length);
}
}
// Redefined printInfo function for derived class
void ExtendSig::printInfo() {
cout << " Length: " << length << endl
<< "Average: " << average << endl;
}
______________________________________________
Modify and/or add code to the BaseSig class to read data in from a file . Dont worry about command line arguments or constructors that take in a string with the name of the file or the file number. Assume a default text file to read from.
Code up a derived ProcessedSignal class (derived from BaseSig). This class should have the data members and methods that class ExtendSig has, and the following additional ones:
Maximum value data member (private)
Minimum value data member (private)
Methods to get the maximum and minimum values (public). Methods like these are called getters.
Normalization method (public)
Redefinition of the printInfo method, which should display the data members: length, average, maximum value, minimum value.
Please help and comment the code so I can understand it !
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
