C/c++ programing
Expert: Joseph Moore - 9/17/2009
Questioni have a C++ project which i am to submit very soon, i am not good at programing. what is that i need to become a good programmer? the project i have goes like we are to create a linked list to register a car into a parking lot. attributes of a car are: a unique registration number, driver's license, model, number of passengers and their ids. is it possible to help me at least for now?
AnswerHi, Thao.
Your first question, "what is it that I need to become a good programmer," is a bit difficult to answer. Time, patience, practice... these are all required. I firmly believe that anyone can become a good programmer, it just takes time (and some people require more time than others). Just try to learn as much as you can as quickly as you can, and don't worry if you seem not to understand things initially. Keep at it and you'll figure it out. I can always try to help you understand things, too.
Now, on to the project. I'm not going to outright give you the code for what you want: I want you to try it yourself first. I will, however, try to explain the concepts to you and I'll give you a little bit of sample code to demonstrate the concepts. I'd like you to try to complete the project yourself, and if it gives you any trouble at all, please feel free to send me the code and I'll help you with it.
So you have to create a linked list of either structures or classes, depending on how you want to implement the car data. You haven't given any indication of how far into C++ programming you are, so I'm just going to demonstrate the concepts of a class and a struct real quick. I apologize if you are already very familiar with this.
In C++, there is very little difference between a class and a struct. Really, it's just a conceptual difference primarily. The only implementation difference is that a struct is public by default whereas a class is private by default. You can, of course, manually change the access level of a class or a struct to be private, public, or protected. Generally, if you're just interested in a collection of data, you'll use a struct. If you want a collection of data and functions, you'll use a class.
Declaration of classes and structs is virtually identical, too. The only difference between a basic declaration of a class or struct is the keyword used, either "class" or "struct":
class myClass
{
// implementation details
};
struct myStruct
{
// implementation details
};
I mentioned earlier that you can change the access level of a class or struct. This is done by using the keywords "private", "protected" and "public". I'll demonstrate:
class myClass
{
public:
// everything placed here is public
private:
// everything placed here is private
protected:
// everything placed here is protected
};
Public means that the data is accessible by anyone. Any function can modify variables that exist in the public space and can call functions that exist in the public space. Private means that no one can access the data (with one exception: if you have marked another class as a "friend", then that class can access private data). Protected means that child (and friend) classes/structs can access the data, but it is off limits to everyone else.
In your case, it sounds like a struct would be sufficient, since you're just collecting data. If you have common operations that need to be performed on that data (IE, operations that are going to be implemented as functions), I would instead recommend a class. Your struct might look something like:
struct car
{
unsigned int registration;
char* dlNumber;
// etc
};
Linked lists are probably the first real data structure you learn. Linked lists are incredibly simple and easy to implement, and a good number of data structures are built on the backbone of a linked list. In its simplest form, a linked list is nothing but some data and a pointer to the next item in the list. This is called a singly-linked list. It can be defined as a class or a structure, but generally the list itself is defined as a class and it is made up of multiple "nodes" which are defined as structures:
struct listNode
{
void* pData;
listNode* pNext;
};
The list class itself serves as a container for what is known as the "head" node, which is simply the node at the beginning of the list. If the head node is NULL, then the list is empty. As you traverse the list, you know you have reached the tail of the list (or the end of the list) when you encounter a node whose "next" node is NULL. The list class should have some basic functionality built into it such as "add" and "remove". The list class definition would be something like:
class linkedList
{
public:
linkedList()
{
pHead = NULL;
}
void add(void* pData);
void remove(listNode* pNode);
bool isEmpty() { return pHead == NULL; }
listNode* getHead() { return pHead; }
private:
listNode* pHead;
};
The simplest way to add a new item to the list is to create the new item, set its next pointer to the list's head pointer, and then set the list's head to the new item. In this way, every new item is put at the beginning of the list, so the list is in reverse creation order. You could put the items at the end of the list, but that would mean that you would have to traverse the list every time in order to add a new item. It also creates a special case scenario where the list is empty, so you must set the head of the list to the new item.
Removing nodes in a singly-linked list is trickier, because you have to maintain the last node you visited so that you can fix up the next pointers when you take an item out of the list. If you simply remove the item without making the item's previous node point to the item's next node, then you have a broken list. The remove operation is much simpler in a doubly-linked list than in a singly-linked list, but for now, let's stick with singly-linked lists. I can discuss doubly-linked lists with you in the future if you so desire.
Ok, that should be enough to get you running with this stuff. If you have any trouble, please let me know. I'm here to help.