Separating class code into header file and cpp

I am confused how to split the implementation code and declaration of a simple class into a new header and cpp file. For example, how can I separate the code for the next class?

class A2DD { private: int gx; int gy; public: A2DD(int x,int y) { gx = x; gy = y; } int getSum() { return gx + gy; } }; 
+80
c ++ oop class
Mar 06 2018-12-12T00:
source share
6 answers

The class declaration is included in the header file. It is important that you add #ifndef security elements, or if you are on an MS platform, you can also use #pragma once . I also omitted the private one; by default, C ++ class members are private.

 // A2DD.h #ifndef A2DD_H #define A2DD_H class A2DD { int gx; int gy; public: A2DD(int x,int y); int getSum(); }; #endif 

and the implementation goes in the CPP file:

 // A2DD.cpp #include "A2DD.h" A2DD::A2DD(int x,int y) { gx = x; gy = y; } int A2DD::getSum() { return gx + gy; } 
+124
Mar 06 2018-12-12T00:
source share

In general, your .h contains a defition class, which is all your data and all your method declarations. How is this in your case:

 A2DD.h: class A2DD { private: int gx; int gy; public: A2DD(int x,int y); int getSum(); }; 

And then your .cpp contains implementations of methods like this:

 A2DD.cpp: A2DD::A2DD(int x,int y) { gx = x; gy = y; } int A2DD::getSum() { return gx + gy; } 
+8
Mar 06 '12 at 8:12
source share

Basically modified function declaration / definition syntax:

a2dd.h

 class A2DD { private: int gx; int gy; public: A2DD(int x,int y); int getSum(); }; 

a2dd.cpp

 A2DD::A2DD(int x,int y) { gx = x; gy = y; } int A2DD::getSum() { return gx + gy; } 
+5
Mar 06 2018-12-12T00:
source share

A2DD.h

 class A2DD { private: int gx; int gy; public: A2DD(int x,int y); int getSum(); }; 

A2DD.cpp

  A2DD::A2DD(int x,int y) { gx = x; gy = y; } int A2DD::getSum() { return gx + gy; } 

The idea is to save all function and member signatures in a header file.
This will allow other project files to see how the class looks, without knowing the implementation.

In addition, you can include other header files in the header instead of the header. This is important because any headers included in your header file will be included (inherited) in any other file that includes your header file.

+3
Mar 06 2018-12-12T00:
source share

You leave ads in the header file:

 class A2DD { private: int gx; int gy; public: A2DD(int x,int y); // leave the declarations here int getSum(); }; 

And put the definitions in the implementation file.

 A2DD::A2DD(int x,int y) // prefix the definitions with the class name { gx = x; gy = y; } int A2DD::getSum() { return gx + gy; } 

You can mix the two (leave the getSum() definition in the header, for example). This is useful, as it gives the compiler a better chance of inlining, for example. But it also means that a change in implementation (if left in the header) may result in a rebuild of all other files containing the header.

Please note that for templates you need to save everything in the headers.

+2
Mar 06 2018-12-12T00:
source share

Usually you put only declarations and really short built-in functions in the header file:

For example:

 class A { public: A(); // only declaration in the .h unless only a short initialization list is used. inline int GetA() const { return a_; } void DoSomethingCoplex(); // only declaration private: int a_; }; 
+1
Mar 06 '12 at 8:10
source share



All Articles