Thread: Class QUestion

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    208

    Class QUestion

    Hi all,

    I am new to C++, though I have been programming in C for quite some time. I have a class named Data, and I would like to define arrays in this class x, y, z, etc etc; now I want the size of these arrays to be defined at the construction of the class. How would I go about doing so?

    Would I just define the array's like this, x[], y[], z[], and use malloc() within the constructor?
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How would I go about doing so?
    The same way you would in C if you wanted a dynamic array. Use pointers and new[], then delete[] in the destructor. However, a much better solution is to take advantage of the vector class. Memory is handled behind the scenes and you can use the vector constructor to define a size in your class' constructor.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    208
    could you elaborate on this vector class I have no heard of it.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by kas2002
    could you elaborate on this vector class I have no heard of it.
    You're the research assistant; I'd expect you'd be able to research.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    208
    Interesting I just read the tutorial on this website and these vectors seem far more flexible then arrays, technically I would not even have to define a size in the constructor if I am reading this correctly, I could just loop through my data and keep adding a new one until all the data is read in?

    edit: I am ever so sorry pianorain that I read his post, replied, THEN went to see what I could find on google. I mean how dare I hope he may have a good reference. Turns out I found one but I should have been able to predict that shouldn't I? I see no need in taking offense to me asking someone to elaborate before I search. Since I had never heard of it I wasn't sure I was going to find it on my own and if that is in someway wrong then I deeply apologize. However, I feel as though most people here are willing to help as opposed to just flaming one for asking for more information before they rush off and search.
    Last edited by kas2002; 08-14-2006 at 09:45 AM.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by kas2002
    I could just loop through my data and keep adding a new one until all the data is read in?
    Yup, as long as you don't run out of memory.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >these vectors seem far more flexible then arrays
    Indeed. There are issues, but you're unlikely to encounter them with basic use.

    >technically I would not even have to define a size in the constructor if I am reading this correctly
    Vectors grow and shrink dynamically, but if you need a fixed size then it's better to define it on construction. That way you don't have to worry about excessive reallocations and data movement.

    >I could just loop through my data and keep adding a new one until all the data is read in?
    For an unknown quantity of data, this would be ideal. But as I said before, if you already know how much data to expect, you can preallocate that many items to the vector and save yourself cycles.

    >Yup, as long as you don't run out of memory.
    Keep in mind that containers have a maximum size that very likely won't be the limit of virtual memory. The max size is rather large though, so there's usually no need to worry.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    208
    Ok, I have one more question
    Can I use a vector in a scanf statement as follows?

    scanf("%i %i", vect.push_back(), vect.pushback())

    because I'm not sure what to put as the argument to push_back in that case
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Can I use a vector in a scanf statement as follows?
    Um, no. push_back requires an argument.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    May 2002
    Posts
    208
    ok

    Thanks all
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    208
    One more question actually, since I haven't got a great grasp on inheritence.

    To access a member of my vector x, which is defined and initialized in class data, does this work?

    data.x.at(index);
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yes, or you can just use the overloaded array access operator:
    Code:
    data.x[index] = whatever;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    May 2002
    Posts
    208
    Even with a vector?
    That causes no conflicts?

    Also I have come across another problem (sorry I am a vector newb), if I want to print the last value added to a vector how would I go about it in a printf(); statement without using iterators?
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use the back() member function.

    cppreference.com has a list of some vector member functions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Even with a vector?
    That causes no conflicts?
    In C++ you can overload operators for you own custom classes. So you can define + - * / == who-knows-what to do anything you like. In this case operator[] has been overloaded for the vector class.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  2. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. question about DLL's and class functions
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 02-25-2003, 06:08 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM