| | |
Please help me to convert a simple C++ program into an object-oriented one.
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
1. The prototype should read like one of the following
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:
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.
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:
C++ Syntax (Toggle Plain Text)
double& operator[] (unsigned int Index) { if ((Index < 0) || (Index >= Length)) throw 0; // throw some appropriate exception here, instead of int. return Values[Index]; }
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.
•
•
Join Date: Dec 2007
Posts: 10
Reputation:
Solved Threads: 0
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...
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...
•
•
Join Date: Dec 2007
Posts: 10
Reputation:
Solved Threads: 0
Re: Please help me to convert a simple C++ program into an object-oriented one.
0
#23 Apr 16th, 2008
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- ASP and SQL ? (MS SQL)
Other Threads in the C++ Forum
- Previous Thread: Problems in Address book programming
- Next Thread: C++ Help Tortoise and the Hare
Views: 2692 | Replies: 22
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






