Thread: Inheritance Problem

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    118

    Inheritance Problem

    In the periodical derived class, i want to be able to output title and publisher on the base class publication where i output the description, how can i do that?
    Code:
    #include <iostream.h>
    #include <string.h>
    
    class publication {
       char title[60];
       char publisher[40];
      public:
       publication(char* t, char* p) {strcpy(title, t); strcpy(publisher,p);}
       virtual void out(ostream &os) const { os << "Publication: " << title << "\n" << "Published by " << publisher << endl; }
    };
    
    ostream &operator<<(ostream &os, const publication &x) {
       x.out(os);
       return os;
    }
    
    class periodical : public publication {
       char description[20];
      public:
       periodical(char* t, char* p, char* d) : publication(t,p) {strcpy(description, d);} 
       void out(ostream &os) const { os << "Published " << description << endl;}     
    };
    
    int main() {
       publication *p[3];
       p[0] = new publication("OOP244 Notes", "Seneca College");
       p[1] = new periodical("Toronto Sun", "Southam News", "Daily");
       /*p[2] = new book("21 Days to a Better You", "Prentice Hall",
    		   "Joseph Noodlemeyer", "1-234-5546-3");*/
       for (int i=0; i < 3; i++) {
          cout << *p[i] << '\n';
          delete p[i];
       }
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    i actually put protected:
    Code:
    class publication {
      protected:
       char title[60];
       char publisher[40];
      public:
       publication(char* t, char* p) {strcpy(title, t); strcpy(publisher,p);}
       virtual void out(ostream &os) const { os << "Publication: " << title << "\n" << "Published by " << publisher << endl; }
    };
    and was wondering is that is bad or its ok to make them protected? becuase it works if they are

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. There is no valid reason to use iostream.h instead of iostream. (No, being able to omit the line "using namespace std;" or not having to qualify ostream as std::ostream are not valid reasons.)
    2. Similarly, there is no valid reason that I can see for using char arrays instead of std::strings.
    3. Given all that, the answer to your question is that you need to call publication::out inside your periodical::out function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance problem
    By logicwonder in forum C++ Programming
    Replies: 5
    Last Post: 10-07-2006, 10:14 AM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Multiple inheritance problem
    By Magos in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2006, 09:27 AM
  4. Inheritance using Stack Class Problem
    By dld333 in forum C++ Programming
    Replies: 17
    Last Post: 12-06-2005, 11:14 PM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM