-1

I am reading book How to program by Paul Deitel. As per him we can make a file which does not contain member function details.

"To hide the class’s member-function implementation details, the class-implementation programmer would provide the client-code programmer with the header Time.h (which specifies the class’s interface and data members) and the Time object code (i.e., the machine-code instructions that represent Time’s member functions). The client-code programmer is not given Time.cpp, so the client remains unaware of how Time’s member functions are implemented."

I have developed below simple code in dev to test this concept but I don't know after compiling which file is the equivalent to cpp file.

Area Class:

#include<iostream>
using namespace std;
class Area {

    public:
        int square(int);

    private:
        int x;  
};

Source Code Area.cpp which contains member function functionality

#include<iostream>
#ifndef AREA_HH
#define AREA_HH
# include "Area.h"

int Area::square(int x)
{
    return x*x;
}

#endif

Main:

#include<iostream>
#include "Area.h"

using namespace std;

int main ()

{
    Area Obj1;

    cout<<Obj1.square(4);

    return 0;
}

The Files created by Dev are: Area.cpp, Area.o,Area.h,main.cpp,main.o,Makefile.win,layout and exe.

Shabbar
  • 33
  • 2
  • 1
    `Area.cpp` is the file that has the implementation. – drescherjm Mar 26 '18 at 16:27
  • Using an hpp header doesn't help with distributing object code though, which is what the question seems to be asking for. – Useless Mar 26 '18 at 16:51
  • @Useless, yes, I know. That's why I commented and didn't provide this as an answer, as I know it is not an answer to the question, but rather a comment on the topic at hand. I thought it could be valuable information since this is c++ code and the OP is obviously looking at distributing a library of sorts. I'm going to delete my comment since I should have said that if it is `c`, use `.h` and if there's `cpp` in it, use `.hpp` But, here's a good resource on the topic. https://stackoverflow.com/questions/152555/h-or-hpp-for-your-class-definitions – adprocas Mar 26 '18 at 16:54
  • There's certainly no problem preferring `.hpp` to `.h` for C++ headers, and it's probably a good suggestion. – Useless Mar 26 '18 at 16:57
  • 2
    You really ought to avoid `using namespace std` - it is a bad habit to get into, and [can silently change the meaning of your program](/q/1452721) when you're not expecting it. Get used to using the namespace prefix (`std` is intentionally very short), or importing *just the names you need* into the *smallest reasonable scope*. It's especially pernicious in header files, as you now inflict the problem on every source file that includes the header! – Toby Speight Mar 26 '18 at 17:29
  • You also have no need to include `` in the header or in `Area.cpp`. – Toby Speight Mar 26 '18 at 17:30

3 Answers3

1

I don't know after compiling which file is the equivalent to cpp file.

It's the file that implements the functions that are declared in the the .h file. In your case, it is "Area.cpp".

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • If I want to share the class with someone else but I dont want to show code of member functions which is written in Area.cpp file then which file if have to share instead of Area.cpp – Shabbar Mar 26 '18 at 16:33
  • @Shabbar, you have to share the .h file. You'll also have to share a library that contains the compiled code of Area.cpp. – R Sahu Mar 26 '18 at 16:34
  • when ever I try to compile the code with out main i.e only Area.h and Area.cpp it gives error undefined reference to WinMain. I am confused how can I make a library that contains compiled code Area.cpp – Shabbar Mar 26 '18 at 16:42
  • @Shabbar, please lookup the documentation of your dev environment on "how to create a library". – R Sahu Mar 26 '18 at 16:49
  • I think I must have misunderstood the question if the cpp file is equivalent to the cpp file! – Useless Mar 26 '18 at 16:57
1

You cannot isolate a class from an executable. To share functionality like that you have to build a library instead of an application. To understand why you can't find a single self-contained file that holds your class when building your project you have to understand how building works.

The standard structure of a C++ application sees functionality split across translation units (.cpp files). Those files hold the definitions of functions. In order for those functions to be used from other translation units, the function declarations need to be visible to those units. Because we do not want to include the entirety of every .cpp file we need, we most commonly separate the declarations into a header file (.h or .hpp). For example:

Circle.h :

// This is where you would put an include guard (NOT IN THE CPP FILE)

struct Circle {
    // Note that member variables need to be visible in the header
    float radius;
    float area();
}

Circle.cpp

float Circle::area() {
    return 2.0 * PI * radius;
}

main.cpp

#include "Circle.h"

int main() {
    Circle c;
    c.radius = 5.0;
    float area = c.area();
}

When the project is built, the compiler will output an object file for each translation unit, eg Circle.o and main.o. Because the compiler works on one translation unit (cpp) at a time, they are not self-contained. For example, in main.o there is a reference to c.area() that has not been resolved. Because the signature of the function was available through the included .h file, the compiler know how to call it but not how to find it. The linker will take all object files and link them into a single executable, eg MyProject.exe.

So in a standard C++ application, there is no "equivalent to cpp file". The intermediate .o is not intended to be shared and included in other projects and MyProject.exe is just an executable.

What you actually need to do is build a library. That is a form of executable that is not a standalone application. Then the final output of the linker is going to be a .dll, .lib, .so or .a (depending on platform and method of linking) which is going to hold all your function implementations in executable (not source) form. Then you can distribute the library alongside your .h files and let people link to that.

Again, there is no "equivalent of cpp" file. The built library will contain all translation units that you specify as inputs because the build process is very similar to the above. The difference is that the output will not be an executable application but rather a library from which functions will be called as needed from another application that links against this library.

There is no unified way to build a library. As you're using an IDE, it's probably going to be an option while creating a new project.

patatahooligan
  • 3,111
  • 1
  • 18
  • 27
  • A `.a` library is roughly an archive of `.o` files, it's not a type of executable. If you share a `.a` lib, you're pretty directly sharing your object files, and they do usually correspond 1:1 with source files. A `.so` _may_ be executable, but is not required to be. – Useless Mar 26 '18 at 16:53
  • Thanks alot for your detailed answer. – Shabbar Mar 26 '18 at 17:11
0

The implementation source code is Area.cpp, and this is compiled to the implementation object code in file Area.o.

If you want to distribute your Area library, you would create a library (Area.a, Area.so, Area.dll or Area.lib, depending on platform and link-style choices), and distribute that along with Area.h.


The general rule is any source file %.cpp is compiled to the object file %.o.

Note you already know perfectly well that Area.h and Area.cpp are inputs to the compiler (because you wrote them yourself), and the file timestamps will show which of the files you listed were created by the compiler, and you have a Makefile which either specifies exactly which file is built from which source, or describes the pattern relating input and output filenames ...

Useless
  • 64,155
  • 6
  • 88
  • 132
  • How can I create these files (Area.a, Area.so, Area.dll) using dev thanks for your prompt responses – Shabbar Mar 26 '18 at 16:45
  • 1
    Look up the documentation for your compiler/IDE: if you can't find it there, it's a new question. – Useless Mar 26 '18 at 16:48