Simple organization of C + + multi-source files
Basically the same as C, except that the C + + method is declared in the class. See a simple example.
Information publicly available in the Ainimal.h class.
Click ( here) to collapse or open
- #ifndef _animal_h__
- #define _animal_h__
- #include <iostream>
- using namespace Std;
- Class Animal{
- Private:
- string name;
- Public:
- void print(void);
- Animal(string name){This->name=name;}
- };
- #endif
The specifics of the implementation of the methods in the Animal.cpp class, or the hidden parts, I have added a private function extra_info,static decoration for this file.
Click ( here) to collapse or open
- #include "Animal.h"
- Static string Extra_info(){
- return "Adding info from Extra_info";
- }
- void Animal:: Print(void){
- cout << name << Endl;
- cout << Extra_info() << Endl;
- }
Main.cpp is of course the user of this class.
Click ( here) to collapse or open
- #include "Animal.h"
- int main(void){
- Animal ani("any");
- Ani. Print();
- cout << "some" << Endl;
- }
Compile
Click ( here) to collapse or open
- G++ animal. cpp main. cpp
Run
Click ( here) to collapse or open
- ./a.out
Output
Click ( here) to collapse or open
- Any
- Adding info from Extra_info
- Some
Well, three files, a class of the head, a class implementation, a user
Good luck
Simple organization of C + + multi-source files