create a objects vector class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jazi
    New Member
    • Jun 2007
    • 21

    create a objects vector class

    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 ).
    Last edited by sicarie; Jun 11 '07, 03:10 PM. Reason: Please use [code=cpp] and [/code] tags around your code. Thanks!
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    Not quite sure what you mean.....
    Or how push_back is implemented.... .

    Incedently, you have a void function (set_no_student s) which aooears to return an int......

    Comment

    • Jazi
      New Member
      • Jun 2007
      • 21

      #3
      Originally posted by DeMan
      Not quite sure what you mean.....
      Or how push_back is implemented.... .

      Incedently, you have a void function (set_no_student s) which aooears to return an int......
      Sorry for not being clearer in explaining what I want to do but here is what I have. I am showing two functions in this class to illustrate my class :set_no_student s and get_all_student s. The set_no_students is a separate function within that class which is not important in here. I should not have included it in here but please ignore it for now. However, the get_all_student s is my concerned function that returns a vector of objects students. In that function I create an object and then I push it into the vector and then I return that vector back whenever called. I thought there might be a better approach than this.

      In fact, this class works fine. I have tested it using c++ driver and everything is great. The issue us when I try to wrap java around this class using SWIG. I am having hard time accessing the vector objects from java once I call this function. So I thought this may work if I implement this class differently. So instead of having this function returns a vector it should push these objects to the main class itself. And this is where I want help or different approach. Does this make any sense to you?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        I think there's a wrong approach here.

        Instead of a RegisteredStude nts class, there should be a vector of Students:
        [code=cpp]
        vector<Student> RegisteredStude nts;
        [/code]

        You can use the remove_copy_if( ) algorithm to copy all Students to another vector<Students > based on a specified age.

        Comment

        • Jazi
          New Member
          • Jun 2007
          • 21

          #5
          So how do you recommend I go about building that class?

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by Jazi
            So how do you recommend I go about building that class?
            You don't have to build a class. You just create a vector<Student> and then use vector methods or maybe write your own functions taking vector<Student: :iterator arguments.

            In this approach, the students are registered because they are in the vector<Student> object and not because they are objects of a RegisteredStude nt class.

            Comment

            Working...