Community discussion forum

Should i split the code into 2 cpp or????

  • 1 year ago

    Hi ,

    Im using VS2005. Currently i have a developed source code. Now i need to do modification and add new variable and function into it. But then the currently source code seem to be quite big and my boss would like me to split it into two if possible.

    So, im wonder how to do and it is a good idea to split or just add my new function into a new cpp? Confused

    Thanks.

    Regards,
    ioo 

     

  • 1 year ago

    Using OOP with any PLanguage like C++, help you in many ways, one is less bulks of codes. You can create a class for each concept that you think it's useful to be a class, then develop methods and variables for that and implement them. For example inheritance helps you for less coding.With OO design, Even if you have big codes, you can easily manage them, since they are separated and have no explicit connection with other parts(like methods).

    But for having how many files like .cpp and .h, with classes you will put each one in a separate file and it'll decrease your file size also. Even if you have no classes, you can logically separate methods in some .cpp and .h files. It's important to write mothods every where you can, if you code without having any methods, it will cause disasters!

    Anyway, if you now have some not classified or methodized code, it's definitely better to do that for your code now, becacuse later, you will have really big problems and maybe in a bad day, finally you get rid of that and code from scratch! To do so, first think of classes that you can create for your code, for ex : if you want to create a game, you can think of these as separated or connected or inherited classes : 'player' , 'enemy' , 'objects' that may appear in the scene, 'scoring system', 'menu', 'Artificial Intelligence', ... . Then you can easily define your class, then declare some methods. A little hard part is to separate code and then connect each part, e.g. calling a method with some params from a class, where you had previously some codes there. But after that you have many advantages, that you can see them in may articles on the web, some are : easily change codes, use codes without write them again, more understandable codes, less error prone, less sized files (your problem), and other benefits of OOP.

    Modern Planguages (like C# , java , other .NET langs) are going closer and closer to be fully Object Oriented, and that really makes programming easier.

    Now it's your choice, take a class or what! Wink 

  • 1 year ago

    Hi,

    Currently my code class is like below:


    **under hpp**
    namaspace X
    {

    Class A
    {
        some variables....
    }

    Class B
    {
       some variables....
    }

    Class  C
    {
       some variables...
    }

    Class D
    {
       some variables...
      
        func_1
        func_2
        func_3
        .
        .
        func_n

        class A   a
        class B   b
        class C   c
    }

    }

    **under cpp**

    using namespace X;

    bool D::func_1()
    {
    ................
    }

     bool D::func_2(class A a, int i)
    {
    ...............
    }

    .............. other functions from class D........
    ............

    ************

    so.. all functions is actually from Class D. Is that mean i should move some of the function into new class? what should i do? SadSad

  • 1 year ago

    This shows that your class is huge and you're doing many things with that. You can do followings to make that smaller :

    - Move some of functions to other classes(existing or new ones).
    - Use inheritance.

  • 1 year ago

    Thanks for your reply. I appreciate your help. And here still have some question which im not sure, hope you can help me again. 

    **under hpp**
    namaspace X
    {

    Class A
    {
        some variables....
    }

    Class B
    {
       some variables....
    }

    Class  C
    {
       some variables...
    }

    Class D
    {
       some variables...
      
        func_1
       // func_2   (this function move to Class F)
        func_3
        .
        .
        func_n

        class A   a
        class B   b
        class C   c
    }

    Class F   //new class
    {
       move some of the function from Class D over here...
       func_2
       func_a    ///own function
       func_b

       class D  d      <---  is this needed? inheritance?
    }

    }

    **under cpp**
    using namespace X;
    bool D::func_1()
    {
    ................
    }

    //bool D::func_2(class A a, int i)
    //{
    //...............
    //}

    .............. other functions from class D........
    ............

    ************


    **new cpp file**
    using namespace X;
    bool F::func_a()
    {
    .........
    }

    bool F::func_2(class A a, int i)
    {
    ...............
    }
    ************

    Hope this is clear enough for you to understand. Hope for your reply too. Thank you.

  • 1 year ago

    Don't use an instance of class D (class D  d) in F, instead :

    - Inherit F from D.

    - Or if it has no relation to class D, use an instance of F in D (if there's some members in D that just F uses them, cut them from D to F).

Post a reply

Enter your message below

Sign in or Join us (it's free).

Want to stay in touch with what's going on? Follow us on twitter!