Re: Idea
From: Alwyn (dt015a1979_at_mac.com.invalid)
Date: 07/29/04
- Next message: Alwyn: "Re: Idea"
- Previous message: Firewalker: "how to dial no in C/C++"
- In reply to: Val: "Re: Idea"
- Next in thread: Alwyn: "Re: Idea"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 29 Jul 2004 04:42:35 +0100
In article <4107fd31$0$62351$5fc3050@dreader2.news.tiscali.nl>, Val
<valmont_programming@hotmail.com> wrote:
> What I tried to achieve is to encapsulate additional functions like poPq, enterNumber etc. That includes
> showHeap() as well.
> As a form of excercise I want to implement it as a part of a speciliazed std::priority_queue. Just because I
> want to learn how to do it.
Personally, I think the best way to do it is the way you've already
done it; there's really nothing wrong with that at all. However, I
appreciate your desire to learn new techniques.
The following example uses private inheritance: you're implementing
your PQueue *in terms of* std::priority_queue. You are essentially
giving the base class a different interface.
#include <iostream>
#include <vector>
#include <queue>
#include <functional>
class SomeClass
{
public:
int someProperty;
};
bool operator<(const SomeClass& l, const SomeClass& r)
{
return l.someProperty < r.someProperty;
}
class PQueue : private
std::priority_queue<SomeClass, std::vector<SomeClass>,
std::less<SomeClass> >
{
public: //Main methods
void enterNumber();
void popPq();
void showHeap();
void Menu();
public: //Various
enum {Exit, Push, Pop, Top};
};
using namespace std;
void PQueue::Menu()
{
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";
}
}
}
void PQueue::enterNumber()
{
SomeClass SC;
cout << "---> ";
cin >> SC.someProperty;
push(SC);
}
void PQueue::popPq()
{
if(!empty())
{
cout << "Popping top of heap (" <<
top().someProperty << ")" << endl;
pop();
}
else
cout<<"*Empty Heap*\n";
}
void PQueue::showHeap()
{
cout << "Top of the heap is: ";
if(!empty())
cout<< top().someProperty;
else
cout<<"*Empty Heap*";
cout<<"\n";
}
int main()
{
PQueue TestPQueue;
TestPQueue.Menu();
return 0;
}
- Next message: Alwyn: "Re: Idea"
- Previous message: Firewalker: "how to dial no in C/C++"
- In reply to: Val: "Re: Idea"
- Next in thread: Alwyn: "Re: Idea"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|