LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Implementing a vector class from a list class (https://www.linuxquestions.org/questions/programming-9/implementing-a-vector-class-from-a-list-class-312608/)

purefan 04-12-2005 09:47 PM

Implementing a vector class from a list class
 
Hello!
Well I need (for a homework) to implement a class vector from a class list.
im using VC6++.
The class list is here:
http://www.di-mare.com/pub/Pg2/ADH_list.zip

We have to use that exact list.
and the declaration of the class vector is:


class vector {
public:
vector(size_t n); // Contructor
vector(); // Contructor by default
~vector();

void Add_End (size_t); // add elements to the end
void Add_Beg (size_t); // add elements to the beginning

void Del_End (size_t); // Delete elemnts from the end
void Del_Beg (size_t); // Delete elements from the beginning

T & operator[] (size_t); // V[j]
size_t Capacity() const;
size_t Dimension() const;
}; // vector

now, don't get me wrong, I don't pretend one of you making all the work and I get a good grade. I really need to learn how to do this.

By implementing I think he means that we must be able to declare vectors like this:
vector MyVector;
vector TheVector(5);
MyVector.Add_End(1);
TheVector.Add_Beg(3);

correct me if Im wrong but the list makes it possible for the vector to hold any of the basic types right?

I must write each method for the class vector (Add_Beg, Add_End etc...) but I dont have a clue of how to do it. I dont really understand how to do it and neither how to make the vector class instantiable...

by the way, we are using a typedef so wherever you see size_t it means int


I must beg for your assistance, im lost here...
Thanks anyway :)

ahwkong 04-12-2005 10:45 PM

For a start I think your declaration of vector class is not correct


class vector {
public:
vector(size_t n); // Contructor
vector(); // Contructor by default
~vector();

void Add_End (const T&); // add elements to the end
void Add_Beg (const T&); // add elements to the beginning

void Del_End (); // Delete elemnts from the end
void Del_Beg (); // Delete elements from the beginning

T & operator[] (size_t); // V[j]
size_t Capacity() const;
size_t Dimension() const;
}; // vector

You are basically asked to use the list to store type T. (Remember the tdef.h?). Therefore your function declaration should take this into account. And therefore they should not just take size_t. And if you delete something form front and back, it does not make much sense to further provide a size_t parameter.

Obviously he does not require you to use template but it is another story.

I think if I start to explain how to do it, you will not be able to learn anything. Please first review what you learn about inheritance. Hint: May be the vector class can inherit form list and modify certain behaviour/add certain operation to make a list work as a vector.

purefan 04-12-2005 11:18 PM

as for the suggestion of the declaration: Im sorry that is how he (the teacher) wants us to use it. so either it is correct or we cannot modify it, in fact he has a version (supossely) that uses this declaration.

Im getting your idea of inheritance, the vector class should inherit from the list class the methods for adding and deleting but not to be used directly, instead to have the functions in the list class using them, for example:

Code:

vector::Add_End(size_t Th)
{
list::push_front(Th);

}//Add_End

but then again this sole function does not compile...

ahwkong 04-12-2005 11:24 PM

I very much doubt it. You can raise this as a question to your lecturer. He may make a mistake.

It is because if you want to do thing like

Code:

list::push_front(Th);  // I am not saying it is a correct way, btw ...
At the very lease the declaration must be:

Code:

class vector: public ADH_list {
You should double check with your lecturer. If he insists what you listed here is correct, then he want you to implement a "Has-a" relationship rather than a "Is-a" relationship.

Again, check the OO literature for more detail

purefan 04-12-2005 11:34 PM

ok, I have just sent him the question.
now, I honestly I trust you ahwkong.
so Im trying to make this as you suggested.
Having the declarations:
Code:

void Add_End (const T&); // add elements to the end
void Add_Beg (const T&); // add elements to the beginning

how can I make my instance of the vector class, be modified by its methods?
I mean, I will instantiate the vector class like this:
Code:

vector MyVector
right?
and then I should be able to do:
Code:

MyVector.Add_Beg(AnInteger);
but when writing the function...how should it be??
I am lost here...

ahwkong 04-13-2005 12:52 AM

Well, don't trust me. Be critical. Trust yourself and 'Reasoning' -- well, I could be wrong, right ? :-)

I guess afterall you are not very familiar with C++ programming in general. I may try to find some C++ material for you.

But, nothing stop you from looking them up now in google! :-)

purefan 04-13-2005 09:32 AM

ok :)
well if you could give me an example of how a method should look in this case I think I would get the idea of them all.
I already tried in google ("how to implement a class vector from a class list") and among the best results I found are:
http://deathrow.vistech.net/docs/pro...65/clvgenr.htm
http://wwwasd.web.cern.ch/wwwasd/lhc...r/vec_0251.htm
and lots of template-related documents, seems like everyone just use the STL classes
I read the chapter of classes in Deitel's "how to program" and of course "The C++ programming language" but I still dont understand how a function may modify an instance of the class....

Code:

vector Myvector;
how:
Code:

Myvector.Add_End(AnyInteger);
can happen?

ahwkong 04-13-2005 09:45 AM

Would that be too easy if "how to implement a class vector from a class list" can be readily located in the web? I guess you should not assume your lecturer THAT lazy right? :-)

I would suggest you to look more in the direction of 'Has-a' and 'Is-a': how to implement these type of relationship in c/c++

Here is a must read:

http://www.mindview.net/Books/TICPP/...ngInCPP2e.html


OK. I can't go any further than that. If I say more, I will virtually write the whole program for you. (i.e. you are already VERY close to the solution)

If you read the above suggested book and still do not understand some CONCEPT, start a new thread with a specific question. You may get more help.

purefan 04-13-2005 10:31 AM

very well. thanks a bunch! :)

purefan 04-14-2005 10:48 PM

ok...well I realized that for such a task I need to create an instance of the list, IN the vector class.
What a better way of doing it than in the constructor of the class!? :D
however I now get 16 linker errors...with things that really don't make much sense....
I also included the ADH_list.cpp and created the same instance in both constructors of the vector class
am I going in the right direction??


All times are GMT -5. The time now is 04:08 PM.