Explain this set of Code

Started by
21 comments, last by SiCrane 12 years, 1 month ago

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

class car
{
public:
car(const string& name = " "); //cars name
string getName() const; //returns name
car* getNext() const;
void setNext(car* next);

private:
string m_name; //naame of car
car* m_pNext; //pointer to next car on the list

};

car::car(const string& name):
m_name(name),
m_pNext(0)
{}

string car::getName() const
{
return m_name;
}

car* car::getNext() const
{
return m_pNext;
}

void car::setNext(car* next)
{
m_pNext = next;
}

class Garage
{
friend ostream& operator<<(ostream& os, const Garage& aGarage);

public:
Garage();
~Garage();
void AddCar();
void DeleteCar();
void Clear();

private:
car* m_pHead;
};

Garage::Garage():
m_pHead(0)
{}

Garage::~Garage()
{
Clear();
}

void Garage::AddCar()
{
//getting new car
cout << "Please enter new name of car: " << endl;
string name;
cin >> name;

car* pNewCar = new car(name);

//if list is empty make new new head
if( m_pHead == 0)
{
m_pHead = pNewCar;
}

//otherwise end of the list
else
{
car* pIter = m_pHead;
while(pIter->getNext() != 0)
{
pIter = pIter->getNext();
}
pIter->setNext(pNewCar);
}
}

void Garage::DeleteCar()
{
if(m_pHead == 0)
{
cout << "The garage is empty !" << endl;
}
else
{
car* pTemp = m_pHead;
m_pHead = m_pHead->getNext();
delete pTemp;
}
}

void Garage::Clear()
{
while(m_pHead != 0)
{
DeleteCar();
}
}

ostream& operator<<(ostream& os, const Garage& aGarage)
{
car* pIter = aGarage.m_pHead;

os << "\nHere's what cars are in the garage:\n";
if(pIter == 0)
{
os << "The garage is empty.\n";
}

else
{
while(pIter != 0)
{
os << pIter->getName() << endl;
pIter = pIter->getNext();
}
}

return os;
}

int main()
{
Garage myGarage;
int choice;

do
{
cout << myGarage;
cout << "Here you'll have your own car garage.\n";
cout << "0 - Quit" << endl;
cout << "1 - Add Car" << endl;
cout << "2 - Delete Car" << endl;
cout << "3 - Clear Car" << endl;
cin >> choice;

switch(choice)
{
case 0:cout << "Thanks for Playing" << endl; break;
case 1:myGarage.AddCar(); break;
case 2:myGarage.DeleteCar(); break;
case 3:myGarage.Clear(); break;
default: cout << "Invalid choice.." << endl;
}
}while(choice != 0);

return 0;
}


This is a some code I got from reading the Beginning C++ through Game Programming, it explains it in the book, but I really don't understand. I was wondering if anyone could help explain. I get really confused around the Garage Add member fucntion, that's what confuses most.
Advertisement
What specifically confuses you about it? The general idea is this: get the name of the car and create a new car, and then add it to a linked list. My guess is it's the adding to the linked list part that's confusing you, but I'm not sure which part of it is confusing, so I'm not sure what more to say at this point. If you google how to make a linked list in C++, you should see other examples that may answer your question. Or you can give greater clarity on what you do/don't understand and we can try and explain a little more.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
I don't understand how the add function really works, the part that really confuses me is the else statement. So this how I think it goes. pIter points to the m_pHead, I get lost at the while statement. Does is keep looping until pIter equals a null pointer ? Then after that pIter sets next, so then pNext is equal to pIter.

I don't understand how the add function really works, the part that really confuses me is the else statement. So this how I think it goes. pIter points to the m_pHead, I get lost at the while statement. Does is keep looping until pIter equals a null pointer ? Then after that pIter sets next, so then pNext is equal to pIter.

Yes, exactly, except for that last part. It sets the last next value to the new car it just created. Think of it as a long chain. Each car has a pointer to the next car, chaining them together. The while loop just follows the head to the end of the chain (or the linked list, as we call it in computer science), and once it reaches the end of the chain (i.e. a null pointer, because the last car isn't linked to anything after it), it appends the car to the end of this chain (by setting pIter's next to the new car that was created).
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
so pIter works almost like an iter, using a while loop. and m_pHead is the head of the chain.

so pIter works almost like an iter, using a while loop. and m_pHead is the head of the chain.

Yes, which is why it was named pIter (i.e. "pointer iterator," I'm guessing is what they meant when they named it that). They use it to iterate through the linked list until the end, and then append the new data once they find the end.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
So every object thats created using the add member function. Has a m_pNext pointer beside it. So when the add function is called, the pointer pIter point to m_pHead which is in front of the chain, the while loop continues until pIter is a null pointer. In the loops body, it returns the address of each m_pNext for each object. Once its gets to zero, pIter calls setNext which equals zero and initlizes pNewCar to it.

void Garage::AddCar()
{
//getting new car
cout << "Please enter new name of car: " << endl;
string name;
cin >> name;

car* pNewCar = new car(name); // Create an instance of car, initialing it with name.

if( m_pHead == 0) // If there are no cars in our list (i.e. m_pHead is 0)
{
m_pHead = pNewCar; // Set the car we just created to be the head of the list.
}
else // If there are cars in the list
{
car* pIter = m_pHead; // Get a pointer to the first car in the list
while(pIter->getNext() != 0) // While their IS another car in the list (i.e. getNext() returns non-null)
{
pIter = pIter->getNext(); // Move to the next car in the list.
}
pIter->setNext(pNewCar); // pIter now points to the last car in the list, insert our new car after it.
}
}

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

So pIter starts off at the first address of the list in this case m_pHead, keeps moving done to the list, and once it reaches the last object it goes straight back to 0 or the next memory address, and assigns the new car to it.
No. pIter never goes to null. It goes to the very last element in the list, whose NEXT pointer is null.

Note: Null. Not zero. The use of 0 here is confusing, but we are talking about NULL pointers, not "zero" pointers.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

This topic is closed to new replies.

Advertisement