240 likes | 373 Vues
Explore the differences between monolithic and modular file structures in C++ programming. This article outlines the benefits of using a modular approach, highlighting its advantages for organization, ease of maintenance, and code reusability. By examining prototype header files, function definitions, and the implementation across multiple files, we demonstrate how a well-structured project enhances collaboration and scalability. With practical examples such as the greet function, learn how to effectively manage your C++ files for better project outcomes.
E N D
Monolithic vs Modular • multiple files now • main driver file • prototypes header file(s) • functions definition implementation source file(s) • one file before • system includes • main driver function • prototypes • function definitions
greet.h #ifndef GREET_H #define GREET_H #include <iostream>using namespace std; #endif main.cpp #include <iostream>using namespace std; int main(){ greetings(); return 0;}void greetings() {cout << "Hello world!" << endl; return;} void greetings();
greet.h #ifndef GREET_H #define GREET_H #include <iostream>using namespace std;void greetings(); #endif main.cpp #include <iostream> #include “greet.h”using namespace std; int main(){ greetings(); return 0;} greet.cpp #include "greet.h" void greetings() {cout << "Hello world!" << endl; return;}
greet.h #ifndef GREET_H #define GREET_H #include <iostream>using namespace std;void greetings(); #endif main.cpp #include <iostream> #include “greet.h”using namespace std; int main(){ greetings(); return 0;} greet.cpp #include "greet.h" void greetings() {cout << "Hello world!" << endl; return;}
Main File main.cpp // Author: Clayton Price // File: main.cpp // This file drives the greet // function. #include "greet.h" #include <iostream> using namespace std; int main(){ greetings(); cout << “Good bye” << endl; return 0;} comment header system and user includes namespace declaration main function
Main File main.cpp // Author: Clayton Price // File: main.cpp // This file drives the greet // function. #include "greet.h" #include <iostream> using namespace std; int main(){ greetings(); cout << “Good bye” << endl; return 0;} comment header system and user includes namespace declaration main function
Main File main.cpp // Author: Clayton Price // File: main.cpp // This file drives the greet // function. #include "greet.h" #include <iostream> using namespace std; int main(){ greetings(); cout << “Good bye” << endl; return 0;} comment header system and user includes namespace declaration main function
Main File main.cpp // Author: Clayton Price // File: main.cpp // This file drives the greet // function. #include "greet.h" #include <iostream> using namespace std; int main(){ greetings(); cout << “Good bye” << endl; return 0;} comment header system and user includes namespace declaration main function
Main File main.cpp // Author: Clayton Price // File: main.cpp // This file drives the greet // function. #include "greet.h" #include <iostream> using namespace std; int main(){ greetings(); cout << “Good bye” << endl; return 0;} comment header system and user includes namespace declaration main function
Implementation File greet.cpp // Author: Clayton Price // File: greet.cpp // This file provides the // greetings functionality #include "greet.h" void greetings() {cout << "Hello world!" << endl; return;} comment header user include function definitions
Implementation File greet.cpp // Author: Clayton Price // File: greet.cpp // This file provides the // greetings functionality #include "greet.h" void greetings() {cout << "Hello world!" << endl; return;} comment header user include function definitions
Implementation File greet.cpp // Author: Clayton Price // File: greet.cpp // This file provides the // greetings functionality #include "greet.h" void greetings() {cout << "Hello world!" << endl; return;} comment header user include function definitions
Implementation File greet.cpp // Author: Clayton Price // File: greet.cpp // This file provides the // greetings functionality #include "greet.h" void greetings() {cout << "Hello world!" << endl; return;} comment header user include function definitions
Header File greet.h // Author: Clayton Price // File: greet.h // This file contains prototype // info for the greet program. // // Function: void greetings(); // This function sends a hello // message to the user through // standard output #ifndef GREET_H #define GREET_H #include <iostream>using namespace std;void greetings(); #endif comment header preprocessor directives system and user includes namespace declaration function prototypes
Header File greet.h // Author: Clayton Price // File: greet.h // This file contains prototype // info for the greet program. // // Function: void greetings(); // This function sends a hello // message to the user through // standard output #ifndef GREET_H #define GREET_H #include <iostream>using namespace std;void greetings(); #endif comment header preprocessor directives system and user includes namespace declaration function prototypes
Header File greet.h // Author: Clayton Price // File: greet.h // This file contains prototype // info for the greet program. // // Function: void greetings(); // This function sends a hello // message to the user through // standard output #ifndef GREET_H #define GREET_H #include <iostream>using namespace std;void greetings(); #endif comment header preprocessor directives system and user includes namespace declaration function prototypes
Header File greet.h // Author: Clayton Price // File: greet.h // This file contains prototype // info for the greet program. // // Function: void greetings(); // This function sends a hello // message to the user through // standard output #ifndef GREET_H #define GREET_H #include <iostream>using namespace std;void greetings(); #endif comment header preprocessor directives system and user includes namespace declaration function prototypes
Header File greet.h // Author: Clayton Price // File: greet.h // This file contains prototype // info for the greet program. // // Function: void greetings(); // This function sends a hello // message to the user through // standard output #ifndef GREET_H #define GREET_H #include <iostream>using namespace std;void greetings(); #endif comment header preprocessor directives system and user includes namespace declaration function prototypes
Header File greet.h // Author: Clayton Price // File: greet.h // This file contains prototype // info for the greet program. // // Function: void greetings(); // This function sends a hello // message to the user through // standard output #ifndef GREET_H #define GREET_H #include <iostream>using namespace std;void greetings(); #endif comment header preprocessor directives system and user includes namespace declaration function prototypes
Header File greet.h // Author: Clayton Price // File: greet.h // This file contains prototype // info for the greet program. // // Function: void greetings(); // This function sends a hello // message to the user through // standard output #ifndef GREET_H #define GREET_H #include <iostream>using namespace std; #include “greet.cpp”void greetings(); #endif comment header preprocessor directives system and user includes namespace declaration function prototypes do not do this!
Compiling Single File g++ -W –Wall –s –pedantic-errors prog.cpp –o my_prog Multiple Files by Name g++ -W –Wall –s –pedantic-errors treeFarm.cpp treeFarmFunctions.cpp –o trees Multiple Files by Wildcard g++ -W –Wall –s –pedantic-errors *.cpp –o trees
Motivation greet.h Compiled greet.cpp #ifndef GREET_H #define GREET_H #include <iostream>using namespace std;void greetings(); #endif 001010101001101010010100101010100110101001010010101010011010100101001010101001101010010100101010100110101001010010101010011010100101001010101001101010010100101010100110101001010010101010011010100101001010101001101010010100101010100110101001010010101010011010100 Security Portability Versitiliy Extendability