I am newbie to c++ and I would like to build a vector class that returns a vector of objects. I have done some thing like this:
student class that contains some of student's properties such as name, address, phone etc.
I also have a registered students class which I like to have it as a vector of student objects. The way I have done it so far is like this.
[code=cpp]
RegisteredStdue nts class{
RegisteredStude nts(no_of_stude nts);
vector<studnet> get_students(by _age){
vector<student> results;
student stud_obj=new student();
for(query students based on age{
stud_obj->set_name(blahb lah);
stud_obj->set_address(bl ahblah);
results->push_back(stud _obj);
}
delete stud_obj;
return results;
}
void set_no_students (no_of_students ){
return no_of_students;
}
}[/code]
This class works fine but I thought there might be a better way to do this. Instead of having a function "get_studen ts" that returns a vector of objects, I can have the whole class to be a vector and in my get_students I can push_back the students objects back to the class vector. I am not really positive on how to do that.
So I will be grateful if someone tell me how to do that or send me a link to an example on the web. I have been searching all over the place but no luck so far. Also it will be helpful if you can show me in how to call this class from a driver (i.e ).
student class that contains some of student's properties such as name, address, phone etc.
I also have a registered students class which I like to have it as a vector of student objects. The way I have done it so far is like this.
[code=cpp]
RegisteredStdue nts class{
RegisteredStude nts(no_of_stude nts);
vector<studnet> get_students(by _age){
vector<student> results;
student stud_obj=new student();
for(query students based on age{
stud_obj->set_name(blahb lah);
stud_obj->set_address(bl ahblah);
results->push_back(stud _obj);
}
delete stud_obj;
return results;
}
void set_no_students (no_of_students ){
return no_of_students;
}
}[/code]
This class works fine but I thought there might be a better way to do this. Instead of having a function "get_studen ts" that returns a vector of objects, I can have the whole class to be a vector and in my get_students I can push_back the students objects back to the class vector. I am not really positive on how to do that.
So I will be grateful if someone tell me how to do that or send me a link to an example on the web. I have been searching all over the place but no luck so far. Also it will be helpful if you can show me in how to call this class from a driver (i.e ).
Comment