Thread: Problems defining classes

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    37

    Unhappy Problems defining classes

    Hello everyone!

    I'm trying to do a program where you are supposed to simulate an automobile registry which stocks vehicles information(license number,model,brand,etc.) and their respective owners' information(name,address,id,etc.).
    In the automobile registry I'll have the possiblities to insert vehicles and owners,erase,modify and search for vehicles and owners
    I did a class for vehicles and their information and another class for owners and their information,but I'm not quite sure If I should make another class for the automobile registry or if that registry will only be the main interface with the options referred above.
    Below are the classes(feel free to add comments or suggest modifications!):
    Code:
    class owners
    {
    private:
    
      string name;
      string address;
      long id;
      long phone;
      vehicles v;
    
    public:
    
      owners(string o_name,string o_address, long o_id,long o_phone);//constructor
      void getname (void) const;
      string setname(string o_name);
      //etc,other gets and sets 
    }
    
    class vehicles
     {
    private:
    
      string model;
      string brand;
      string license;
      long nr_oldowners;
      owners o;
    
    public:
    
      vehicles(string v_model,string v_brand,string v_license,long v_nr_oldowners)
      void getlicense (void) const;
      string setlicensestring o_name);
      o.setname(string o_name);
      //etc,other gets and sets 
    }
    Does this make any sense at all?Any help appreciated!Thanks in advance!

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Your question is not very clear. Does the code work fine for you?

  3. #3
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Yeah, I'm not really following either, but you do need ;'s after your closing }'s

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I think another class for the registry makes sense.

    In your current code, an owners has vehicles and a vehicles has owners. The classes should be owner and vehicle singular, not plural. Each should represent a single owner or vehicle. Also, you shouldn't have each contain the other. This won't work as you have it anyway. If you want, you can have each contain references to the other, or you can just have your automobile registry class manage that.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    DaveD makes a very good point. You can NOT have class A contain class B where class B contains class A - it would be infinitely large for one thing.

    The second thing is that you can certainly have one person own more than one vehicle - so you need to take this into account [at least, you do if you want this to work in a REAL situation - if you're just doing this as a school excercise you may want to clarify if the requirements assume that there is at most one vehicle per owner].

    There are several ways to do this. One of the simpler is to use a linked list of vehicles pointed to by the owner. You probably also want a pointer (or some other type of reference) to the owner for the vehicle, so that you can easily do a lookup of the owner from a registration number or other vehicle ID).

    I take it this isn't supposed to be very realistic: In real life, vehicles are identified by a VIN (Vehicle Identification Number). This NEVER changes - but people can change registration number (in many countries at least).

    In the real world, the previous owner is also kept on register (at least in England).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    37

    ...

    I've made the automobile registry class with their atributes and functions but I have a few questions...Why can't class a have class b and vice versa?If we were to make another class for the owner's birthday couldn't we include it on the owner's class since there's an association with both?
    Also,I'm a bit confused,if the automobile registry manages both vehicles and cars(inserts,erases,updates,etc.) is there a need for a constructor on both vehicles and owners,since there are already constructors on autoreg that receive the owners' and vehicles' properties?
    And answering someone who has asked,yes,a owner can have multiple vehicles but a vehicle has one owner only,but could have had before multiple owners.
    Any help is appreciated and thakns for the help!

    Code:
    class owner
    {
    private:
    
      string name;
      string address;
      long id;
      long phone;
    
    public:
    
      owner(string o_name,string o_address, long o_id,long o_phone);//constructor
      void getname (void) const;
      string setname(string o_name);
      //etc,other gets and sets 
    }
    
    class vehicle
     {
    private:
    
      string model;
      string brand;
      string license;
      long nr_oldowners;
    
    public:
    
      vehicle(string v_model,string v_brand,string v_license,long v_nr_oldowner)
      void getlicense (void) const;
      string setlicensestring o_name);
      //etc,other gets and sets 
    }
    
    class autoreg
    {
    
    private:
    
      vehicle v;
      owner o;
    
    public:
    
         insert_v(string v_model,string v_brand,string v_license,long v_nr_oldowner);//is this okay??
    
         insert_o(string v_model,string v_brand,string v_license,long_nr_oldowner,string o_name,string o_address, long o_id,long o_phone););//is this okay??
    
         erase_o(string o_name,string o_address, long o_id,long o_phone););//is this okay??
    
         erase_v(string v_model,string v_brand,string v_license,long v_nr_oldowner););//is this okay??
    
         update(string v_model,string v_brand,string v_license,long_nr_oldowner,string o_name,string o_address, long o_id,long o_phone););//is this okay??
       
    }

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't think that's how I would do the class design.

    Assuming we have two vehicles owned by the same person, that SHOULD be THE SAME OBJECT - not two independent copies [say you change the address for example, you don't want to have to change the address twice, do you?]

    Let's explain why class A can't have class B inside it when you have class A in class B. If we write it out, you'll see:
    Code:
    class A {
       class B {
          ... 
          class A {
            class B {
              ... 
              class A {
                class B  {
                ...
                  class A  ... 
                  // This continues forever and ever. 
                }
             }
           }
         }
       }
    };
    Essentially, that is an infinite recursion problem, and it can not be solved with a finite number of recursions or iterations - the structure itself would take an infinite amount of memory.

    What you can do is have mutual pointers:
    Code:
    class B;  // We need to tell the compiler that THERE IS a class B. 
    class A {
       ... 
       B *b;
    };
    
    class B {
       ... 
       A *a;
    };
    Since the compiler doesn't have to infinitely recurse to build the data structure, we are fine.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    37

    ...

    But is it wrong to have the class autoreg like I defined (containg vehicle v and owner o)?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by esmeco View Post
    But is it wrong to have the class autoreg like I defined (containg vehicle v and owner o)?
    You mean aside from the fact that if you have two vehicles by the same owner, you need to keep two copies of the same owner, which I think is the wrong solution.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    So,if I understood you correctly,since a owner can have multiple vehicles we should have a pointer in both classes?Something like this?:

    Code:
    class owner
    {
    private:
    
      string name;
      string address;
      long id;
      long phone;
    
    public:
    
      owner(string o_name,string o_address, long o_id,long o_phone);//constructor
      void getname (void) const;
      string setname(string o_name);
      //etc,other gets and sets 
    }
    
    class vehicle
     {
    private:
    
      string model;
      string brand;
      string license;
      long nr_oldowners;
    
    public:
    
      vehicle(string v_model,string v_brand,string v_license,long v_nr_oldowner)
      void getlicense (void) const;
      string setlicensestring o_name);
      //etc,other gets and sets 
    }
    
    class autoreg
    {
    
    private:
    
      vehicle *v;
      owner  *o;
    
    public:
    
         insert_v(string v_model,string v_brand,string v_license,long v_nr_oldowner);//is this okay??
    
         insert_o(string v_model,string v_brand,string v_license,long_nr_oldowner,string o_name,string o_address, long o_id,long o_phone););//is this okay??
    
         erase_o(string o_name,string o_address, long o_id,long o_phone););//is this okay??
    
         erase_v(string v_model,string v_brand,string v_license,long v_nr_oldowner););//is this okay??
    
         update(string v_model,string v_brand,string v_license,long_nr_oldowner,string o_name,string o_address, long o_id,long o_phone););//is this okay??
       
    }

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, that would make more sense.

    Technically, vehicle could be constant part of the registry - there's no registration without a vehicle, only the owner may be shared between multiple vehicles.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    You mean the
    Code:
    insert_v(string v_model,string v_brand,string v_license,long v_nr_oldowner)
    method,making it constant?

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by esmeco View Post
    You mean the
    Code:
    insert_v(string v_model,string v_brand,string v_license,long v_nr_oldowner)
    method,making it constant?
    Not quite: just replace vehicle *v with vehicle v; this means that the vehicls is part of the registry, raher than something pointed to by the registry.

    Each registration concerns exactly and always one unique vehicle. The owner may or may not be unique, so you need a way to refer to an owner from more than one registration. This means that it has to be a pointer or some other type of reference [you could keep all owners in an array and keep the index of the owner in the registry, for example - but I think a pointer is better].

    [Edit]Also, I would probably just make functions to get and set things, rather than functions that take a lot of parameters and do lots of things.

    If you maintain the existing model with a pointer to a vehicle, you could even us a method "setVehicle(vehicle *av);" and "setOwner(owner *ao);". [/edit]

    --
    Mats
    Last edited by matsp; 10-11-2007 at 08:25 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    Could this class also be done like this with array,instead of pointer(to use later with linked lists)?

    Code:
    class autoreg
    {
    
    private:
    
      vector <vehicle> v;
      vector <owner>  o;
    
    public:
    
         insert_v(string v_model,string v_brand,string v_license,long v_nr_oldowner);//is this okay??
    
         insert_o(string v_model,string v_brand,string v_license,long_nr_oldowner,string o_name,string o_address, long o_id,long o_phone););//is this okay??
    
         erase_o(string o_name,string o_address, long o_id,long o_phone););//is this okay??
    
         erase_v(string v_model,string v_brand,string v_license,long v_nr_oldowner););//is this okay??
    
         update(string v_model,string v_brand,string v_license,long_nr_oldowner,string o_name,string o_address, long o_id,long o_phone););//is this okay??
       
    }

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, you need a way to connect a vehicle to an owner. Your current class will be able to hold a number of owners and a number of cars - but there's no direct connection [of course, if you insert them together, they would be at the same index both of them - but hardly a practical arrangement to connect them].

    But a lot of it depends on how you are actually going to use the data.

    My approach is mostly based on the fundamental principles in database design - and I don't think that's a bad thing, because what you are doing is essentialy a database. In database design, the principle is that you only hold something in one place, and then use an ID to refer to it someplace else. So in these terms, the owner would have an ID, and then the registry would refer to the owner by the ID, and the vehicle would have another ID, and a the ID would be stored in the registry.

    But in C/C++, you may use a pointer as a more direct way to access the owner - it's still the same principle - one owner stored somewhere, referenced by the connection to the vehicle in the registry. If you hold the vehicle itself in the registry or not is a different matter.

    Does this make sense to you?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. College Classes
    By nubby in forum C Programming
    Replies: 2
    Last Post: 10-07-2007, 12:32 AM
  2. Inheritance, overriding, problems
    By CodeMonkey in forum C++ Programming
    Replies: 8
    Last Post: 01-04-2007, 01:26 AM
  3. classes, inheritance and include problems
    By baniakjr in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2006, 01:45 PM
  4. C++ Classes: Use, Misuse...Confusion.
    By Snorpy_Py in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2006, 01:46 AM
  5. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 02:57 PM