title: Converting C++ Procedural Program Into Object Oriented
body: <p>I’ve been told to convert a procedural <a href="http://pasted.co/cb410afe" rel="nofollow noreferrer">C++ program</a> into an object-oriented one, using the principles of polymorphism, encapsulation and inheritance. </p>
  
  <p>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.</p>
  
  <p>I have made a start by deciding to use a class for 'TheFilter' and a class for 'TheData' each with their own 'enterdata()', 'displaydata()' and constructor member functions. </p>
  
  <p>The problem I am having is with part of the <a href="http://pasted.co/0ada8568" rel="nofollow noreferrer">criteria set</a>, "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().</p>
  
  <p>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? </p>
  
  <p>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!</p>
  
  <p>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!</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class TheData 
  {
  public:
      TheData(double* = 0, unsigned long = 0, bool = false); //constructor function
      ~TheData();
  
      void EnterData(TheData&amp; 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&amp; 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;
  }  
  </code></pre>