Multi c File

Embed Size (px)

Citation preview

  • 7/22/2019 Multi c File

    1/11

    Multi-file C++ ProgramsCS1044

    Wednesday, April 24, 13

  • 7/22/2019 Multi c File

    2/11

    Single File C Programs

    So far all of the programs we have looked at in this

    class have lived in a single file

    As your programs become more complex it can becumbersome to put all of the code into a single file

    Eventually youll want to, or need to, put your C++

    code in multiple filesIdeally, this makes logically breaks down your code

    further, making it easier understand

    Wednesday, April 24, 13

  • 7/22/2019 Multi c File

    3/11

    Single C++ File Example

    #include // includes

    doubleaverage(inta, intb); // prototypes

    // mainintmain(){ cout

  • 7/22/2019 Multi c File

    4/11

    Multiple C++ Files

    But lets say you wanted to implement your own vector

    type or some other domain specific data structure

    Ideally youd write it once and then use it over and over

    again, not rewrite it every time

    So embedding complex and generic data types into

    your main.cpp file is not ideal, or even structures like inProject 3

    Another example: a program with 10,000 lines in a

    single file, can be broken down by functionality

    Wednesday, April 24, 13

  • 7/22/2019 Multi c File

    5/11

    Multiple C++ Files

    For more interesting C++ programs, the code is

    typically organized into a collection of header files and

    cpp files

    Header files end in .hi.e. myfile.hand cpp files

    look like just main.cpp

    In most cases, the header filescontain only typedeclarations and function prototypes, while the cpp

    filescontain the corresponding implementations.

    Wednesday, April 24, 13

  • 7/22/2019 Multi c File

    6/11

    Multiple C++ Files

    Youve been #include

    ing since almost day

    one

    When writing large C++ fortunately, you can also#includeyour own files

    Like we mentioned earlier .h files contain declarations

    Let say we are writing our own vector type and wevestored in myvector.cppand myvector.h

    In main:#include myvector.h, notice the rather

    than

    Wednesday, April 24, 13

  • 7/22/2019 Multi c File

    7/11

    Typical C++ Program

    Organization

    Wednesday, April 24, 13

  • 7/22/2019 Multi c File

    8/11

    Multiple Inclusion Problem

    #includedirectives are used within both .hand .cpp

    files to import declarations as necessary.

    However, there is potential danger:

    As written we will have multiple

    copies of the stuff in B.h

    This will cause errors

    Wednesday, April 24, 13

  • 7/22/2019 Multi c File

    9/11

    Multiple Inclusion Problem

    However, there is a simple fix, by adding 3 lines to all

    of your .h files

    Wednesday, April 24, 13

  • 7/22/2019 Multi c File

    10/11

    Multiple Inclusion Problem

    These 3 lines are the the same in pretty much every .h

    file except the bold part is different:

    The convention is to just capitalize the file name and

    replace the . with _Wednesday, April 24, 13

  • 7/22/2019 Multi c File

    11/11

    A Final Example

    Wednesday April 24 13