Hi all,
I’ve been told to convert a procedural C++ program into an object-oriented one, using the principals of polymorphism, encapsulation and inheritance.
The standard C++ program accepts data values and filter values from the user. It then multiplies these values together using a simple algorithm and prints the output to the screen. The standard program is shown below.
I have made a start by deciding to use a class for 'TheFilter' and a class for 'TheData' each with there own 'enterdata', 'displaydata()' and constructor member functions.
The problem I am having is with part of the criteria set, "main() should do no more than create the first object", I am having difficulty getting my head around this as all examples of OOP code I have ever seen generate objects within main().
The only current solution I could come up with to obey this is to create a 3rd class (FilteredData - see below) which would instantiate the other classes objects 'OriginalData', 'Filter' and 'FilteredData' upon being instantiated itself. However this is troubling me as would it not take away from the concept of encapsulation as it would result in having a class with members which are other classes objects?
If anyone has any suggestions at all into how this could be avoided and best obey the principles of encapsulation I would be very grateful!
I hate to admit defeat, but I’ve never programmed using an object-oriented approach and I’ve only been studying C++ for a couple of months. Please help!
Thanks in advance.
eelhonk
#include <iostream>
using namespace std;
class TheData
{
public:
TheData(double* = 0, unsigned long = 0, bool = false); // constructor function
~TheData();
void EnterData(TheData& OriginalData);
void DisplayData(TheData OriginalData, TheData FilteredData) const;
private:
double* Values;
unsigned long Length;
bool Valid;
}
class TheFilter
{
public:
TheFilter(double* = 0, unsigned long = 0, bool = false); // constructor function
~TheFilter();
void EnterData(TheData& OriginalData);
void DisplayData(TheData OriginalData, TheData FilteredData) const;
int ApplyFilter();
private:
double* Values;
unsigned long Length;
bool Valid;
}
class
{
public:
FilteredData(); // constructor function that somehow instantiates an object for the filter and the data???
void DisplayData();
private:
TheData data
TheFilter filter
double * filteredData
}
int main()
{
FilteredData Object1;
}