Thread: Seperating interface and implementation

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Seperating interface and implementation

    Hi,

    I am hoping for some general advice on how to create a user interface, by use of a processor class.

    The program I am creating uses inheritance.

    I am creating the following functionalities:

    addACar();
    deleteACar();
    editACar();
    searchForACar();
    loadCarsToCSVFile();
    saveCarsToCSVFile();
    exitTheProgram();
    viewNextCar();
    viewPreviousCar();
    showHelp();

    Until now I have only ever used a single main.cpp and a single base.h file.

    My question is where should I locate these functions so that I create a user interface using a processor class.

    I thought about it for a while, and came up with the following:

    Functions to be implemented within main.cpp:

    exitTheProgram();
    viewNextCar();
    viewPreviousCar();
    showHelp();


    Functions to be interfaced in 'base.h' and implemented within 'base.cpp':

    addACar();
    deleteACar();
    editACar();
    searchForACar();
    loadCarsToCSVFile();
    saveCarsToCSVFile();


    The reason I am somewhat confused is because of reasons such as the following:

    1) Surely exitTheProgram(); should completely be within main() right? No part of it needs to be inside base.h or base.cpp.

    All the function does is call exit(0);

    2) viewNextCar(); and viewPreviousCar(); will iterate though a std::list. So should these functions we located within main() to? There is no use of constructors/destructors.

    //-------------------

    Am I wrong with this do you think?

    Should EVERY function have an interface inside base.h?

    Or should I go ahead and place (for example) exitTheProgram(); within main() ?

    I understand it's difficult to give specific advice without seeing the program, but if anyone can help clarify how one should implement a processor class and create a 'single' user interface that would be brilliant.

    I've read the pages I've found in Google describing inheritance, virtual member functions etc, but I'm still confused about the general method of creating one.

    Many thanks!
    Last edited by Swerve; 01-27-2010 at 11:58 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I havent really read your post, but the title of your thread is exactly what MVC is for. So if you actually meant what you put in the title, then it is certainly a great place to start learning about and applying some of its concepts to your task.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Main() should be such that reading it should tell you what the program does. So if some code is particularly complicated and isolated, it should be separated into another function. So showHelp() is probably wordy enough that it is useful to separate it. exitTheProgram() sounds like it would just call exit(0), which makes it a pointless function, since main can call exit(0) or return 0 and there is no loss of clarity.

    As for separating source files: C++ is generally written with an object oriented model. Each separable component of the program is written to be an object. In your case both Car and CarDatabase would make idea classes.

    Each class has it's own .cpp and .h file pair. Each class has a role, and the methods facilitate that one role. So a CarDatabase might store and list through cars. But editing a car would probably be a property of the car itself. This kind of design contains how source data is separated.

    For other free functions, like showHelp, whether it should be in a seperate file or not depends largely on the source length. Having a large source files makes source control difficult. Obviously it makes sense to group similar functions together.

    You also asked if every function should have a prototype in a header file. I'd say any non static function should have a prototype, and no reusable function should be static. Obviously if a function is called from another file it cannot be static, and must be prototyped.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 06-17-2009, 04:20 PM
  2. How to get RSSI value, send to sensor, sensor receive package, repackage it?
    By techissue2008 in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-04-2009, 10:13 AM
  3. interface implementation
    By George2 in forum C# Programming
    Replies: 2
    Last Post: 05-04-2008, 12:46 AM
  4. interface, implementation, & application files
    By trongsi in forum C++ Programming
    Replies: 4
    Last Post: 04-08-2006, 02:11 PM
  5. Replies: 2
    Last Post: 02-11-2002, 01:03 PM