Bibliography: Visual C + + Getting Started classic seventh edition Ivor Horton eighth chapter
Learn to use the multi-file project of the class using the examples in the book.
The first thing to do is define the class Cbox as a coherent whole, write the relevant class definition in the CBox.H file, and write the code of the class function member in CBox.cpp. Include the # include "CBox.H" in CBox.cpp, while the other library functions are written in CBox.h.
Then we can define the relevant files for multiple classes.
Finally, they are organized in a ***.cpp, the file has the main function, and contains all the. h files.
Example:
In one solution there are the following files
Ex8_13.cpp
Box.cpp
Box.h
BoxOperators.h
The code snippet is as follows
//Ex8_13.cpp//Main#include <iostream>#include"Box.h"#include"BoxOperators.h"usingstd::cout;usingStd::endl;intMain () {CBox candy{1.5,1.0,1.0 }; CBox candybox{1.5,1.0,1.0 }; CBox carton{3,2.0,1.5 }; intnumcandies{Carton/Candybox}; cout<<"Numcandies is:"<< numcandies <<Endl, .......
//Box.h#pragmaOnce#include<algorithm>#include<utility>usingStd::rel_ops::operator<=;usingStd::rel_ops::operator>;usingStd::rel_ops::operator>=;usingStd::rel_ops::operator!=;classcbox{ Public: CBox (); ~CBox (); ExplicitCBox (DoubleLV =1.0,DoubleWV =1.0,DoubleHV =1.0);Private: Doublem_length; DoubleM_width; DoubleM_height; Public: //Less-than operator for CBox objects BOOL operator< (Constcbox&Abox); DoubleVolume ()Const; BOOL operator==(ConstCBox & Abox)Const; CBoxoperator+(ConstCBox &Abox); int operator/(ConstCBox & Abox)Const;};
//Box.cpp#include"Box.h"Cbox::cbox () {}cbox::~CBox () {}cbox::cbox (DoubleLvDoubleWV,DoubleHV): m_length{LV}, m_width{WV}, m_height{HV} {if(M_height >m_length) {Std::swap (m_height, m_length); Std::swap (M_width, m_height); } Else if(M_height >m_width) {Std::swap (m_height, m_width); }}DoubleCbox::volume ()Const{ returnm_length*m_height*m_width;}//Less-than operator for CBox objectsBOOLCBox::operator< (Constcbox&Abox) { returnVolume () <abox.volume ();}BOOLCBox::operator==(ConstCBox & Abox)Const{ returnVolume () = =abox.volume ();} CBox CBox::operator+(ConstCBox &Abox) { returnCBox (Std::max (M_length, Abox.m_length), Std::max (M_width, abox.m_width), M_height +abox.m_height);}intCBox::operator/(ConstCBox & Abox)Const{ returnVolume ()/abox.volume ();}
//BoxOperators.h//CBox operatoions that dont need to access private members#pragma once#include"Box.h"InlineboolOperator> (ConstDouble value,Const CBOX &Abox) {return value >Abox.volume ();} Inlinebooloperator< (ConstDouble value,Const CBOX &Abox) { return value< abox.volume ();} Inline bool operator> (const CBox & abox, const Double value) { return value < AB Ox.volume ();} Inline bool operator< ( const CBox & abox,const Double value) { return value > abox.volume ();} ............
Summarize:
The C + + console program contains the following basic types of files:
- The. h file that contains the library file # include directives, global constants and variables, class definitions, and function prototypes. In other words, the. h file contains everything except the executable code. They also contain the definition of an inline function. When there are multiple class definitions in a program, these class definitions are usually put into different. h files.
- A. cpp file that contains the program executable code, which also contains the # include directives that are required for the executable code to be defined;
- Another executable **.cpp that contains the main function.
Organization of multiple files in C + +