Search code examples
c++classobjectinheritancethrow

Understanding a simple C++ class structure


I am trying to understand some C++ code and find that my C++ is a little rosty. Amongst others the code features the following class structure (CLASS1 to CLASS3 not shown for brevity):

class CLASS4
  :public CLASS3
  ,public CLASS2{
  public:
    CLASS4(double VARA, double VARB, char VARC, int VARD, double VARE, std::vector<double> VARF, std::string VARG = "")
    throw(Error);
    CLASS4(const CLASS4&);
    ~CLASS4();

    double METHOD1();

  protected:
    void METHOD2();
};

CLASS4::CLASS4(double VARA, double VARB, char VARC, int VARD, double VARE, vector<double> VARF, string VARG) throw(Error)
  :CLASS1(VARC, VARD, VARE, VARF, VARG)
  ,CLASS2(VARB)
  ,CLASS3(VARA, VARC, VARD, VARE, VARF, VARG){}

CLASS4::CLASS4(const CLASS4& VARH)
  :CLASS1(VARH), CLASS2(VARH), CLASS3(VARH){}

CLASS4::~CLASS4(){}

I understand the object concept and the concept of inheritance. I see the constructor and destructor of CLASS4. What I do not understand is the listing of classes in the inheritance :public CLASS3, public CLASS2. Whats the purpose of it? Furthermore I wonder what the throw(Error) is supposed to do. Also I wonder what the purpose of the second constructor CLASS4(const CLASS4&); is. I am aware that this will be a simple problem for a C++ programmer.


Solution

  • class CLASS4
      :public CLASS3
      ,public CLASS2
    

    is multiple inheritance. The puspose of multiple inheritance is making your object's functionalities has also functionalities of CLASS2 and CLASS3 which is created from CLASS4 ( read this page for detailed explenation : http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr134.htm )

    throw(Error) is supposed to throw you Error when it finds an error, it may sound lame, but it's used for exception handling :) (I suggest you to read this page for clear referance : http://msdn.microsoft.com/en-us/library/6dekhbbc(v=vs.80).aspx)

    and CLASS4(const CLASS4&); is the copy constructor. "A copy constructor is a special constructor in the C++ programming language for creating a new object as a copy of an existing object." (quoted from wikipedia. http://en.wikipedia.org/wiki/Copy_constructor)