how do i define a class that is to be publicly derived from two other classes

Posted on 2005-04-08
Last Modified: 2010-04-24
I have two classes and I am going to have another, but I want to have this new class derived from the first two.

Here are my classes each in  there own header and at the bottom is what I'm proposing.

#ifndef DATE
   #define DATE
class date
      long  month,

      date(){ }
      date(long m, long d, long y)
          { month = m;
            day = d;
                year = y;
      void get_date(void);
      void set_date(long m,long d,long y);
      long last_day(long m,long y);
      long show_day();
      long show_month();
      long show_year();
      bool leap_year(long y);
      date operator +(long number_days);
      date operator -(long number_days);
      long operator  -(date other_date);
      long days_since(long m,long d,long y);
      long f(long m,long y);
      long g(long m);
      bool incorrect_date(long m,long d,long y);
      bool operator <(date a_date);
      bool operator <=(date a_date);
      bool operator ==(date a_date);
        bool operator !=(date a_date);
      bool operator >(date a_date);
      bool operator >=(date a_date);
      friend ostream &operator << (ostream &stream, date ab)
         stream << ab.month << "/";
         stream << << "/";
         stream << ab.year <<" ";
         return stream;

      friend istream &operator >> (istream &stream, date &ab)
         char slash;
         stream >> ab.month >> slash >> >> slash >> ab.year;
         if(ab.year< 100)
              if(ab.year >10)
                  ab.year += 1900;
                  ab.year += 2000;

         return stream;



#ifndef LUMBER
#define LUMBER

const short LEN = 40;       // maximum length of strings

class Type                // type of lumber
      char dimensions[LEN];
      char grade[LEN];
      Type();                     // constructor (no args)
      Type(char di[], char gr[]);  // constructor (two args)
      void gettype();              // get type from user

      void showtype();             // display type

      char * show_grade();         // displays the grade

      char * show_dimensions();    // displays the dimensions

class Distance                    // English Distance class
      short feet;
      double inches;
      Distance();                  // constructor (no args)
      Distance(short ft, double in);  // constructor (two args)

      void getdist();              // get length from user

      int show_feet();             // shows the feet

      double show_inches();        // shows the inches

      void showdist();             // display distance      

class Lumber : public Type, public Distance
      short quantity;                      // number of pieces
      double price;                       // price of each piece
      Lumber(); // constructor (no args)  
                                     // constructor (6 args)
      Lumber( char di[], char gr[],      // args for Type
            short ft, double in,          // args for Distance
              short qu, double prc );

      void getlumber();                    // gets from the keyboard the lumber values
      void showlumber();                   // displays on the screen the values

      int show_quantity();                 // diplays on the screen the quantity

      double show_price();                 // diplays on the screen the  price



class inventory : public date, public lumber

Would I need virtual classes? I do not want to modify my existing code, I just wnat this in a hearder and have it linked to the other classess.
Question by:stallion5000
    LVL 9

    Accepted Solution

    You do not *need* "virtual classes".

    class inventory : public date, public lumber
    // ...

    is just fine.

    One question though, is an inventory a date? Or does the inventory have a date?
    This is not just a philosophical question. And it has nothing to do with programming in C++.
    But, it's the question you always need to ask yourself in order to decide whether to use inheritance or aggregation in your OO model.

    If the inventory is a date, then inheritance is the correct choice. In your case, the inventory would also need to be a lumber.
    If the inventory has a data, then you should use aggregation instead. Think about a car. It has a steering wheel, it isn't a steering wheel. Thus, you wouldn't inherit a 'car' class from a 'wheel'. You would make a 'wheel' instance a member of the 'car' class (i.e. aggregate).


    LVL 1

    Expert Comment

    rcarlan :
    when you define a class , its accesibilty is private by default :
    define your previous class as public :

    public class date ........
    public class Type ......
    public class Distance .......
    public class Lumber : public Type, public Distance .......
    public class inventory : public date, public lumber ........

    LVL 9

    Expert Comment


    Maybe in the managed C++ world ;-)

    In standard C++, your declarations would actually result in compilation errors.

    stallion5000's code didn't look to me like managed C++.


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
    In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now