The proper way of function encapsulation.

From: Val (valmont_programming_at_hotmail.com)
Date: 07/27/04


Date: Tue, 27 Jul 2004 04:53:42 +0200

Concerning the code below, wich serves as getting aquinted with the priority queue.
I'll ask my questions after the code:

#include <iostream>
#include <vector>
#include <queue>
#include <functional>
using namespace std;

class SomeClass
{
public:
 int someProperty;
};

//Override original less for any class/struct that holds a "someProperty".
//Assumes operator<() is defined on T.
template<typename T>
class less : binary_function<T, T, bool>
{
public:
 bool operator()(const T& a, const T& b) const
 {
  return a.someProperty < b.someProperty;
 }
};

typedef priority_queue<SomeClass, vector<SomeClass>, less<SomeClass> > PQ;
//For basic function encapsulation demonstrational purpose only!
//Assumes the existence of SomeClass.
//Assumes SomeClass holds a public member "int somePropery".
class PQueue
{
public: //Main methods
 void enterNumber();
 void popPq();
 void showHeap();
 void Menu();
public: //Various
 enum {Exit, Push, Pop, Top};
private: //Members
 PQ MP;
 SomeClass SC;
};

int main()
{
 PQueue TestPQueue;
 TestPQueue.Menu();
 return 0;
}

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()
{
 cout << "---> ";
 cin >> SC.someProperty;
 MP.push(SC);
}

void PQueue::popPq()
{
 if(!MP.empty())
 {
  cout << "Popping top of heap (" <<
   MP.top().someProperty << ")" << endl;
  MP.pop();
 }
 else
  cout<<"*Empty Heap*\n";
}

void PQueue::showHeap()
{
 cout << "Top of the heap is: ";
 if(!MP.empty())
  cout<< MP.top().someProperty;
 else
  cout<<"*Empty Heap*";
 cout<<"\n";
}

It's PQueue what bothers me.
What I wanted to achieve is to encapsulate "push", "pop" and "top" and actually also the "view" (showHeap).
So I made this PQueue class to experiment. But I don't like it. It doesn't feel good. I can't say "it isn't
right" because I am way too much unexperianced for such a remark.
But I don't like it at all.
I *could* have derrived PQueue from "priority_queue<SomeClass, vector<SomeClass>, less<SomeClass> >" (somehow)
to specialize the class but basically my question is:
How do I encapsulate these kind of standard methods? I don't understand Bjarne S. explanation about interface
classses or handle classes so I am lost.
I'll post some code of a linked list implementation with some basic encapsulation in a class. So you'll
understand how I got to this little task:

#include <iostream>
#include <string>
using namespace std;

struct Node
{
        int Item;
        Node *Next;
};

class LinkedList
{
public:
        void AddItem(int itm);
        void ShowItems();
//Dtors, Ctors etc.
public:
        LinkedList() : m_nodeStart(0){ }
        ~LinkedList();
private:
        Node* m_nodeStart;
};

void LinkedList::AddItem(int itm)
{
        Node* p = m_nodeStart;
        m_nodeStart = new Node;
        m_nodeStart->Next = p;
        m_nodeStart->Item = itm;
}

void LinkedList::ShowItems()
{
        for(Node* p = m_nodeStart; p != 0; p = p->Next)
                cout<<p->Item<<" ";
        cout<<endl;
}

LinkedList::~LinkedList()
{
        Node* p;
        while(m_nodeStart != 0)
        {
                p = m_nodeStart;
                m_nodeStart = m_nodeStart->Next;
                delete p;
        }
}

int main ()
{
        LinkedList MyList;
        MyList.AddItem(15);
        MyList.AddItem(30);
        MyList.AddItem(45);
        MyList.ShowItems();

        return 0;
}

See? No biggie here. But how do I this with the priority queue? Or did manage it well already?



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)
  • Re: Idea
    ... > showHeap() as well. ... your PQueue *in terms of* std::priority_queue. ... bool operator<(const SomeClass& l, const SomeClass& r) ... void enterNumber(); ...
    (alt.comp.lang.learn.c-cpp)
  • 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)