What you want is to call the PrintMe
method of the parent class GeneralHeader
from the PrintMe
method of IPHeader
and MACHeader
. This can be done by prefixing the method call with the parent class. Example:
void IPHeader::PrintMe(){
GeneralHeader::PrintMe(); //Call the super method
std::cout << "Version: " << Version << std::endl; //Print IPHeader members
}
The method of MACHeader
is analoguous.
If you have polymorphism, i.e., you want to call the PrintMe
method of a derived class through a pointer or reference of the super class, e.g.:
GeneralHeader* h = new IPHeader();
h->PrintMe();
then you should declare the method in the GeneralHeader
class virtual
. Otherwise, only PrintMe
of GeneralHeader
will be called in the above code, even if h
is actually an IPHeader
. If you use C++11, you might want to declare the methods in the deriving classes override
to make sure that they do override GeneralHeader::PrintMe()
.