|
I am a student TRYING very hard to learn C++! However, I am having a hard time understanding it. My teacher gave us a lab to derive a queue class from a list class using private inheritance. I have tried and tried to make my code work! I am just not having any luck. If there is anyone that can help I can send you the code that I have and maybe you can tell me what I have done wrong.
Or I will attach everything here! Following is Node.h, Node.cpp, Queue.h, Queue.cpp, List.h, List.cpp and the driver.cpp (driver program).
Node.h
//node header
#ifndef NODE_H
#define NODE_H
class Node
{
Node * next;
int data;
int CompareTo(Node &);
Node() {data=0; next = 0;}
};
#endif
Node.cpp
#include "node.h"
int Node::CompareTo(Node& n)
{
if(data < n.data)
{
return -1;
}
else if(data == n.data)
{
return 0;
}
else
return 1;
}
List.h
#ifndef LIST_H
#define LIST_H
#include "node.h"
class Queue;
class List
{
private:
Node *front;
Node* pos;
Node * FindNode(Node*, Node*);
public:
List();
List(List&);
virtual ~List();
bool IsFull();
bool IsEmpty();
void AddAtFront(Node &);
void AddAtRear(Node &);
void DeleteItem();
void Reset();
void GetNext(Node &);
void InsertBefore(Node &);
void InsertInOrder(Node &);
void FindItem(Node &, bool &);
bool EndOfList();
List& operator = (List&);
void Print();
};
#endif
List.cpp
#include "list.h"
#include "node.h"
#include <iostream>
#include <new>
#include <stdexcept>
using namespace std;
List::List()
{
pos=0;
front=0;
}
List::~List()
{
cout << "destructor called " << endl;
Node * temp;
while(front != 0)
{
temp=front->next;
cout << front->data << " ";
delete front;
front = temp;
}
cout << endl;
}
List& List::operator =(List & L)
{
if(this == &L)
return *this;
Node n;
front =0;
pos =0;
Node * temp =0;
Node * start =;
start = L.front;
while(start !=temp)
{
temp=FindNode(start,temp);
if(temp==0)
{
break;
}
AddAtFront(*temp);
}
return *this;
}
Node * List::FindNode(Node * start, Node * end)
{
Node * temp=start;
Node * test;
while(temp != end)
{
test=temp->next;
if(test == end)
{
return temp;
}
else
{
temp = test;
}
}
return end;
}
List::List(List & L)
{
Node n;
front =0;
pos = 0;
Node * temp=0;
Node * start;
start = L.front;
while(start != temp)
{
temp=FindNode(start, temp);
if(temp ==0)
{
return;
}
AddAtFront(*temp);
}
}
bool List::IsFull()
{
try
{
Node * temp = new Node();
delete temp;
return false;
}
catch (bad_alloc e)
{
return true;
}
return false;
}
bool List::IsEmpty()
{
return front ==0;
}
bool List::EndOfList()
{
if(pos == NULL)
return false;
if(pos->next==NULL)
return true;
else
return false;
}
void List::GetNext(Node & v)
{
if(IsEmpty())
{
throw "empty list";
}
if(pos==NULL)
{
pos=front;
v=*pos;
return;
}
pos=pos->next;
if(pos==NULL)
{
pos=front;
}
v=*pos;
return;
}
void List::Reset()
{
pos=NULL;
}
void List::AddAtFront(Node & v)
{
Node * nPtr;
if(!IsFull())
{
nPtr = new Node();
nPtr->next=front;
nPtr->data=v.data;
front=nPtr;
}
}
void List::AddAtRear(Node& v)
{
Node * nPtr;
if(!IsFull())
{
if(front==0)
{
AddAtFront(v);
return;
}
else
{
Node * temp;
temp=FindNode(front, 0);
if(temp==0)
{
return;
}
nPtr= new Node();
temp->next=nPtr;
nPtr->data=v.data;
}
}
}
void List::FindItem(Node &v, bool & found)
{
found=false;
if(IsEmpty())
{
return;
}
Node n;
Reset();
while(!EndOfList())
{
GetNext(n);
if(n.CompareTo(v)==0)
{
found=true;
break;
}
}
return;
}
void List::DeleteItem()
{
Node * tempPtr=0;
if(front==0)
{
return;
}
if(pos==0)
{
pos=front;
}
if(pos==front)
{
front=front->next;
delete pos;
Reset();
}
else
{
tempPtr=front;
while(tempPtr !=NULL)
{
if(tempPtr->next==pos)
{
tempPtr->next=pos->next;
delete pos;
Reset();
break;
}
else
{
tempPtr=tempPtr->next;
}
}
}
}
void List::InsertInOrder(Node &v)
{
if(front==0)
{
AddAtFront(v);
return;
}
else
{
Node vv;
Node & oldPos = pos;
Reset();
while(!EndOfList())
{
GetNext(vv);
if(v.CompareTo(vv) <= 0)
{
InsertBefore(v);
cout << " b " << v.data << " ";
pos=oldPos;
return;
}
}
AddAtRear(v);
pos=oldPos;
}
}
void List::InsertBefore(Node &v)
{
if(front==0 || pos==0 || pos==front)
{
AddAtFront(v);
return;
}
else
{
Node * temp=FindNode(front, pos);
if(temp==0)
{
throw "bad return from FindNode in InsertBefore";
}
Node * nPtr= new Node();
nPtr->data = v.data;
nPtr->next=pos;
temp->next=nPtr;
}
}
void List::Print()
{
Node * temp=FindNode(front, 0);
cout << "end value= " << temp->data << endl;
}
Queue.h
#include "list.h"
class Queue : private List
{
public:
Queue(); {}
Queue(Queue &);
~Queue() {};
bool IsFull() {return List::IsFull();}
bool IsEmpty() {return List::IsEmpty();}
void Enqueue(Node) {AddAtFront(v);}
void Dequeue(Node &) {Reset(); GetNext(v); DeleteItem();}
};
Queue.cpp
#include "queue.h"
#include "list.h"
Queue::Queue(Queue & s):List()
{
Node v;
Reset();
s.Reset();
while(!s.EndOfList())
{
s.GetNext(v);
AddAtRear(v);
}
}
Driver program
#include "list.h"
#include "queue.h"
#include "node.h"
//using namespace std;
void main()
{
Queue q;
Node n;
for(int i=0; i<20; i++)
{
n.data=i;
q.Enqueue(n);
}
cout << " finished adding data " << endl;
cout << " dequeuing data " << endl;
for(int j=0; j < 10; j++)
{
q.Dequeue(n);
cout << n.data << endl;
}
cout << endl;
Queue qq(q);
while(!q.IsEmpty())
{
qq.Dequeue(n);
count << n.data << endl;
}
cout << endl;
}
Thanks for helping me~
|
|
|
|
|
I am definitely missing something here, and I'm hoping that someone can provide an explanation about this question around XP themes. - I am using MFC, by the way.
In a nutshell, I guess I just don't really understand what all of this is doing. I know that you load up the dll, and then start calling functions like drawthemebackground and things like that. But - how is this different than the standard functionality provided in MFC?
Say I have a regular CButton on a form. If I run this on w2k or xp classic mode, it looks like the regular button that we all know and love. If I turn on a theme in XP, like the default one with the blue titlebar and rounded edged buttons, the button automatically looks like what I'd expectn in XP, with the rounded edged button, different color text when you hover, etc...
So - obviously I am missing something, but why go through all of this dll loading, calling functions, etc...when it appears that MFC is already handling this stuff? What is the benefit?
Confusedly yours,
Marcus Spitzmiller
"Why must life be so hard? Why must I fail at every attempt at masonry?" - Homer
|
|
|
|
|
Ut isn't MFC that is handling XP Themes but the Manifest file or .RC entry you add that tells Windows to use XP Themes.
I haven't used any of the drawthemexxx functions yet, but my understanding is that if you are using any of your own owner draw controls and you want them to look like XP controls then you use this stuff. Hope that helps.
Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"
|
|
|
|
|
That was so rude of me....I forgot to reply.
Thanks very much for the answer - that makes sense, and makes me feel a whole lot better
Marcus Spitzmiller
"Why must life be so hard? Why must I fail at every attempt at masonry?" - Homer
|
|
|
|
|
I just got a IBM Assambly Language book, and I'm reading it now.
However, the book does not talk about how to compile the Assambly.
Can anyone write a very simple assambly inside C file, so I can
compile it in Visual Studio? If possible, can you also provide
C++ version?
Thanks.
-----------------------------
C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".
|
|
|
|
|
Using the __asm keyword, as example:
__asm
{
movl eax, 123
xorl ebx, ebx
}
Or, you can put __asm on each assembly instruction:
__asm add esi, 0x5
__asm mov push ecx
Regards,
Michael
|
|
|
|
|
how to know if computer is connected to the internet programmatically?
includeh10
|
|
|
|
|
|
i think this api will help --->InternetGetConnectedState
-----------------------------
"I Think It Will Help"
-----------------------------
Alok Gupta
visit me at http://www.thisisalok.tk
|
|
|
|
|
Hello!
I have a dialog-based application. The main dialogs shows another dialog before showing itself (think of a login dialog before the main dialog). Now my problem is that the login dialog isn't shown in the taskbar, only the main window shows up when you have logged in.
The dialog has caption, system menu, format: context menu, border: dialog border, 3D view, visible, no fail create, centered.
Now the question: how do I show up the first dialog in the taskbar??
Dominik
_outp(0x64, 0xAD);
and
__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do??
(doesn't work on NT)
|
|
|
|
|
I assume the app dialog is hidden until they login. Have a look at http://www.codeproject.com/shell/systemtray.asp[^] Functions there like CSystemTray::RemoveTaskbarIcon() may provide some clues.
Another thing to try is to make the login dialog a top level window, then it should appear in the task bar.
Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"
|
|
|
|
|
Hi all! I have a strange problem with drawing icons in GDI+. I'm loading icon at init of my custom control class with:
HICON hIcon = theApp.LoadIcon(<some resorse="" id="">);
m_pIcon = new Bitmap(hIcon);
then in my drawing function I'm using
graphics.DrawImage(m_pIcon, destRect);
Well, everything is ok when icon is black, but more it is lightly and has more thin lines, then more it becomes ugly gray around edges (gray more visible in direction to right-bottom corner of wnd, something like inner shaddow in PhotoShop). How can I eliminate that?
Thanks!
|
|
|
|
|
I have a dialog box that I am trying to change the background color of the listbox and text fields (CEDIT). This is what I have done so far:
1. In the header, public section,declare a variable (CBrush m_bkColor;)
2. Using wizard, created the WM_CTLCOLOR function
3. funtion looks like this
HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
switch(nCtlColor)
{
case CTLCOLOR_LISTBOX:
case CTLCOLOR_EDIT:
{
pDC->SetTextColor(RGB(0,0,0));
pDC->SetBkColor(RGB(192,220,192));
return (HBRUSH)(m_bkColor.GetSafeHandle());
}
case CTLCOLOR_BTN:
case CTLCOLOR_STATIC: //text for dialog box
//case CTLCOLOR_DLG: //dialog background color
//return static_cast<hbrush>(m_pBrush->GetSafeHandle());
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
The problem is that only the background of the text is colored, not the entire listbox or text fields.
Any help will be greatly appreciated.
|
|
|
|
|
I was using the wrong function.
I used m_bkColor.CreateSolidBrush(RGB(10,45,12));
fixed it.
thanX
|
|
|
|
|
When I debug my program, I do not get any errors. When I run the program, it wont quit. It is a dialog application and the button to exit is:
void COpenGLDialogDlg::OnOk()
{
CDialog::OnOK();
}
When I look at the task manager to quit the application, no application that is running shows up and the usage is at 100%.
Help!
Thanks,
Chris
|
|
|
|
|
have you tried replacing CDialog:OnOK() with simply OnOK() or PostQuitMessage(0)?
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Thanks for the suggestion. It turns out that it was my bad, a infinite loop...thanks again.
Regards,
Chris
|
|
|
|
|
Hello,
Need to save a user.
Here is the problem. I have a listbox with logs and a have a funktion that needs to check user and save it in to a file.
In the listbox there can be lots of text but I now what text to find, I when I find that user I need to check if the user in logon or if the user have logout. So I search for the user text and get the user and check how many logon and how many logout but I don't get it to work.
*** Listbox log ***
bla bla bla USER:test logon
bla bla text
bla bla text
bla bla user: test logout
bla bla bla user: test logon
First I search for the logon text and if I find that
I get the user namne.
And when I have the user name I check how many logon and how many logout, But there can be many users so I donn't now how I should get this to work!
So I need help please.
|
|
|
|
|
Hi!
I'm currently writing some help file for my application using HtmlHelp.
Now I have a problem: I have 2 folders, containing help documents, the first folder is always open (i.e. the documents are shown), but the second one is always closed. I want that the second folder is open, too...
How can I do that?
Thanks and best regards,
Dominik
_outp(0x64, 0xAD);
and
__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do??
(doesn't work on NT)
|
|
|
|
|
I have put a command button on a dialog form and I am trying to simply call AfxMessageBox("Simple message box."); when I do call this function, the dialog form dims as if it is giving up focus but does not show the message box and hangs. Is there a header file that I need to include to use this function? Is there a message map involved?
Please help!
Thanks,
Chris
|
|
|
|
|
There is nothing you need to call AfxMessageBox(). Sounds like something else is wrong. Replace the AfxMessageBox() call with a TRACE statement and see what happens. Also step through the code in the Debugger which should shed some light on what's going on.
Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"
|
|
|
|
|
I do not get any errors when I do not have the AfxMessageBox() call. Any other suggestions?
|
|
|
|
|
Programmer_Chris wrote:
I do not get any errors when I do not have the AfxMessageBox() call. Any other suggestions?
Yes, stepping through in the debugger, like I already suggested.
Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"
|
|
|
|
|
Try to call MessageBox("Simple...") instead
|
|
|
|
|
I did and got the same problem. Thanks for the reply. Any other ideas?
|
|
|
|
|