How is a C++ Program broken down?

This is a discussion on How is a C++ Program broken down? within the C++ Programming forums, part of the General Programming Boards category; Is it just one big file of code or do you use many different files of code and "call" them ...

  1. #1
    Registered User Fool's Avatar
    Join Date
    Aug 2001
    Posts
    335

    How is a C++ Program broken down?

    Is it just one big file of code or do you use many different files of code and "call" them in or something when needed? If anyone could point me to a link with any info about the setup of projects or anything I would appreciate it.

  2. #2
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189
    Well, it depends on the size of the project, of course.

    The most basic programs obviously are nothing but a single .cc file.

    Slightly more complicated programs structure things out, usually creating multiple .h files (containing declarations and maybe implementations of short classes/functions) and corresponding .cc file (implementations only), only one of which has a main() function. Some sort of project manager (depending on your compiler) separatately compiles each .cc and links them all together at the end. Of course, the files are dependent and one class may need to know of the existence of structures and functions in the other; so you use #include to include the declarations in the .h files.

    The largest projects are structured similarly to this. From my experience working as an intern developing part of a large (8 year effort so far) software package in C++, each major class (there may be hundreds or more) is given its own .h and .cc files; the .h contains the class declaration and the .cc contains all the implementations. Related .h and .cc files are grouped in modules; for each of these modules there is a corresponding templates file containing explicit instantiations for templates needed by those classes (automatic instantiation is too slow for large projects). Since you typically have to include hundreds of files, there are also files created that are updated daily that save your compiler some of the effort of constantly recompiling low-level code. There is also of course required documentation for each class, and a required testing function. This particular project documents all its structure on the web; if you're curious check out http://www.aips2.nrao.edu/docs/aips/aips.html. The project code organization standards (the long version of what I wrote) are at http://www.aips2.nrao.edu/docs/refer...ng/node29.html.
    Last edited by Procyon; 09-28-2001 at 06:30 PM.

  3. #3
    Registered User Fool's Avatar
    Join Date
    Aug 2001
    Posts
    335
    Wow, nice run down. That makes alot of sence. I've heard of classes, declarations, and fuctions but I'm not sure what each means and where they would be found in a program. Using this code...
    Code:
    #include < stdio.h > 
    #include < iostream.h > 
    #include < conio.h > 
    
    int main() 
    { 
    cout<<"Hello World!"; 
    getch(); 
    return 0; 
    }
    ...would each of these be?

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    How is a C++ program broken down? Like most of us, I suppose. No specific thing, just the wearisome sameness, the endless repetition, the pressure to make choices or "else", trying to keep our private members out of the public. Then the same thing the next day, no matter how hard we "run". It's no wonder the programs break down.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    The program below is actually a cpp file with a main() function, and is sometimes called a driver file/program. Only one cpp file in each program can have a main() function. This is the code your write. Your compiler/project software/IDE turns this file into a program that (likely) has an associated header file (which is probably a default file) somewhere and, depending on your compiler, probably has a bunch of other files associated with it too, like .ide, .obj, .mak, .csm, .exe, etc. which are generated by the compiler/project software when it changes the stuff you write into an executable file. Many compilers/progect managers/IDEs have switches to tell it what type of file you want it to make (.exe vs .dll vs lib) etc., but these options usually are used only after you have mastered the basic syntax of C/C++.

    The following code lines are all precompiled header files container (primarily) function declarations and some inline functions. You can create your own header files and include them here, too. Each header file (usually) is associated with an associated .c or .cpp or .cc file or library file (.lib extension) (.dll files fit in here someplace too) that contain the function definitions for all the functions listed in the header files. Note you don't have to (and shouldn't) list any of the associated cpp file for the header files included in this program.
    --------------------------------------------------------------------------------#include < stdio.h >
    #include < iostream.h >
    #include < conio.h >

    ----------------------------------------------------------------------
    //here's the code to actually run the program
    int main()
    {
    cout<<"Hello World!";
    getch();
    return 0;
    }

    This program has no user defined functions or classes/structs/types. If it did, the user defined function prototypes and class declarations (or other user defined types like enums, typedefs, structs, etc.) would routinely be listed after the includes and before main(), unless they were compiled into a separate header so they could be reused in another project someday. The function definitions and class methods are commonly placed after main(), although you will see them between the includes and before main(), but after any pertinent declarations/prototypes, too. Either way works and is more a personal style than anything. If the function and class declarations are in a header file, the definitions will usually be in the associated cpp file (although they could be in a .lib or .dll file too).

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4

    welll

    well the #includes are the Header files which hold all the prototypes of the meathods you are going to call apon

    eg to use the cout function you have to have #include <iostream>
    other wise the compiler dosent know what your talking about.

    and as for the getch();, its not even needed... try it with out that

  7. #7
    Registered User Fool's Avatar
    Join Date
    Aug 2001
    Posts
    335
    So if for example, I was creating a math test program (which would ask you random math questions). I could create a file called "Numbers.h" and declare the numbers? Example
    Code:
    N1=2
    N2=4
    N3=6
    So I could just add this header file ("Numbers.h") into the preprocessor and it would automaticly know what I mean when I say "N1+N2" and so on?

    Is the function the part of int main()? Is that the only function or are there more (it's the only thing I've seen so far, still new )? What what is inside the { } have to be declared under the #included header file for it to compile and execute?

    Thanks for the help!

  8. #8
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    not quite although I think you can use a keyword like extern and that might work. Anyway that is not the way to do it.

    You know how functions work? You declare a prototype than you write the main program, than your function definitions follow:

    void function(int);

    int main()
    {
    int num=3;
    function(num);
    return 0;
    }

    void function(int x)
    {
    cout << ++x;
    }

    Okay for this nonsense program you could do this:

    nonsense.h
    Code:
    #ifndef GUARD_nonsense_h
    #define GUARD_nonsense_h
    void function(int);
    #endif

    nonesense.cpp
    Code:
    #include nonsense.h
    #include<iostream.h>
    void function(int x)
    {
       cout << ++x;
    }
    main.cpp
    Code:
    #include nonsense.h
    #include<iostream.h>
    
    int main()
    {
       int num=3;
       function(num);
       return 0;
    }
    This is more or less the way it is done although this program is nonsense
    I compile code with:
    Visual Studio.NET beta2

  9. #9
    Registered User Fool's Avatar
    Join Date
    Aug 2001
    Posts
    335
    Originally posted by guest
    #include < stdio.h >
    #include < iostream.h >
    #include < conio.h >

    //here's the code to actually run the program
    int main()
    {
    cout<<"Hello World!";
    getch();
    return 0;
    }
    I tried to compile and run that in MSVC++ but it wouldn't run. I figured out that I needed the < stdafx.h > file for it to work. Anybody know why?

  10. #10
    Registered User Fool's Avatar
    Join Date
    Aug 2001
    Posts
    335
    But Dean, I don't understand where the nonesense.cpp file comes into play. You don't call it in or something? Kind of like a frames page in HTML?

  11. #11
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    You need <stdafx> in all your Win32 console projects. The nonsense.cpp file is compiled along with main.cpp and nonsense.h. It is just the definition of the function. Separate compilation makes more sense when you use classes. A .cpp file is dedicated to one objects definition. It's only used for large programs.
    I compile code with:
    Visual Studio.NET beta2

  12. #12
    Registered User Fool's Avatar
    Join Date
    Aug 2001
    Posts
    335
    I just get confused when talking about classes and fuctions. Can you show me an example of each (within a program)? You know, what does it look like and what do they do?

  13. #13
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    I won't write a whole program. I have to get a couple hrs sleep right away, but the difference between classes and function is that classes combine functions and the data values that relate to the function. Infact the functions are now known as methods.

    class Fool
    {
    //accessor available to instances of class Fool
    public:
    double Height();
    double Weight();
    string FavorateGamesSystem();

    //accessor available to methods only
    private:
    double height;
    double weight;
    string gamesystem("Playstation2");
    };

    This is not a great example but the purpose of the class is to describe the object. When you declare an instances of the class in main:
    Fool person1;
    Fool person2;
    They are Fool objects.
    cout << person1.Height();
    This would give the fools height. But this function has not been defined yet. The definition would go into a file called fool.cpp and the class itself would belong to fool.h
    I compile code with:
    Visual Studio.NET beta2

  14. #14
    Unregistered
    Guest
    Classes are like the built in data types (int, float, etc), except you create them yourself.
    A header file for a class contains the class declaration, which contains the data and function prototypes associated with that class, along with settings like private, public, etc. Data & functions declared private can only be accessed by objects of the class.
    There's a .cpp (in msvc) file associated with the class header file that contains the definitions for the class functions. The .cpp file also often contains default initializations for the data members
    This file has the class header file as an #include statement, along with any others it may need, such as <iostream>.
    A separate .cpp file, often called the driver, contains int main(), which often consists largely of declaring objects (int x, myClass z, etc) and calling functions of those classes to do most of the work. This file has the class header file as an #include statement as well, but doesn't have the class .cpp file as an #include.
    However, all these files are compiled together to make up an .exe file. In msvc, this is done by creating them all in a project, or adding them to one.
    The reason for separate files, besides helping debug, is to keep as much of the details of the program as possible hidden from other parts, so it can't be messed around with as much by other parts of the program or other programs. It also helps reuse and modify classes for other uses.
    All three files can be written as one .cpp file. Classes are like nouns, functions like verbs, even though classes contain functions and functions contain data. Classes "are" things, objects like x of int x; or z of myClass z; Functions do things to and with objects.
    Hope that helps. I'm still learning, maybe got a detail or two off, but that's the idea.

  15. #15
    Unregistered
    Guest
    #include <iostream.h>

    //class declaration--contains two data members and two methods

    class Fraction
    {
    private:
    int Numerator;
    int Denominator;

    public:
    Fraction(int, int);
    void display();
    }

    //function declaration
    void getInput(int &, int &);

    //driver portion of program
    int main()
    {
    //declare local variables
    int a, b;

    //initialize local variables
    a = 0;
    b = 0;

    //use local variables in function, they may be changed in function
    getInput(a, b);

    //declare a fraction according to user input
    Fraction fraction(a, b);

    //now display the fraction using user input
    fraction.display();

    //end of driver function
    return 0;
    }

    //Now show function definitions
    //the first two could be placed in a cpp file correspond to the h file
    Fraction::Fraction(int num, int den)
    {
    Numerator = num;
    Denominator = den;
    }

    void Fraction::display()
    {
    cout << Numerator << "/" << Denominator << endl;
    }

    void getInput(int & A, int & B)
    {
    cout << "enter numerator:" << endl;
    cin >> A;
    cout << "enter denominator:" << endl;
    cin >> B;
    }

    =====================================
    =====================================
    If you did this with the multiple file thingy:

    ////create a header file to hold the class declaration. Is reuseable
    //fraction.h
    class Fraction
    {
    private:
    int Numerator;
    int Denominator;

    public:
    Fraction(int, int);
    void display();
    }

    ////create a cpp file to hold the class method definitions
    //fraction.cpp

    #include "fraction.h"
    #include <iostream.h>

    Fraction::Fraction(int num, int den)
    {
    Numerator = num;
    Denominator = den;
    }

    void Fraction::display()
    {
    cout << Numerator << "/" << Denominator << endl;
    }


    //now use the files in the driver program

    #include <iostream.h>
    #include "fraction.h"///this will automatically bring in fraction.cpp

    //function declaration
    void getInput(int &, int &);

    //driver portion of program
    int main()
    {
    //declare local variables
    int a, b;

    //initialize local variables
    a = 0;
    b = 0;

    //use local variables in function, they may be changed in function
    getInput(a, b);

    //declare a fraction according to user input
    Fraction fraction(a, b);

    //now display the fraction using user input
    fraction.display();

    //end of driver function
    return 0;
    }

    void getInput(int & A, int & B)
    {
    cout << "enter numerator:" << endl;
    cin >> A;
    cout << "enter denominator:" << endl;
    cin >> B;
    }


    This is a simple example, focusing on declaring a class and using it in a program with and without placing the class in it's own header file for reuse. The program using methods and a user defined function. It could (should) be much more sophisticated, but that would distract from the purpose of the example.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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