I have been asked to design an API that allows the user to do this and do that in c++.. What exactly is an API. In c++ is an API a .header file? Are the methods in class header file the API to allow user to interact with our .cpp file?

If someone asks, a class should have an API that allows user to calculate this, print this etc etc. then should we consider the .h file in c++ as an API.

I have been asked to.. Having a table class The aim of this class is to hold a table containing numerical data . This class should have an API that allows the user to: - Populate the table with the data from a CSV file. - Get the average of a column specified by the user.

so is the public function in my headder files an API
Please enlighten me

Thank you

Recommended Answers

All 4 Replies

The term API is a bit of a fuzzy term today. Back in the early days of computing, you had an operating system and you had applications. The company making the operating system would ship it along with a document that specified all the functions (in C) that the operating system provided and that applications could be programmed to call to perform certain tasks (e.g., opening files, opening network connections, etc..). This set of functions became known as the "Application Programming Interface" (API) because it was all the things that application programmers needed (or were allowed) to know about the operating system to be able to interact with it.

Today, the number of layers of software has greatly increased and diversified, and any typical system has a whole ecosystem to libraries, languages, interpreters, utility programs, and so on. So, the term API has taken a much more general meaning, which should really be just "interface", dropping the AP part, which was only meaningful in the simple OS vs. Apps paradigm.

So, the broad definition of what an API is is still pretty much the same as before. If you write code that you intend to package up as a library to be used by other libraries or applications, then you will naturally have to determine how the users of your library are supposed to be using it. This includes figuring out what they should know and be able to do, and what should be hidden from them (a general concept called "encapsulation"). So, an API is generally that set of things that you expose to the user as a means to use your library, and in the same stroke, the things that you, as the library implementer, can guarantee will be present and well-behaved in your library (generally, you would create documentation that specifies what the guaranteed behaviours are).

The concrete form that an API takes differs from language to language, and from library to library. There is no definitive form, especially in a multi-paradigm language like C++.

Typically, in procedural programming (e.g., C-style libraries), the API is the set of functions that are exported by the library and that are documented in the library's specifications.

In object-oriented programming, the API is the (documented) set of classes that the user can create and the (documented) set of public member functions that those classes have.

In a language like C++, the API takes a broader form since you can mix procedural style with OOP and half a dozen other programming paradigms that have their own API "shapes".

From the sound of the task that you are asked to do, the professor is probably talking about the OOP form that the API takes. In other words, he wants your class to provide a set of public member functions that allow the user of that class to perform the tasks that he listed.

In c++ is an API a header file?

No. You could have header files that are not part of your API, and you could have headers that are part of your API, and those headers might contain things that are not part of your API (things that are there, but the user is not required to know about and that you do not guarantee will be there). But generally speaking, when you distribute a C++ library, you provide the API in the form of a set of header files and an accompanying document that describes what they contain. You might not provide all the header files of your library as part of that distribution (i.e., you can have "private" headers), and you might not document all the things that your headers contain (you might want to be able to change them in the future without violating the guarantees you made to your users, so, you leave those parts out of the documentation).

So, the API is not the header files, but the header files are the vehicle for the API, in addition to the API documentation.

Are the methods in class header file the API to allow user to interact with our .cpp file?

Yes, that is one form that it takes, which is very common in object-oriented programming.

If someone asks, a class should have an API that allows user to

You have to understand that most programmers are not necessarily the most rigorous when it comes to the way they express themselves. Strictly-speaking, you shouldn't say "a class should have an API that..". A better way to put it would be to use the term "public interface", or something along those lines. A (documented) class is part of a library's API, and so is any of its (documented) public members (data or functions). Programmers tend to throw the term API everywhere, just get used to it (you have to learn to translate it in your mind to a more accurate term).

@mike Hats off to you. Great answer. One , the length of your answer, secondly your way of explanation. +1. :)

@mike, now Can I say that a library is an API? Basically, API is a set of functions/classes which you showcase to users for their use. Library includes many headers including private and public headers. Still, Can I say that a library is an API for the programmers?

Can I say that a library is an API for the programmers?

You can say that a library has an API.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.