vectors and classes

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2006
Posts: 15
Reputation: puppy is an unknown quantity at this point 
Solved Threads: 0
puppy puppy is offline Offline
Newbie Poster

vectors and classes

 
0
  #1
Apr 19th, 2006
Hi,
I'm trying to write a class that uses a vector, but I can't get the right syntax in the Staff constructor. Can anyone help?

public:
Staff();
Staff (???????????);

private:
vector<Employee> members;
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 505
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 51
Bench's Avatar
Bench Bench is offline Offline
Posting Pro

Re: vectors and classes

 
0
  #2
Apr 19th, 2006
My mind-reading device isn't working today - what are you actually trying to do with the constructor? Construct a Staff object using an existing vector<Employee> ? Construct it using a single Employee object? Make a copy constructor..? Construct it using some other value..?

Bear in mind that the syntax of a constructor is exactly the same as the syntax for any other function.. (Although constructors can have an initialisation list) How would you accomplish what you need with a plain old function?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 15
Reputation: puppy is an unknown quantity at this point 
Solved Threads: 0
puppy puppy is offline Offline
Newbie Poster

Re: vectors and classes

 
0
  #3
Apr 19th, 2006
Originally Posted by Bench
My mind-reading device isn't working today - what are you actually trying to do with the constructor? Construct a Staff object using an existing vector<Employee> ? Construct it using a single Employee object? Make a copy constructor..? Construct it using some other value..?

Bear in mind that the syntax of a constructor is exactly the same as the syntax for any other function.. (Although constructors can have an initialisation list) How would you accomplish what you need with a plain old function?
Wow! You have a mind-reading device. That's cool!

Employee is a class. I'm supposed to write another class, Staff, that has functions add(), find(), etc. and uses "vector<Employee> members" in private section of the definition.

Thanks for your help.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 505
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 51
Bench's Avatar
Bench Bench is offline Offline
Posting Pro

Re: vectors and classes

 
0
  #4
Apr 20th, 2006
OK - you still haven't said anything about what you want your constructor to do, which, I believe is what your original question was about

if your vector<Employee> is your only data member, then that doesn't need initialising in order for you to add elements to it. The main purpose of a constructor is to initialise data members and sometimes obtain memory resources for them. It seems from what you've said, that you don't need to do either of those, so you can just make do with the default constructor.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 15
Reputation: puppy is an unknown quantity at this point 
Solved Threads: 0
puppy puppy is offline Offline
Newbie Poster

Re: vectors and classes

 
0
  #5
Apr 20th, 2006
I'm not trying to be vague. I'm new to programming and don't know all the ins and outs yet. A constructor constructs. That's all I know. As to your original reply, I'm pretty sure I don't want a copy constructor. I think I want to construct a Staff object using an existing vector<Employee>.
Thanks.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 505
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 51
Bench's Avatar
Bench Bench is offline Offline
Posting Pro

Re: vectors and classes

 
0
  #6
Apr 20th, 2006
Originally Posted by puppy
I'm not trying to be vague. I'm new to programming and don't know all the ins and outs yet. A constructor constructs. That's all I know. As to your original reply, I'm pretty sure I don't want a copy constructor. I think I want to construct a Staff object using an existing vector<Employee>.
Thanks.
When you write a class, C++ already provides you with an invisible default constructor - you don't need to do anything, you don't even need to type it in your code. the constructor will automatically be invoked when you create a staff object, eg,
  1. Staff myStaffList;
Since the only member of your Staff class is a vector<Employee> - a member which doesn't need initialising in order to use it (the vector class itself will have its own constructor which will allow it to be used), you should settle for the default constructor, and concentrate on your member functions for now.

On the other hand, if you later add other values, for example, you might want a string data member to hold the name of your Staff list, you should use a constructor to assign a default value such as "Unnamed Staff List", or a constructor taking a std::string argument to let you specify your own name at the point of construction.
eg,
  1. class Staff
  2. {
  3. vector<Employee> members;
  4. std::string listName
  5. public:
  6. Staff() : listName("Unnamed List");
  7. Staff(std::string s) : listName(s);
  8. };
This above example uses the initialisation list to assign values (preferred method for constructors)

You could also do the initialisation in the constructor body
  1. class Staff
  2. {
  3. vector<Employee> members;
  4. std::string listName
  5. public:
  6. Staff()
  7. {
  8. listName = "Unnamed List";
  9. }
  10. Staff(std::string s)
  11. {
  12. listName = s;
  13. }
  14. };
This way might be a bit easier for you to read, since the syntax is identical to that of any other member function - the end result is the same however.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 15
Reputation: puppy is an unknown quantity at this point 
Solved Threads: 0
puppy puppy is offline Offline
Newbie Poster

Re: vectors and classes

 
0
  #7
Apr 20th, 2006
Can a constructor take more than one argument? If so what's the syntax? Is it something like

Staff(std::string s, double x)

Is there a situation where that would be useful or is it unconventional?

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 505
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 51
Bench's Avatar
Bench Bench is offline Offline
Posting Pro

Re: vectors and classes

 
0
  #8
Apr 20th, 2006
Originally Posted by puppy
Can a constructor take more than one argument? If so what's the syntax? Is it something like

Staff(std::string s, double x)
Yes, that's perfectly acceptable. there's no arbitrary limit to the number of arguments you can take - although it becomes somewhat unweildy if you provide too many IMHO. (if you've got 6 or more, your code is probably going to look quite ugly - there's almost certainly a better way)

Also, as with member functions, you may define the body of the constructor outside the class declaration, eg,

  1. class Staff
  2. {
  3. vector<Employee> members;
  4. public:
  5. Staff(std::string, double);
  6. Staff(Employee);
  7. };
  8.  
  9. Staff::Staff(std::string s, double x)
  10. {
  11. // do something here
  12. }
  13.  
  14. Staff::Staff(Employee e)
  15. {
  16. members.push_back(e);
  17. }

Is there a situation where that would be useful or is it unconventional?
Many situations - you only need to look through the STL to find dozens of examples. The std::vector class has 8 different constructors, some of which take 2, 3 or 4 parameters.

A good example with vectors would be to create a new container based on some elements of an existing container:

#include <vector>
#include <iterator>
#include <algorithm>

int main()
{
    std::vector<int> numberList;
    numberList.push_back(5);
    numberList.push_back(7);
    numberList.push_back(19);

    std::vector<int>::iterator myIter;
    myIter = std::find(numberList.begin(), numberList.end(), 7);

    std::vector<int> newNumberList(numberList.begin(), myIter); 
}
The snippet above creates a vector of int's holding these numbers - 5, 7, and 19
then the find() algorithm searches for the number 7 in the vector.
when it finds 7, it records the position into myIter

newNumberList is then created with all the elements from numberList(begin) - that is, the first element, up until the position stored by myIter.

This is done by a vector constructor which accepts 2 STL iterators as parameters.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 15
Reputation: puppy is an unknown quantity at this point 
Solved Threads: 0
puppy puppy is offline Offline
Newbie Poster

Re: vectors and classes

 
0
  #9
Apr 20th, 2006
Thanks for all the examples. That's really helpful.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 505
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 51
Bench's Avatar
Bench Bench is offline Offline
Posting Pro

Re: vectors and classes

 
0
  #10
Apr 20th, 2006
Originally Posted by Bench
newNumberList is then created with all the elements from numberList(begin) - that is, the first element, up until the position stored by myIter.
Urgh, sorry about that typo - that should have been numberList.begin()
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 2151 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC