GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 30-Dec-2007, 09:54
zekesteer zekesteer is offline
New Member
 
Join Date: Dec 2007
Posts: 3
zekesteer is on a distinguished road

Please help me to convert a simple C++ program into an object-oriented one.


Hi all,

I’ve been given told to convert a standard C++ program into an object-oriented one, using the principals of polymorphism, encapsulation and inheritance. Our lecturer covered all three principals in one 45 minute slot. I’m completely stuck and would be grateful for any help.

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 haven’t made much of a start as I’m unsure how to allocate the classes. I’ve allocated one class to the TheFilter structure with its associated data and functions, and another to the TheData structure.

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.

Zeke

Standard C++ program follows:

CPP / C++ / C Code:
// Purpose
// A program to demonstrate the application of a simple digital filter
// 
// Overview
// A sequence of data items and digital filter values need to be entered by the
// user. The application of the filter to the data involves a simple convolution 
// operation. The filtered data are stored separately. 
//
// Example
//  before filtering: 
//   data_in = [0 1 3 6 3 1 0]
//   filter = [-0.5 1 -0.5]
//  after filtering:
//   data_out = [-0.5 -0.5 3 -0.5 -0.5]
//  where
//   data_out[0]=data_in[0]*filter[0]+data_in[1]*filter[1]+data_in[2]*filter[2]
//   data_out[1]=data_in[1]*filter[0]+data_in[2]*filter[1]+data_in[3]*filter[2]
//   data_out[2]=data_in[2]*filter[0]+data_in[3]*filter[1]+data_in[4]*filter[2]
//   data_out[3]=data_in[3]*filter[0]+data_in[4]*filter[1]+data_in[5]*filter[2]
//   data_out[4]=data_in[4]*filter[0]+data_in[5]*filter[1]+data_in[6]*filter[2]
//
// The program checks the following
// 1. The data and filter values must have been entered before the filter is 
//    applied
// 2. The filter is not applied if the number of filter values is greater than
//    the number of input data values
// 3. The data and filter values must have been entered and the filter applied 
//    before the filtered data can be displayed
#include <iostream>
using namespace std;
 
// the data values and the filter
struct TheFilter {
  double* Values;   // the filter values
  unsigned long Length;  // number of filter values
  bool Valid;   // true if the filter values have been Valid by the user
};

struct TheData {
  double* Values;  // holds the data to be filtered
  unsigned long Length;  // number of data values
  bool Valid;   // true if the data values have been Valid by the user
};

// function return values
enum {OK,FILTER_TOO_LONG};

// function prototypes
void EnterData(TheData&);
void EnterFilter(TheFilter&);
int ApplyFilter(TheFilter, TheData, TheData&);
void DisplayData(TheFilter, TheData, TheData);
 
// Control the principal operations of the program
// Arguments: None
// Returns: 0 on completion
int main()
{
  // define the filter and its initial values
  TheFilter Filter = {0,0,false};

  // define the original data and its initial values
  TheData OriginalData = {0,0,false};

  // define the filtered data and its initial values
  TheData FilteredData = {0,0,false};

  char UserInput;

  // loop until the user wishes to exit
  while (1) {
    
    // show the menu of options
    cout << endl;
    cout << "Filter Menu" << endl;
    cout << "-----------" << endl;
    cout << "1. Enter data for filtering" << endl;
    cout << "2. Enter filter values" << endl;
    cout << "3. Apply filter" << endl;
    cout << "4. Display filtered data" << endl;
    cout << "5. Exit from the program" << endl << endl;
    
    // get the user's choice
    cout << "Enter your option: ";
    cin >> UserInput;
    cout << endl;
    
    // act on the user's input
    switch(UserInput) {
      case '1':
        EnterData(OriginalData);
        FilteredData.Valid = false;
        break;

      case '2':
        EnterFilter(Filter);
        FilteredData.Valid = false;
        break;      
 
      case '3':
        if (Filter.Valid == true && OriginalData.Valid == true &&
            FilteredData.Valid == false) {
          if (ApplyFilter(Filter,OriginalData,FilteredData) == FILTER_TOO_LONG) {
             cout << "The filter must not be longer than the data" << endl;
          }
          else {
            FilteredData.Valid = true;
            cout << "Filter applied" << endl;
          }
        }
        break;

      case '4':
        if (Filter.Valid == true && OriginalData.Valid == true &&
            FilteredData.Valid == true) {
          DisplayData(Filter,OriginalData,FilteredData);
        }
	 else {
	    cout << "Data have not yet been filtered" << endl;
	 }
        break;

      case '5':
        delete [] Filter.Values;
        delete [] OriginalData.Values;
        delete [] FilteredData.Values;
        return 0;
        break;

      default:
        cout << "Invalid entry" << endl << endl;
        break;
    }
  }
}

// Allow the user to enter the data to be filtered
// Arguments:
//   (1) the structure containing the input data
// Returns: nothing
// 
void EnterData(TheData& GetData)
{  
  // initialize the data structure that holds the data to be filtered, including getting
  // the number of data values from the user
  delete [] GetData.Values;
  cout << "How many data values do you wish to enter: ";
  cin >> GetData.Length;
  GetData.Valid = true;

  // allocate memory to the data
  GetData.Values = new double[GetData.Length];
  if (GetData.Values == 0) {
    cout << "Unable to allocate sufficient memory" << endl;
    exit(1);
  }

  // obtain all of the data values
  cout << endl;
  cout << "Enter the data values" << endl;
  cout << "---------------------" << endl;
  for (unsigned long CountData = 0; CountData < GetData.Length; CountData++) {
    cout << "Enter value " << CountData+1 << ": ";
    cin >> GetData.Values[CountData];
  }
}

// Allow the user to enter the filter values
// Arguments:
//   (1) the structure of the filter to be defined
// Returns: nothing
// 
void EnterFilter(TheFilter& GetFilter)
{  
  // initialize the data structure that holds the filter, including getting the number of
  // filter values from the user
  delete [] GetFilter.Values;
  cout << "How many data values do you wish to enter: ";
  cin >> GetFilter.Length;
  GetFilter.Valid = true;

  // allocate memory to the filter values
  GetFilter.Values = new double[GetFilter.Length];
  if (GetFilter.Values == 0) {
    cout << "Unable to allocate sufficient memory" << endl;
    exit(1);
  }

  // obtain all of the filter values
  cout << endl;
  cout << "Enter the filter values" << endl;
  cout << "-----------------------" << endl;
  for (unsigned long CountData = 0; CountData < GetFilter.Length; CountData++) {
    cout << "Enter value " << CountData+1 << ": ";
    cin >> GetFilter.Values[CountData];
  }
}

// Apply the filter to the input data and store in the filtered data structure
// Arguments:
//   (1) the structure of the filter to be applied
//   (2) the structure containing the data to be filtered
//   (3) the structure to hold the filtered data
// Returns: OK - if the filter is applied
//          FILTER_TOO_LONG - the filter is longer than the data 
//  
int ApplyFilter(TheFilter Filter, TheData DataIn, TheData& DataOut)
{  
  // return an error if the filter is longer than the data
  if (Filter.Length > DataIn.Length) return FILTER_TOO_LONG;

  // initialize the data structure that holds the filtered data
  delete [] DataOut.Values;
  DataOut.Length = DataIn.Length - Filter.Length + 1;

  // get memory for the filtered data
  DataOut.Values = new double[DataOut.Length];
  if (DataOut.Values == 0) {
    cout << "Unable to allocate sufficient memory" << endl;
    exit(1);
  }

  // apply the filter to the data
  for (unsigned long CountData = 0; CountData < DataOut.Length; CountData++) {
    DataOut.Values[CountData] = 0.0; 
    for (unsigned long CountFilter = 0; CountFilter<Filter.Length; CountFilter++) {
      DataOut.Values[CountData] += DataIn.Values[CountData+CountFilter] *
                                   Filter.Values[CountFilter]; 
    }
  }

  return OK;
}


// Display input data, filter values and output data
// Arguments:
//   (1) the structure of the filter to be applied
//   (2) the structure containing the data to be filtered
//   (3) the structure that holds the filtered data
// Returns: nothing
// 
void DisplayData(TheFilter Filter, TheData DataIn, TheData DataOut)
{  
  // display all of the input data values
  cout << endl;
  cout << "The input data values" << endl;
  cout << "---------------------" << endl;
  cout << "[ ";
  for (unsigned long CountData = 0; CountData < DataIn.Length; CountData++) {
    cout << DataIn.Values[CountData] << " ";
  }
  cout << "]" << endl;
    
  // display all of the filter values
  cout << endl;
  cout << "The filter values" << endl;
  cout << "-----------------" << endl;
  cout << "[ ";
  for (unsigned long CountData = 0; CountData < Filter.Length; CountData++) {
    cout << Filter.Values[CountData] << " ";
  }
  cout << "]" << endl;
    
  // display all of the data output values
  cout << endl;
  cout << "The data output values" << endl;
  cout << "----------------------" << endl;
  cout << "[ ";
  for (unsigned long CountData = 0; CountData < DataOut.Length; CountData++) {
    cout << DataOut.Values[CountData] << " ";
  }
  cout << "]" << endl;
}
Last edited by LuciWiz : 30-Dec-2007 at 12:09. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 02-Jan-2008, 09:10
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Please help me to convert a simple C++ program into an object-oriented one.


CPP / C++ / C Code:
// Filter.h
class Filter
{ public:
    vector<double> Values; // like an array that can grow/shrink and knows its own size
    bool Valid;

    Filter();
    ~Filter();

    // methods of the Filter class    
    void Add( double value );
    void Clear();
};
CPP / C++ / C Code:
// Data.h
#include "Filter.h"

class Data
{ public:
    vector<double> Values; 
    bool Valid;

    Data();
    ~Data();

    // methods of the Data class    
    void Add( double value );
    void Clear();
    Data ApplyFilter( Filter& f );
};
CPP / C++ / C Code:
// Filter.cpp
#include "Filter.h"

Filter::Filter()
{ Valid = false;
}

Filter::~Filter()
{ // Free any memory here
}

void Filter::Add( double value )
{ Values.push_back(value);
}

void Filter::Clear()
{ Values.clear();
}
CPP / C++ / C Code:
// Data.cpp
#include "Data.h"

Data::Data()
{ Valid = false;
}

Data::~Data()
{ // Free any memory here
}

void Data::Add( double value )
{ Values.push_back(value);
}

void Data::Clear()
{ Values.clear();
}

Data Data::ApplyFilter( Filter& f )
{ Data result;

  // apply the filter

  return result;
}
CPP / C++ / C Code:
// main.cpp
#include "Data.h"
#include "Filter.h"

int main()
{ Filter f;
  Data d1,d2;

  f.Add( -0.5 );
  f.Add(  1.0 );
  f.Add( -0.5 );

  d1.Add( 0 );
  d1.Add( 1 );
  d1.Add( 3 );
  d1.Add( 6 );
  d1.Add( 3 );
  d1.Add( 1 );
  d1.Add( 0 );

  d2 = d1.ApplyFilter(f);
}
  #3  
Old 03-Jan-2008, 09:20
zekesteer zekesteer is offline
New Member
 
Join Date: Dec 2007
Posts: 3
zekesteer is on a distinguished road

Re: Please help me to convert a simple C++ program into an object-oriented one.


fakepoo,

A little more help than I was expecting! Thank you so much for your post.

I didn't explain that the task stipulates the following:

1. The program must use an object-oriented approach; it must contain only member functions (other than main() which should do no more than create the first object).

2. All members must be private.

With some minor alterations, your program could easily be changed to accomodate these stipulations. Just one question; for the destructor functions ~Data and ~Filter, how would I free the memory as indicated in the comments?

Once again, thanks for taking the time to provide such a helpul reply.

Zeke
  #4  
Old 03-Jan-2008, 09:38
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Please help me to convert a simple C++ program into an object-oriented one.


Quote:
Originally Posted by zekesteer
Just one question; for the destructor functions ~Data and ~Filter, how would I free the memory as indicated in the comments?
You would only need to delete any memory that you allocated with malloc, calloc, or new. If you use the vector like I suggested, you won't need to delete it because it will delete itself.


Quote:
Originally Posted by zekesteer
1. The program must use an object-oriented approach; it must contain only member functions (other than main() which should do no more than create the first object).
You should probably create another class (I'm thinking something called Controller) that will run everything in it's constructor.


Quote:
Originally Posted by zekesteer
2. All members must be private.
I hope this just means variables and not methods. If it means methods too, you'll have to pass all of the information in the constructor.


Good luck.
~fakepoo
  #5  
Old 03-Jan-2008, 12:01
howtoprogramc howtoprogramc is offline
New Member
 
Join Date: Dec 2007
Posts: 1
howtoprogramc is an unknown quantity at this point

Re: Please help me to convert a simple C++ program into an object-oriented one.


A tutorial on this website might be of help to you

www.pickatutorial.com
  #6  
Old 05-Jan-2008, 11:05
zekesteer zekesteer is offline
New Member
 
Join Date: Dec 2007
Posts: 3
zekesteer is on a distinguished road

Re: Please help me to convert a simple C++ program into an object-oriented one.


Hi howtoprogramc,

Thanks for your post.

I couldn't find a tutorial suited to my particular task. Please could you send me the link?

Zeke
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 11:13
Beginner working with classes Timothy_Bennett C++ Forum 14 05-Apr-2007 23:22
Please Help Me To Build My Calendar!!! suriacute85 Java Forum 0 05-Oct-2006 20:39
A program to convert miles to kilometres Persian_Devil13 C Programming Language 23 03-Feb-2006 23:57

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:35.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.