Thread: Overloading Array Objects for use with Classes

  1. #1
    Registered User
    Join Date
    Mar 2007

    Question Overloading Array Objects for use with Classes

    I'm trying to find a tutorial or some direction on overloading array objects for use with a class. An example of a program that does this would really help since the book I'm using doesn't provide any help here, but simply lists the syntax for a non-constant array as "Type& operator[](int index);" and a constant array as "const Type& operator[](int index) const;" with Type being the data type of the array elements. I'm trying to instantiate objects for my CollegeCourse class by creating an array of objects. I need to create three CollegeCourse objects containing information such as "ENG101", 'A', and 3 (credits). I learn best by example, but like I said, the book I'm referencing is severely lacking in examples. I'm having a hard time finding class examples at all on the internet. Can anyone help? Hope I explained this alright. Thanks!

  2. #2
    Join Date
    Oct 2007
    Inside my computer
    If you're trying to initialize classes (you are trying that, right?), then you need to create your custom constructors.
    Syntax is
    ClassName(argument_list); // Declaration
    ClassName::ClassName(argument_list) { } // Definition
    Note that constructors have no return type.
    Though I'm not sure how you're trying to create those objects. And I don't see why you need to overlord operator [].

  3. #3
    Registered User
    Join Date
    Jan 2005
    An array will work with a class already. The code you sited looks like part of a user defined array class, rather than built-in arrays.

    Do you have your CollegeCourse class written? Can you show the "array" class you're using?

    For arrays and most array classes, you can create the objects in the array automatically, but you cannot initialize them with different data when they are created (although this might change in future C++ versions). So to create three objects with different information, you'll have to create the three in the array and change the information, or create them one at a time and add them to the array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Array of pointers to point objects
    By totalfreeloader in forum C++ Programming
    Replies: 6
    Last Post: 11-27-2003, 09:26 AM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. for loop to create an array of objects
    By Shadow12345 in forum C++ Programming
    Replies: 9
    Last Post: 05-03-2002, 06:26 PM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM