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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:31:01+00:00 2026-05-13T17:31:01+00:00

Can someone please give me an example of how public and private headers work?

  • 0

Can someone please give me an example of how public and private headers work? I have done some reading on the net but I can’t seem to find much useful information with sample codes. I was advised that I should use private headers to separate the public and private parts of my code for creating a static library. After some reading I have a general idea of how it works, but would really appreciate a good example to get me started. Specifically, what I don’t quite understand is how to put the interface functions in my public header, and the private variables/functions in my private header? Thanks!

EDIT:

Maybe I’m not wording my question right, but what I meant is, for example, I have myMath.h and myMath.cpp, and myMath.h has:

class myMath{
public:
    void initialise(double _a, double _b);
    double add();
    double subtract();

private:
    double a;
    double b;
};

And myMath.cpp has the implementations of the functions. How can I make it so that myMath.h only has the three public functions, and the private variables are defined in another file (e.g. myMath_i.h), and these three files are in such a way that after I create a static library, only myMath.h is needed by users. This also means myMath.h cannot #include myMath_i.h. So something like:

myMath.h:

class myMath{
public:
    void initialise(double _a, double _b);
    double add();
    double subtract();
}

and myMath_i.h:

class myMath{
private:
    double a;
    double b;
}

Of course that’s not possible because then I’ll be redefining the class myMath…

  • 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-13T17:31:01+00:00Added an answer on May 13, 2026 at 5:31 pm

    You have two header files MyClass.h and MyClass_p.h and one source file: MyClass.cpp.

    Lets take a look at what’s inside them:

    MyClass_p.h:

    // Header Guard Here
    class MyClassPrivate
    {
    public:
        int a;
        bool b;
        //more data members;
    }
    

    MyClass.h:

    // Header Guard Here
    class MyClassPrivate;
    class MyClass
    {
    public:
        MyClass();
        ~MyClass();
        void method1();
        int method2();
    private:
        MyClassPrivate* mData;
    }
    

    MyClass.cpp:

    #include "MyClass.h"
    #include "MyClass_p.h"
    
    MyClass::MyClass()
    {
        mData = new MyClassPrivate();
    }
    
    MyClass::~MyClass()
    {
        delete mData;
    }
    
    void MyClass::method1()
    {
        //do stuff
    }
    
    int MyClass::method2()
    {
        return stuff;
    }
    

    Keep your data in MyClassPrivate and distribute MyClass.h.

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

Sidebar

Related Questions

can someone please give me an example on the internet of how the system
Can someone give me a simple example involving threads in this manner, please. Problem
Can someone please give me an example of using Attribute.isDefined() to check if a
can someone please give me a c# example of drawing a string using GDI+
Can someone please redirect me to the right link or give an example of
Can someone please explain or give some resources on how function composition works in
can someone please give me an example of how to change the SORL-thumbnail format
Can someone please give an example of how to use the HashMap forall() method?
can someone please give me an example of how you detect if the iphone
Can someone please give me an example of how to access the Halo: Reach

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.