?
Solved

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

Posted on 2005-04-08
3
Medium Priority
?
163 Views
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
{
  private:
      long  month,
            day,
            year;
  public:

      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 << ab.day << "/";
         stream << ab.year <<" ";
         return stream;
      }
   

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

         return stream;
     }
};



#endif

AND

#ifndef LUMBER
#define LUMBER

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

class Type                // type of lumber
{
   private:
      char dimensions[LEN];
      char grade[LEN];
   public:
      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
{
   private:
      short feet;
      double inches;
   public:
      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
{
   private:
      short quantity;                      // number of pieces
      double price;                       // price of each piece
   public:
      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
};


#endif



WOULD THE NEW CLASS LOOK LIKE THIS?

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.
0
Comment
Question by:stallion5000
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
rcarlan earned 2000 total points
ID: 13741716
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).

Radu

0
 
LVL 1

Expert Comment

by:mr60
ID: 13743111
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 ........

GOOD LUCK
0
 
LVL 9

Expert Comment

by:rcarlan
ID: 13743119
mr60,

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++.

Radu
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

749 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