Re: Idea

From: Alwyn (dt015a1979_at_mac.com.invalid)
Date: 07/29/04


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;
}



Relevant Pages

  • Re: The proper way of function encapsulation.
    ... > Perhaps that's what bothers me. ... what is the PQueue class? ... to manipulate a priority queue that contains SomeClass objects. ... void enterNumber() { ...
    (comp.object)
  • The proper way of function encapsulation.
    ... //Assumes the existence of SomeClass. ... void enterNumber(); ... PQueue TestPQueue; ...
    (comp.object)
  • Re: Idea
    ... My goal is to find the best ways to specialize containers wich holds objects. ... //Assumes the existence of SomeClass. ... void enterNumber(); ... case Top: showHeap(); break; ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Best Practices: always use new() with objects?
    ... rather, as in your example, void somemethod, then you can ... use SomeClass X = Y; for code clarity, if you do stuff in the method ... the lack of the 'ref' keyword should clue in the reader that this is ...
    (microsoft.public.dotnet.languages.csharp)