473,378 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

create a objects vector class

21
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.
Expand|Select|Wrap|Line Numbers
  1. RegisteredStduents class{
  2.    RegisteredStudents(no_of_students);
  3.   vector<studnet> get_students(by_age){
  4.    vector<student>  results;
  5.    student stud_obj=new student();
  6.    for(query students based on age{
  7.         stud_obj->set_name(blahblah);
  8.         stud_obj->set_address(blahblah);
  9.         results->push_back(stud_obj);
  10.     }
  11.     delete stud_obj;
  12.     return results;   
  13.    }
  14.    void set_no_students(no_of_students){
  15.        return no_of_students;
  16.    }
  17. }
This class works fine but I thought there might be a better way to do this. Instead of having a function "get_students" 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 ).
Jun 11 '07 #1
5 2762
DeMan
1,806 1GB
Not quite sure what you mean.....
Or how push_back is implemented.....

Incedently, you have a void function (set_no_students) which aooears to return an int......
Jun 11 '07 #2
Jazi
21
Not quite sure what you mean.....
Or how push_back is implemented.....

Incedently, you have a void function (set_no_students) 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_students and get_all_students. 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_students 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?
Jun 12 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
I think there's a wrong approach here.

Instead of a RegisteredStudents class, there should be a vector of Students:
Expand|Select|Wrap|Line Numbers
  1. vector<Student> RegisteredStudents;
  2.  
You can use the remove_copy_if() algorithm to copy all Students to another vector<Students> based on a specified age.
Jun 12 '07 #4
Jazi
21
So how do you recommend I go about building that class?
Jun 12 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
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 RegisteredStudent class.
Jun 14 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Audrius | last post by:
Hello, what is the best solution to hold the collection of Objects. Lets say I have such class: class Task{ private: CString title; CString description; CString owner;
12
by: Alfonso Morra | last post by:
I have the ff code for testing the concept of storing objects: #include <vector> #include <iostream> using namespace std ; class MyClass { public: MyClass(){
6
by: ken.carlino | last post by:
>From http://www.parashift.com/c++-faq-lite/references.html#faq-8.6, it said "Use references when you can, and pointers when you have to." And I need to convert this java class to c++: public...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
7
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
9
by: Jazi | last post by:
Hello everyone, I was wondering if someone can help me figuring out some errors I keep getting when I try to create a dll library from existing c++ code using VS2005. The issue is related to a...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
3
by: Ramon F Herrera | last post by:
Newbie alert: I come from C programming, so I still have that frame of mind, but I am trying to "Think in C++". In C this problem would be solved using unions. Hello: Please consider the...
3
by: Alex | last post by:
Hi everyone, I have a question that implies thinking about some design issues with C ++. I have a method like this: class C { .... public:
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.