Vous êtes sur la page 1sur 11

Multi-le C++ Programs

CS1044

Wednesday, April 24, 13

Single File C Programs


So far all of the programs we have looked at in this class have lived in a single le As your programs become more complex it can be cumbersome to put all of the code into a single le Eventually youll want to, or need to, put your C++ code in multiple les Ideally, this makes logically breaks down your code further, making it easier understand

Wednesday, April 24, 13

Single C++ File Example


#include <iostream> // includes double average(int a, int b); // prototypes // main int main() { cout << average(100, 200); return 0; } // function definitions double average(int a, int b) { return (a + b) / 2; }
Wednesday, April 24, 13

Multiple C++ Files


But lets say you wanted to implement your own vector type or some other domain specic 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 le is not ideal, or even structures like in Project 3 Another example: a program with 10,000 lines in a single le, can be broken down by functionality
Wednesday, April 24, 13

Multiple C++ Files


For more interesting C++ programs, the code is typically organized into a collection of header les and cpp les Header les end in .h i.e. myle.h and cpp les look like just main.cpp In most cases, the header les contain only type declarations and function prototypes, while the cpp les contain the corresponding implementations.

Wednesday, April 24, 13

Multiple C++ Files


Youve been #includeing <iostream> since almost day one When writing large C++ fortunately, you can also #include your own les Like we mentioned earlier .h les contain declarations Let say we are writing our own vector type and weve stored in myvector.cpp and myvector.h In main: #include myvector.h, notice the rather than <>
Wednesday, April 24, 13

Typical C++ Program Organization


// main.cpp #include<iostream> #include A.h #include B.h int main() { A(); B(); return 0; } // A.h #include<iostream> void A(); // B.h #include<iostream> void B();

// A.cpp #include<iostream> #include A.h void A() { ... }

// B.cpp #include<iostream> #include B.h void B() { ... }

Wednesday, April 24, 13

Multiple Inclusion Problem


#include directives are used within both .h and .cpp les to import declarations as necessary. However, there is potential danger:
// main.cpp #include<iostream> #include A.h #include B.h int main() { A(); B(); return 0; }
Wednesday, April 24, 13

// A.h #include<iostream> #include B.h void A();

As written we will have multiple copies of the stuff in B.h

This will cause errors // B.h #include<iostream> void B();

Multiple Inclusion Problem


However, there is a simple x, by adding 3 lines to all of your .h les
// main.cpp #include<iostream> #include A.h #include B.h int main() { A(); B(); return 0; }
Wednesday, April 24, 13

// A.h #ifndef A_H #dene A_H #include<iostream> #include B.h void A(); #endif

// B.h #ifndef B_H #dene B_H #include<iostream> void B(); #endif

Multiple Inclusion Problem


These 3 lines are the the same in pretty much every .h le except the bold part is different:
// A.h #ifndef A_H #dene A_H #include<iostream> #include B.h void A(); #endif // B.h #ifndef B_H #dene B_H #include<iostream> void B(); #endif

The convention is to just capitalize the le name and replace the . with _
Wednesday, April 24, 13

A Final Example
//main.cpp #include <iostream> #include "rFibonacci.h" using namespace std; int main() { for (int i = 0; i < 50; i++) { cout << rFibonacci(i)) << endl; } return 0; } // rFibonacci.cpp #include "rFibonacci.h" int rFibonacci(int n) { if ( n < 2 ) return 1; return rFibonacci(n - 1) + rFibonacci(n - 2); } int rFibonacci(int n); #endif // rFibonacci.h #ifndef RFIBONACCI_H #define RFIBONACCI_H

Wednesday, April 24, 13

Vous aimerez peut-être aussi