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 6170829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:06:29+00:00 2026-05-23T23:06:29+00:00

I have a question about the relationship between header and implementation files in C++.

  • 0

I have a question about the relationship between header and implementation files in C++.

I have received two empty header files and one empty implementation file. When I say “empty” I mean that the files only contain method declarations; I have to implement the code inside each method. (I thought that posting the code for the three files would be redundant, since they only contain declarations..)

The three files are:

  • interface_complexmutex.h
  • complexmutex.h
  • complexmutex.cpp

interface_complexmutex.h has virtual methods for complexmutex. Also, the complexmutex class inherits from interface_complexmutex.

These files have basic mutex functions that I have to implement. I have 2 questions:

  1. Why do I need interface_complexmutex.h? It does not really have a .cpp file, and the entire implementation will be in complexmutex files, right?

  2. How should I include these files and use complexmutex class in a program? (if I have the init() method in complexmutex.cpp, for example)

    Should main.cpp look like this?

    #include <stdlib.h>
    #include <stdio.h>
    #include "interface_complexmutex.h"
    #include "complexmutex.h"
    using namespace std;
    int main(){
    
        complexmutex a;
    
        /* do something with mutex to test */
    
        return 0;
    }
    

    I made my own Makefile, and the code above seems to run fine so far.

  • 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-23T23:06:30+00:00Added an answer on May 23, 2026 at 11:06 pm

    1) You need an interface only if it is likely that the implementation will change later on. That will leave you free to make all the changes you want to the internals of your code without impacting the people using it to create software. That means that the users should only see the interface class and never the implementation.

    To do that you’ll need to implement what is called a factory pattern. This is a function that will create an instance of the implementation class and return it as a pointer (or reference) to the interface class. You probably can write that function as a static method of your interface class.

    2) In a program you should image you are the user of your code. The user doesn’t care how you implement your feature, so it should not use the implementation at all. If possible it should only include the interface header and call the factory method like this:

     #include "interface_complexmutex.h"
     using namespace std;
    
     int main(...) {
         interface_complemutex* mutex = interface_complexmutex::create_new(...);
         // do something interesting with the mutex
     }
    

    In this example, create_new is your factory method. You can have parameters for it (usually an enumeration) if you have different implementations.

    When using interfaces (actually abstract classes in C++), you need to use pointers or references. You can’t create values for abstract classes. Of course, if you write test code, it’s a bit different because test code is not meant to be seen/used by the users of your library.

    Notes:

    Let me point out that the naming is quite bad. The interface should be named complexmutex and the implementation complexmutex_impl since users should use the interface more, its name should be shorter (and you can replace the “impl” by something relevant such as “pthread” if you use pthreads to implement your mutex class.

    As a PS note, in C++0x there is a thread library including mutex available. It is supported by some compilers already.

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

Sidebar

Related Questions

I have a question about how to set up the relations between two models
I have a question about using streams in .NET to load files from disk.
I have a question about best practices regarding how one should approach storing complex
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and
I have a question about locking. This doesn't have to be only about record
I have a question about how to deploy WPF application into a PC without
I have a question about using os.execvp in Python. I have the following bit
I have a question about using new[] . Imagine this: Object.SomeProperty = new[] {string1,
I have a question about this question . I posted a reply there but
I have a question about tables in MySQL. I'm currently making a website where

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.