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

  • Home
  • SEARCH
  • 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 6988565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:05:53+00:00 2026-05-27T19:05:53+00:00

I’ve been reading different articles and tutorials on Header files. I understand that Headers

  • 0

I’ve been reading different articles and tutorials on Header files.
I understand that Headers serve for the purpose of keeping the “interface” from the implementation. ( and other things like some compile optimization )

What I still don’t get, and really can’t wrap my mind around it, is do you always use headers?
I know you can write blocks of code within a header file itself. But that is where I get lost.

When I look at a video tutorial, people just define functions with their body within the Header file. Then a different article only defines the functions ( I guess that is the idea of an interface).

Just for now I’m making a simple class named Color.
implementation:

/* 
* File:   Color.cpp
* Author: Sidar
* 
* Created on 26 december 2011, 16:02
*/

#include <stdio.h>

#include "Color.h"

Color::Color() {

 reset();
}

Color::Color(const Color& orig) {

 a = orig.a;
 r = orig.r;
 g = orig.g;
 b = orig.b;
}

void Color::reset()
{
    a = 0;
    r = 0;
    g = 0;
    b = 0;
}

 Color::Color(unsigned int r, unsigned int g, unsigned int b, unsigned int a)
 {
   this->r = r;
   this->g = g;
   this->b = b;
   this->a = a;
 }

Color::~Color() {
   r = 0;
   g = 0;
   b = 0;
 }

 //getters____________________________
 unsigned int Color::getRed() const
 {
   return r;
 }

 unsigned int Color::getBlue() const
 {
   return b;
 }

 unsigned int Color::getGreen() const
 {
    return g;
 }

 unsigned int Color::getAlpha() const
 {
   return a;
 }

 //setters____________________________

 void Color::setRed(unsigned int r)
 {
   if(r > 255)r = 255;
   if(r < 0)r = 0;

   this->r = r;
}


void Color::setGreen(unsigned int g)
{
  if(g > 255)g = 255;
  if(g < 0)g = 0;

  this->g = g;
}

 void Color::setBlue(unsigned int b)
{
   if(b > 255)b = 255;
  if(b < 0)b = 0;

  this->b = b;
}

void Color::setAlpha(unsigned int a)
{
 if(a > 255)a = 255;
 if(a < 0)a = 0;

 this->a = a;
 }

 unsigned int Color::color()
 {
   return (int)a << 24 | (int)r << 16 | (int)g << 8 | (int)b << 0;
  }

and here the header

/* 
 * File:   Color.h
 * Author: Sidar
 *
 * Created on 26 december 2011, 16:02
 */

 #ifndef COLOR_H
#define COLOR_H
#include <string>

class Color {
public:

    Color();
    Color(const Color& orig);
    Color(unsigned int r,unsigned int g,unsigned int b, unsigned int a);

    virtual ~Color();
    //____________________
    void setRed(unsigned int r);
    unsigned int getRed()const;
    //____________________  
    void setBlue(unsigned int b);
    unsigned int getBlue()const;
    //____________________
    void setGreen(unsigned int g);
    unsigned int getGreen()const;
    //____________________
    void setAlpha(unsigned int a);
    unsigned int getAlpha()const;
    //____________________
    unsigned int color();

   void reset();

private:

    unsigned int r;
    unsigned int b;
    unsigned int g;
    unsigned int a;


};

#endif  /* COLOR_H */

This code does work, I’m not getting any errors. But is this the general idea of headers and cpp files?
And my second question:
I read a lot that when using Templates it’s easier to just implement the code within the Header I understand this(to prevent many implementations for something that is suppose to be so generic). But are there any other situations as well?

  • 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-05-27T19:05:54+00:00Added an answer on May 27, 2026 at 7:05 pm

    You don’t “always” do anything, it all depends on the circumstances, your goals, and the coding standards of your group or organization.

    C++ is a very flexible language that allows things to be done and organized in many different ways.

    Some reasons separate implementation files might be used:

    1. To keep the implementation separated from the interface, as you
      suggest

    2. To speed up compile times

    3. To handle cyclic dependencies

    4. So you can ship a binary library with only the header files and not
      the underlying source code

    Some reasons why you might not want separate implementation files:

    1. You use templates, which “usually” have to be defined with the
      declarations

    2. You DON’T want the implementation separated from the interface. In
      many cases this makes things easier to understand as you don’t have
      to flip back and forth between header and implementation file. This
      can be counterproductive if you’re dealing with large classes with
      many methods, though.

    3. You want as much of your code to be inlined by the compiler as
      possible.

    4. You are creating a code library, for which you don’t want the user
      to have to worry about building. Most of the Boost libraries are
      this way, where you don’t have to use the Boost build system, which
      can be quite a chore, but instead you just include the header files
      in your code, and that’s all you need to do to use them.


    I usually begin the work on a new class by defining all of the logic within the header file. And then later when the class is complete, or when it starts to get crowded in the header file, I will start moving the logic out into a separate implementation file. This is strictly for making the most of my time, as I am able to get things done faster and with fewer bugs when I can see everything in the same file.

    It should also be noted that you don’t necessarily have to use header files at all. You can define some classes directly in your .cpp files. This is often done for private classes that will never be used outside of that .cpp file.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I need a function that will clean a strings' special characters. I do NOT

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.