Thread: Class Arrays

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    55

    Class Arrays

    I was wondering how you can define an array of the same class. Pretend we have 10 different cars and they all have the same properties. (This isn't really what I'm doing, but just a made up example).

    Code:
    class Car
    {
    public:
    	unsigned int year;
    	unsigned char color[256];
    	unsigned short weight;
    }
    Now say I want to create multiple instances of Car class in an array. Would I do something like this?:

    Code:
    Car cars[10];
    If this is correct, could I do something like this to loop through and get information from a user? Just an example:
    Code:
    	for( unsigned int i = 0; i < 10; i++ )
    	{
    		cout << "Enter a cars year: \n";
    		cin >> cars[i].year;
    		cout << "Enter a cars color: \n";
    		cin >> cars[i].color;
    		cout << "Enter a cars weight: \n";
    		cin >> cars[i].weight;
    	}
    	
    	for( unsigned int i = 0; i < 10; i++ )
    	{
    		cout << "Year: " << cars[i].year;
    		cout << "\tColor: " << cars[i].color;
    		cout << "\tWeight: \n" << cars[i].weight;
    	}

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indeed, you could. Your example is correct.
    You just beware that you're destroying the entire purpose of the class. This isn't a class, this is a POD.
    Classes hide the implementation. Just so you're aware.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    55
    Awesome! Guess I'll just have to play around with it.

    Can you elaborate on POD's and how I'm destroying the purpose of the class? Not sure what you mean, I am still new to this class thing.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A class is like an object. If you make a car, you don't expose the car's internals.
    Instead, you expose a public interface, such as a wheel or pedestals for acceleration and retardation.
    These public interfaces then control the hidden engine and interfaces with all the other parts of the car, which you don't see.

    The same idea holds true for classes. It exposes a public interface (methods, functions), but hides its internals (variables). The methods manipulate the internal data in such a way that you, as a programmer that creates an instance of the class, do not have to worry about what goes on inside the class, only that the function do their work.

    A POD is essentially a struct with just members. No functions, no constructors, and all members are visible. It is treated as a separate type according to the standard. It's a POD.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    POD Type = Plain Old Data Type
    You could write the same thing as:
    Code:
    struct Car
    {
    	unsigned int year;
    	unsigned char color[256];
    	unsigned short weight;
    }
    In C++ a struct is the same as a class, except it defaults to public, whereas class defaults to private; but a lot of people use a struct instead of a class when they need a POD.

  6. #6

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    I'm a little bit confused... wouldn't a class with a member of itself be some infinitely recursive type?

    Code:
    struct Uh_Oh
    {
         struct Uh_Oh n;
    }    obj;
    
    obj.n.n.n.n ...

    EDIT:
    I just realized this didn't even happen in the code, so never mind.
    Last edited by rudyman; 06-23-2008 at 01:25 PM.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> wouldn't a class with a member of itself be some infinitely recursive type?
    Yes, that is not allowed. But nobody is doing that here, are they?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The code won't compile because Uh_Oh hasn't been declared at this point (or is in the process of being declared). But you can drop the "stuct" word:
    Code:
    struct Uh_Oh
    {
          Uh_Oh x;
    };
    In C++, we have a luxury C coders don't.
    (Or are you hijacking the thread with C code?)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That's not allowed either, Elysia. An Uh-Oh cannot contain an Uh_Oh.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, no, I didn't say it was allowed, either.
    But the "static" keyword can be dropped, which was all I implied. It doesn't matter if it's there or not.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by Elysia View Post
    A class is like an object. If you make a car, you don't expose the car's internals.
    Instead, you expose a public interface, such as a wheel or pedestals for acceleration and retardation.
    Or better yet, add a drive() method so the user doesn't have to fiddle around with wheels and pedals, which may be replaced with trackballs and levers in the future.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  2. Replies: 7
    Last Post: 05-26-2005, 10:48 AM
  3. Replies: 1
    Last Post: 04-20-2003, 05:02 PM
  4. arrays as class names
    By Katle in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2002, 03:58 PM
  5. Do you store store one off data arrays in a class?
    By blood.angel in forum C++ Programming
    Replies: 5
    Last Post: 06-24-2002, 12:05 PM