troubleshooting Question

Confused with C++ header files

Avatar of GPicasso
GPicasso asked on
C++Programming Theory
5 Comments1 Solution360 ViewsLast Modified:
Ok I've got a c++ project that I want to break down into properly organized header files but the help has me confused
Here is an example i found on the internet:


Date.h:
	
#ifndef DATE_H
#define DATE_H
 
class Date
{
private:
    int m_nMonth;
    int m_nDay;
    int m_nYear;
 
    Date() { } // private default constructor
 
public:
    Date(int nMonth, int nDay, int nYear);
 
    void SetDate(int nMonth, int nDay, int nYear);
 
    int GetMonth() { return m_nMonth; }
    int GetDay()  { return m_nDay; }
    int GetYear() { return m_nYear; }
};
 
#endif
Date.cpp:
	
#include "Date.h"
 
// Date constructor
Date::Date(int nMonth, int nDay, int nYear)
{
    SetDate(nMonth, nDay, nYear);
}
 
// Date member function
void Date::SetDate(int nMonth, int nDay, int nYear)
{
    m_nMonth = nMonth;
    m_nDay = nDay;
    m_nYear = nYear;
}

Ok my issue here is that there is actual code in the header file! but only for the accessors ...  not for the constructor.  Seems inconsistent to me so I'm not sure how I should be fixing my own code (Which will not compile as is due to lack of forward declaration.)
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros