Thread: Project Layout

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    11

    Project Layout

    Hi, can you guys tell me if this is the right way to write or layout the codes? It's just a simple project and I finally got it to work. I just don't know if this is the right way to lay things out...

    Code:
    //SIMPLE CALCULATOR
    
    //In main.cpp
    
    #include <cstdlib>
    #include <iostream>
    #include "header2.h"
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int choice;
        cout << "Please select an option from the menu:\n\n";
        cout << "1. Add\n";
        cout << "2. Subtract\n";
        cout << "3. Multiply\n";
        cout << "4. Divide\n";
        cout << "\nEnter choice:  ";
        cin >> choice;
        cout << "\n\n\n\n";
        switch(choice)
        {
                      case 1:  myFunc();
                           break;
                      case 2:  myFunc2();
                           break;
                      case 3:  myFunc3();
                           break;
                      case 4:  myFunc4();
                           break;
                      default:  cout << "Please select one of the options in the menu.\n";
                                break;
                      }    
        cout << "\n\n\n\n";
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    
    //In header2.h
    //This is where I list my functions
    
    #include <cstdlib>
    #include <iostream>
    
    int myFunc();
    int myFunc2();
    int myFunc3();
    int myFunc4();
    
    
    
    //In header1.h
    //The class
    
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    class Add
    {
          public:
                 int getAnswer() const {return one + two;}
                 void setAnswer(int a, int b) {one = a, two = b;}
                           
          private:
                  int one;
                  int two;
          };    
         
    class Subtract
    {
         public:
                 int getsubAnswer() const {return subone - subtwo;}
                 void setsubAnswer(int a, int b) {subone = a, subtwo = b;}
                           
          private:
                  int subone;
                  int subtwo; 
          };
    
    class Multiply
    {
         public:
                 int getmultAnswer() const {return multone * multtwo;}
                 void setmultAnswer(int a, int b) {multone = a, multtwo = b;}
                           
          private:
                  int multone;
                  int multtwo; 
          };
    
    class Divide
    {
         public:
                 int getdivAnswer() const {return divtone / divtwo;}
                 void setdivAnswer(int a, int b) {divtone = a, divtwo = b;}
                           
          private:
                  int divtone;
                  int divtwo; 
          };
    
    
    
    //In myFunc.cpp
    //for addition
    
    #include <cstdlib>
    #include <iostream>
    #include "header1.h"
    using namespace std;
    
    int myFunc()
    {
        int numbOne, numbTwo;
        
        cout << "Enter a number:  ";
        cin >> numbOne;
        cout << "Enter another number:  ";
        cin >> numbTwo;
        
        Add myAdd;
        myAdd.setAnswer(numbOne, numbTwo);
        cout << "Answer:  " << myAdd.getAnswer() << "\n\n"; 
        
        return 0;
        }
    
    
    
    //In myFunc2.cpp
    //For subtraction
    
    #include <cstdlib>
    #include <iostream>
    #include "header1.h"
    using namespace std;
    
    int myFunc2()
    {
        int numbOne, numbTwo;
        
        cout << "Enter a number:  ";
        cin >> numbOne;
        cout << "Enter another number:  ";
        cin >> numbTwo;
        
        Subtract mySubtract;
        mySubtract.setsubAnswer(numbOne, numbTwo);
        cout << "Answer:  " << mySubtract.getsubAnswer() << "\n\n"; 
        
        return 0;
        }
    
    
    
    //In myFunc3.cpp
    //For multiplication
    
    #include <cstdlib>
    #include <iostream>
    #include "header1.h"
    using namespace std;
    
    int myFunc3()
    {
        int numbOne, numbTwo;
        
        cout << "Enter a number:  ";
        cin >> numbOne;
        cout << "Enter another number:  ";
        cin >> numbTwo;
        
        Multiply myMultiply;
        myMultiply.setmultAnswer(numbOne, numbTwo);
        cout << "Answer:  " << myMultiply.getmultAnswer() << "\n\n"; 
        
        return 0;
        }
    
    
    
    //In myFunc4.cpp
    //For division
    //here I forgot to set the variables as float - i'll do different next time
    
    #include <cstdlib>
    #include <iostream>
    #include "header1.h"
    using namespace std;
    
    int myFunc4()
    {
        int numbOne, numbTwo;
        
        cout << "Enter a number:  ";
        cin >> numbOne;
        cout << "Enter another number:  ";
        cin >> numbTwo;
        
        Divide myDivide;
        myDivide.setdivAnswer(numbOne, numbTwo);
        cout << "Answer:  " << myDivide.getdivAnswer() << "\n\n"; 
        
        return 0;
        }

    I set these codes on separate files and linked them.

    Is this how I should be laying out the codes? And if you find anything to correct, I'd really appreciate it. Thanks!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That method seems fine. I might put all the function implementations into a single cpp file instead of separating them.

    You will want to add header include guards to all of your header files, though.

    Also, since your functions do not use the return value, I would make them specify void instead of int as the return type and remove the return statements.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    11
    Thanks! That really helped. =)

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I actually prefer style
    Code:
    int main()
    {
    	//some statements
    }
    so the open and closing brekets are in the same column - it is easier for me to identify the start and the end of the block. I havn't see the approach where the closing breket is leaved in the indented column as you did.
    The second widly use alternative is like this
    Code:
    int main(){
    	//some statments
    }
    It saves one line but makes for me alittle bit difficult to look for the open breket...
    But also it doesn't hide the closing breket under the block but places it on the previous indentation.

    PS. And tab size is set to 4 in my editor... (so I'm not using spaces to indent the code)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    What I just noted in your program. Your Subtract class does not have a constructor. What will happen if someone calls getsubAnswer without calling set function?
    I think it is better to provide default constructor setting your members to 0 and maybe the constructor accepting 2 ints, so the class can be constructed and directly used without call the set function...
    The same about other classes
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Good point. Given the way the classes are being used, I think that only providing a two argument constructor and no default constructor might be more appropriate.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    11
    >>I actually prefer style
    Code:
    int main()
    {
    	//some statements
    }

    Thanks! but that is the default indentation layout of DevC++. I also like that kind of style you showed. I'll probably change my settings. I'll also take note of your second suggestion. Actually, honestly, and being a noob, I don't think I fully understand what the Constructor and Destructor is for... but I'll study on it.
    Last edited by lasher; 12-03-2006 at 11:18 PM.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    no default constructor might be more appropriate.
    Till we do not make vectors of the calss object or something calling the default constructor without us realizing it completely...
    I prefer to build the private default constructor in this case so the compiler will announce me if I miss that point in my code
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I don't think I fully understand what the Constructor and Destructor is for.
    When I mentioned in the other thread that constructors are important even for beginners' classes, this is a good example of what I was talking about. If you or somebody else is using your classes, and you forget to call setAnswer, then the code will compiler but you will get garbage results.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Maybe it's because I'm coming from java but I din't like that way. I would use main.cpp for main routine and global functions and than per class one <classname>.h file declaring that class with privat and public members and one <classname>.cpp file with the implementation (skipped in your case because all methods are inline). I guess with that source navigation is much easier in bigger projects.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Thanks! but that is the default indentation layout of DevC++. I also like that kind of style you showed.
    http://en.wikipedia.org/wiki/One_True_Brace_Style
    Which one you choose is largely a matter of preference.

    So long as you apply your style consistently, then it's all good
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    I think there was a thread ages ago on this board about the alignment of braces and it started off as a simple question simular to the above and it turned into a 50 reply debate amongst board members giving their preferences on what style to adapt. Quite an intreeging read I thought.

    PS: Considering it was me who began that thread, I did not mean to begin such a heated debate to0!

    Look back at the older threads to see just how argumentive a simple subject can turn!
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM