Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I can understand that headers are used to include classes and functions into another file, but .cpp just seem a bit useless to me.
.h files are used to declare functions that can be exported/imported, however, template functions and classes must be defined in the same .h file and not only declared.
I have only used .cpp files as of now for the main program, but can they be used for any other thing? I am using .h files for everything and just .cpp files for the int main() function
Question: What are the cases where you use .cpp files over .h files
Here's an example of how to organize your code. Define headers for any classes you're using, like:
// example.h
#pragma once // Avoid duplicate imports, if supported by your compiler
#include <string>
class Example {
public:
Example();
Example(const char* name);
~Example();
protected:
std::string name;
};
Then implement it:
#include "example.h"
Example::Example() {
}
Example::Example(const char* name_) : name(name_) {
}
Example::~Example() {
}
Then in main.cpp:
#include "example"
int main() {
Example e("Some Name");
return 0;
}
This will all change for the better when modules become the best way to declare things, but until then, this is how it's typically done.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
insertItem (value_type )
{
}
Most code functions i have used so far have been along the lines of
insertItem( value_type var) Which allows me to access the data through var. In these new functions the only thing inside the parathesis is a object type. I cant access an object by saying only its type.
The functions in the header files do not use any variable names so i cant add to the functions in my implementation
No, you're wrong in saying the above statement. It is optional to name the parameters in the declarations(in header files) of the member functions. If you choose to not name the parameters in the declarations in header files, you can still name the parameter in the implementation files. One such example to get you started is shown below:
header.h
#pragma once
#include <string>
class Name
{
public:
//setter. Note here you can skip naming the parameter
void setName(std::string);
//constructor
Name(std::string); //note that here also we can skip naming the parameter
private:
std::string name;
};
source.cpp
#include "header.h"
//note here in implementation we've named the member function parameter to be p
void Name::setName(std::string p)
{
name = p;
}
//here in implementation we've named the constructor parameter to be p
Name::Name(std::string p): name(p)
{
}
main.cpp
#include <iostream>
#include<string>
#include "header.h"
int main()
{
Name n1("anoop");//this uses constructor
n1.setName("rana");//this uses the setter setName
return 0;
The output of the above program can be seen here
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I researched this question on Google and Stak Overflow but couldn't find an answer to it.
I am trying to find out what all information is stored in a .cpp file extension. Meaning, is it just code that has been compiled (meaning compiled code)? Does have an object file in it? Does it include an object in it? What exactly does it consist of?
The .h usually has the class definition (code)
#ifndef CLASS_T_H
#define CLASS_T_H
class class_t {
public:
class_t();
class_t(const class_t& o);
~class_t();
class_t& operator=(const class_t& o);
private:
};
#endif
And the .cpp usually has the class implementation (code)
#include "class_t.h"
class_t::class_t() {
}
class_t::class_t(const class_t& o) {
}
class_t::~class_t() {
}
class_t& class_t::operator=(const class_t& o) {
return *this;
}
You could use the class_t in another .cpp file by inclduing the .h file and compiling the cpp files into a binary executable. One of the cpp files would contain your main() method.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Hello i want to create my own public c++ tool like auto socket connection. but i have two ways and i want to know which way is better.
way 1 :
insert the functions (with source) inside one header file and other people just include the header file and use the functions...
sock.h
#include <iostream>
class sock {
public:
bool create();
bool bind();
bool listen();
}
inline bool sock::create() { ... }
inline bool sock::bind() { ... }
inline bool sock::listen() { ... }
and second way is create a header file and also create static lib (c++)
sock.h
#include <iostream>
class sock {
public:
bool create();
bool bind();
bool listen();
}
sock.cpp
#include "sock.h"
bool sock::create() { ... }
bool sock::bind() { ... }
bool sock::listen() { ... }
which way is better ? ( i choose the first way myself because it's easy for client to just include one header file and use the functions but in second way client must include the header and also include the lib file. is there any problem for first way?
Functionally there is no problem. But by keeping the function definitions inside the header, every time the header is imported into a source file, all of the definitions will be compiled again, negatively impacting both compile time and binary size (in certain cases).
Best practice is to define only once, forward declare wherever possible. Keep your abstractions water tight.
Unless you're using template classes, in which case header definitions are generally required.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Take the following example:
// base.h
#include <string>
struct base
{
virtual ~base() = default;
virtual void do_something(const std::string& arg) const = 0;
};
// derived.h
struct derived : base
{
void do_something(const std::string& arg) const
{
//...
}
};
In this example should derived.h include the string header?
I fully agree with the principle of including what you use but in this case base.h has to include string and if the interface changes to not use string (and the include <string> is accordingly removed from base.h) then the interface will break highlighting anyways.
If the interface changes not to include string, your derived do_something will have to change as well. If you need in the derived.h independently of the interface (e.g. due to details unrelated to interface but present in the implementation) - in this case yes, it's valid and probably preferable to include <string> there as well.
I take the approach of always includeing anything I need for that file. I then don't have to go around hunting through headers and untangling webs of dependencies when I'm missing a symbol somewhere. Include guards mean that this is cheap.
But it's completely up to you. If you want to omit it, to make derived.cpp shorter, and add it in at a later date if the need arises, that's fine too.
There's simply no "right" answer.
You can include "base.h". Don't think too much.The less code you write,the better your code is.
#include "base.h"
struct derived :public base
{
void do_something(const std::string& arg) const
{
//...
}
};
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am learing c++
All I want to know is I made a three file in code blocks using GCC compiler main.cpp printing.h printing.cpp
And I want to print the text in function "printer" in class "printingarea" in the file "printing.cpp" and want to prototype it in "printing.h" and want to call in "main.cpp"
Based on Claudios answer:
printing.h
class PrintingArea{
public:
void printer();
};
printing.cpp
#include "printing.h"
#include <iostream>
void PrintingArea::printer() {
std::cout << "Hello, World !";
}
main.cpp
#include "printing.h"
int main()
{
PrintingArea pa;
pa.printer();
return 0;
}
As you see, you define the prototype of the class in the header, and the implementation of the methods in the cpp.