Re: The proper way of function encapsulation.
From: Daniel T. (postmaster_at_eathlink.net)
Date: 07/27/04
- Next message: Bjorn Reese: "Re: We might as well shut down comp.object"
- Previous message: H. S. Lahman: "Re: new bie"
- In reply to: Val: "Re: The proper way of function encapsulation."
- Next in thread: Val: "Re: The proper way of function encapsulation."
- Reply: Val: "Re: The proper way of function encapsulation."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 27 Jul 2004 19:01:42 GMT
"Val" <valmont_programming@hotmail.com> wrote:
> Basically I find that the class PQueue assumes too much too. I'd like to
> generilize the class a bit if
> possible as well.
> Perhaps that's what bothers me.
I expect that this is what is bothering you. PQueue only works on
priority_queues that contain SomeClass objects...
First, what is the PQueue class? It is a UI designed to allow the user
to manipulate a priority queue that contains SomeClass objects. Let's
generalize that a bit and make it a UI class designed to allow the user
to manipulate anything that has the approprate identifiers. Notice that
in order for the below to work, we have to remove all knowledge about
the interface of SomeClass from PQueue. SomeClass would need an op<<,
and op>> defined for it.
The PQueue class below will now work with any class that has value_type,
push(const value_type&), pop(), top() and empty() defined for it. Those
are still pretty strong requirements but they are more loose than the
origional PQueue. Also, this makes it clear that PQueue's job is to act
as a user interface for the queue type class that it contains.
// include list removed, add it back in
using namespace std;
// all information about SomeClass is now grouped between here
// and the 'end SomeClass' comment.
class SomeClass {
public:
int someProperty;
};
bool operator<(const SomeClass& lhs, const SomeClass& rhs) {
return lhs.someProperty < rhs.someProperty;
}
ostream& operator<<(ostream& os, const SomeClass& sc) {
os << sc.someProperty;
return os;
}
istream& operator>>(istream& is, SomeClass& sc) {
is >> sc.someProperty;
return is;
}
// end SomeClass
// For basic function encapsulation demonstrational purpose only!
// No longer assumes the existence of SomeClass.
// No longer assumes SomeClass holds a public member "int somePropery".
// Assumes 'T' is a queue type object that holds objects that
// can be initialized by istreams and displayed by ostreams.
template < typename T >
class PQueue
{
public: //Main methods
void enterNumber() {
cout << "---> ";
typename T::value_type SC;
// note this is no longer a member-variable,
// it is only used in this one function
cin >> SC;
MP.push(SC);
}
void popPq() {
if(!MP.empty()) {
cout << "Popping top of heap (" <<
MP.top() << ")" << endl;
MP.pop();
}
else
cout<<"*Empty Heap*\n";
}
void showHeap()
{
cout << "Top of the heap is: ";
if(!MP.empty())
cout<< MP.top();
else
cout<<"*Empty Heap*";
cout<<"\n";
}
void Menu() {
enum {Exit, Push, Pop, Top};
// no longer defined at the class level,
// it's only used in this one function
int input=1;
while (input != 0)
{
cout << "\nEnter a Selection:\n";
cout << "0) Exit\n";
cout << "1) Push a Number\n";
cout << "2) Pop the heap\n";
cout << "3) Show top of heap\n";
cin >> input;
switch(input)
{
case Exit: cout << "Exited.\n"; break;
case Push: enterNumber(); break;
case Pop: popPq(); break;
case Top: showHeap(); break;
default: cout << "ERROR, reason: Incorrect Entry\n";
}
}
}
private: //Members
T MP;
};
int main()
{
typedef priority_queue< SomeClass > PQ;
PQueue<PQ> TestPQueue;
TestPQueue.Menu();
return 0;
}
- Next message: Bjorn Reese: "Re: We might as well shut down comp.object"
- Previous message: H. S. Lahman: "Re: new bie"
- In reply to: Val: "Re: The proper way of function encapsulation."
- Next in thread: Val: "Re: The proper way of function encapsulation."
- Reply: Val: "Re: The proper way of function encapsulation."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|