LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-12-2005, 09:47 PM   #1
purefan
Member
 
Registered: Aug 2003
Location: Sweden
Distribution: Ubuntu 10.04
Posts: 99

Rep: Reputation: Disabled
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
 
Old 04-12-2005, 10:45 PM   #2
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Rep: Reputation: 30
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.
 
Old 04-12-2005, 11:18 PM   #3
purefan
Member
 
Registered: Aug 2003
Location: Sweden
Distribution: Ubuntu 10.04
Posts: 99

Original Poster
Rep: Reputation: Disabled
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...

Last edited by purefan; 04-12-2005 at 11:27 PM.
 
Old 04-12-2005, 11:24 PM   #4
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Rep: Reputation: 30
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
 
Old 04-12-2005, 11:34 PM   #5
purefan
Member
 
Registered: Aug 2003
Location: Sweden
Distribution: Ubuntu 10.04
Posts: 99

Original Poster
Rep: Reputation: Disabled
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...
 
Old 04-13-2005, 12:52 AM   #6
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Rep: Reputation: 30
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! :-)
 
Old 04-13-2005, 09:32 AM   #7
purefan
Member
 
Registered: Aug 2003
Location: Sweden
Distribution: Ubuntu 10.04
Posts: 99

Original Poster
Rep: Reputation: Disabled
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?
 
Old 04-13-2005, 09:45 AM   #8
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Rep: Reputation: 30
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.
 
Old 04-13-2005, 10:31 AM   #9
purefan
Member
 
Registered: Aug 2003
Location: Sweden
Distribution: Ubuntu 10.04
Posts: 99

Original Poster
Rep: Reputation: Disabled
very well. thanks a bunch!
 
Old 04-14-2005, 10:48 PM   #10
purefan
Member
 
Registered: Aug 2003
Location: Sweden
Distribution: Ubuntu 10.04
Posts: 99

Original Poster
Rep: Reputation: Disabled
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!?
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??
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ template smart pointers for class hiearchies in a vector push_back error R00ts Programming 7 09-20-2004 01:14 PM
implementing Class-based queue from a C program.. shrike_912 Programming 1 07-05-2004 01:01 PM
BlackBox.class & VerifierBug.class virus ??? dalek Linux - Security 4 02-29-2004 08:55 AM
Inheriting class members (Qt C++, QApplication class) jtshaw Programming 2 01-15-2004 11:52 AM
c++ : regarding (inheritence)base class and derived class edreddy Programming 6 07-31-2002 06:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration