| | |
vectors and classes
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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?
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?
•
•
Join Date: Apr 2006
Posts: 15
Reputation:
Solved Threads: 0
•
•
•
•
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?
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.
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.

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.
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
Staff myStaffList;
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,
C++ Syntax (Toggle Plain Text)
class Staff { vector<Employee> members; std::string listName public: Staff() : listName("Unnamed List"); Staff(std::string s) : listName(s); };
You could also do the initialisation in the constructor body
C++ Syntax (Toggle Plain Text)
class Staff { vector<Employee> members; std::string listName public: Staff() { listName = "Unnamed List"; } Staff(std::string s) { listName = s; } };
•
•
•
•
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)
Also, as with member functions, you may define the body of the constructor outside the class declaration, eg,
C++ Syntax (Toggle Plain Text)
class Staff { vector<Employee> members; public: Staff(std::string, double); Staff(Employee); }; Staff::Staff(std::string s, double x) { // do something here } Staff::Staff(Employee e) { members.push_back(e); }
•
•
•
•
Is there a situation where that would be useful or is it unconventional?
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);
}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.
![]() |
Similar Threads
- How do i do chat program using MFC (Microsoft Foundation Classes) and Visual Basic? (Visual Basic 4 / 5 / 6)
- File Operations (C++)
- lists and vectors (C)
- Help with Classes (C++)
- Working with objects of different Classes (Java)
- Classes (C++)
- Summer Classes (Geeks' Lounge)
Other Threads in the C++ Forum
- Previous Thread: urgent help!!!
- Next Thread: GetValues Help
Views: 2151 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





