Subclasses

This is a discussion on Subclasses within the C++ Programming forums, part of the General Programming Boards category; Are there such a thing? I am wanting to create a class "car" that contains many classes. Could I do ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    241

    Subclasses

    Are there such a thing?

    I am wanting to create a class "car" that contains many classes. Could I do this, if so how.
    I would like to address it like car.engine.size()

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Yep, something like -

    Code:
    class car
    {
    public:
    	class engine
    	{
    	public:
    		int hp;
    		int cc;
        }the_engine;
    	//more stuff
    
    };
    
    int main()
    {
    
    	car beast;
    	beast.the_engine.hp=15000;
    
    	return 0;	
    }

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    241
    Thanks, I thought it might be something like that

  4. #4
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    i don't think so sorenson, it'd be more like this:

    Code:
    class engine
    {
     public:
     int hp, cc /*etc.*/;
    }
    
    class car
    {
     public:
     engine theEngine;
     //other stuff.
    }
    what you did may work, but i prefer doing it this way.

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >what you did may work, but i prefer doing it this way

    Of course it'll work. If engines are only used within cars then I see no reason not to do what I have done, as long as engine is a simple class and they are only required by cars, it would be possible to prevent orphaned engines being created.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors and subclasses
    By Siphon in forum C++ Programming
    Replies: 5
    Last Post: 01-06-2007, 10:51 AM
  2. Classes, Subclasses & Dynamic Memory
    By CaeZaR in forum C++ Programming
    Replies: 6
    Last Post: 02-06-2006, 06:07 PM
  3. Need Help with Inheritance assignment
    By mejv3 in forum C++ Programming
    Replies: 6
    Last Post: 11-08-2005, 12:56 AM
  4. virtual functions and poniters to subclasses
    By Asagohan in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2005, 10:52 AM
  5. Replies: 3
    Last Post: 07-23-2005, 09:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21