Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8491177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:20:25+00:00 2026-06-10T22:20:25+00:00

Possible Duplicate: In C++ why have header files and cpp files? I was reading

  • 0

Possible Duplicate:
In C++ why have header files and cpp files?

I was reading some KDE tutorials for creating basic plasma widgets and other QT stuff. One thing that I noticed is almost all the programs has a .h and .cpp with header file included in the cpp file.

The basic thing that I get is to minimize the clutter in the file and make the code more readable.

My question is what things should I put in a .h file and .cpp file writing a program and how it is going to benefit me in improving my code.

For example – I’ve created 3 files , add.h, add.cpp and pass.cpp – Now add.h has the function while other 2 have definition.

Add.h

#include <iostream>

using namespace std;

int add (int x,int y);

Add.cpp

#include "add.h"

int main() {

    add (int x, int y) {

    int z = x+y;

    cout<<"Add is "<<"\t"<<z;

    }
return 0;
}

Pass.cpp

#include "add.h"


add (3,4);

However this doesn’t work , judging from the answer here Why have header files and .cpp files in C++?

contents of add.h are automatically copied yet I get several errors.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:20:26+00:00Added an answer on June 10, 2026 at 10:20 pm

    Write all the functions completely in your ".cpp" file and write it first of all in your program.
    In your ".h" file, write just the proptype, that is the declarations of your functions. Make this ".h" file at the end of your program. And don’t forget to write a semi-colon(;) at the end of each function prototype.

    This is a good programming practice and it gives you the benefit of writing the code once in these separate files and using their code across your entire project or any other number of files.

    Suppose you have a file "myProgram.cpp" and myProgram.h, you can just include this one line at the beginning of any program to use its code.
    #include "myProgram.cpp".

    It works well when you have a number of functions and declarations in your file. If you have a small piece of code, there is no need of creating these separate files.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: In C++ why have header files and cpp files? I don't quite
Possible Duplicate: Why does this integer division yield 0? I have a C header
Possible Duplicate: Distance between geopoints I have some latitudes and longitudes and i need
Possible Duplicate: C++ Header order My question is about includes in .h files. Is
Possible Duplicate: C program without header I have been studying C for a long
Possible Duplicates: [C] Header per source file. In C++ why have header files and
Possible Duplicate: .NET & Tab-Delimited files I have a list that has 2 columns:
Possible Duplicate: PHP error: Cannot modify header information – headers already sent I have
Possible Duplicate: <header> vs. <head> Do I have to use <div class=header> as <header>
Possible Duplicate: I have an array of integers, how do I use each one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.