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

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

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

 
0
  #21
Jan 9th, 2008
1. The prototype should read like one of the following
int TheData::ApplyFilter( TheFilter &Filter ) (operates on self)
TheData TheData::ApplyFilter( TheFilter &Filter ) (returns new data)

Personally, I would have made ApplyFilter a method of the TheFilter class... but so long as you can justify your use you are fine.

2. The point of overloading operators is to make hard things easy. So, you were using it correctly when you got the error message. The error message is because there is no member field named "values". There is, however, one named "Values". (Don't slap yourself too hard.)

The procedures themselves should verify that the index is valid:
  1. double& operator[] (unsigned int Index) {
  2. if ((Index < 0) || (Index >= Length))
  3. throw 0; // throw some appropriate exception here, instead of int.
  4. return Values[Index];
  5. }
My sample code just throws an int, but you should include <exception> and throw something like std::out_of_range.

3. That is because you are having problems with the default copy constructor, which only makes a shallow copy. Thus, when a temporary copy of your object is deleted, it deletes the original object's data. Then, when you delete the original object, the heap complains.

Make sure to have both a copy constructor and an overloaded assignment operator that makes a deep copy of the data. (This will be useful in your assignment.)

Use Google to learn about shallow and deep copies.

4. Don't hazard. Just do it.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 10
Reputation: zekesteer is an unknown quantity at this point 
Solved Threads: 0
zekesteer zekesteer is offline Offline
Newbie Poster

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

 
0
  #22
Jan 9th, 2008
Hi Duoas,

Thanks for your reply!

1. I think I can see your reasoning; if I make ApplyFilter() a method of the TheFilter class then it need only accept data of class, TheData (i.e. int ApplyFilter(TheData, TheData). Thus, I can reference the objects, OriginalData and FilteredData, individually. Problem solved!

2. Oops... I forgot to mention that I'd made the members lowercase and member functions titlecase to allow me to use, for example, .length and .Length(). As far as I can tell, unable to use the syntax object.values[], which seems strange as the operator [] is overloaded in the public areas of both the TheFilter and TheData classes. Hmm...

3. Thank you for clearing this up for me! I'll resolve this later tonight.

4. I was right! Seems all this talk about OOP is really rubbing off on me...
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 10
Reputation: zekesteer is an unknown quantity at this point 
Solved Threads: 0
zekesteer zekesteer is offline Offline
Newbie Poster

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

 
0
  #23
Apr 16th, 2008
Hi Duoas and Lerner,

Just a quick post to thank you for your help. I received my mark yesterday; 27/30!

Many thanks,

Zeke
Last edited by zekesteer; Apr 16th, 2008 at 1:16 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 2692 | Replies: 22
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC