C++ Both class should inherit same base class. Add methods

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mobola
    New Member
    • Feb 2014
    • 7

    C++ Both class should inherit same base class. Add methods

    Class book
    {
    private:
    char title [30];
    char author [];};
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    There's not enough information for me to know what your question is. Please post additional details.

    Comment

    • Mobola
      New Member
      • Feb 2014
      • 7

      #3
      Originally posted by weaknessforcats
      There's not enough information for me to know what your question is. Please post additional details.
      what happen is that from the two class definitions, using generalization, put them into appropriate classification hierarchy si that they both inherit from a common base class. Add appropriate methods. I posted the two codes earlier. Help please

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You showed only one class. You don't have an inheritance situation unless you have at least one more class.

        What are the other classes?

        Comment

        • Mobola
          New Member
          • Feb 2014
          • 7

          #5
          Originally posted by weaknessforcats
          You showed only one class. You don't have an inheritance situation unless you have at least one more class.

          What are the other classes?
          there were two classes. The first one is class book, and the second one was class magazine. Below

          class magazine
          {
          private:
          char title [30];
          char editor [30];
          char publisher [30];
          };

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            OK.

            Look at the Magazine and the Book. If there are common data members you could put them in a third class called, say, Publication and take them out of Magazine and Book.

            Magazine derives from Publication.
            Book derives from Publication.

            Next, write member functions in Publication to change the data and to show the data.

            Since Book derives from Publication, when you create a Book object you can use the Publication methods with the Book object because Book IS-A Publication.

            In memory you have only one object, Book, and part of that object is an embedded Publication object.

            The idea is that when you create a Magazine object there will be an embedded Publication object there also. What you have is ONE SET of functions for the Book and the Magazine as opposed to two sets of functions, one for each class. Reuse of code is a prime C++ objective.

            Comment

            • Mobola
              New Member
              • Feb 2014
              • 7

              #7
              Thank you so much. But please is there a way you can tip me on the code for publication. Like shoud it be private or public. I mean can i have something like this....

              {
              Class publication: private class book: private class magazine

              {
              private:
              char title [30];
              char publisher [30];

              setdisplay() {title};
              setdisplay() {publisher};

              };

              {
              cout<<''enter title"<<
              cout<<''enter publisher"<<
              };

              };

              please correct me. Thank you

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                The data members should always be private. The member functions in this case should be public. You should inherit public so that everything that is public in the base class will be public in the derived class.

                Do not inherit private or protected until you know what those are for. I can tell you they do not apply here.

                Think about what you are doing. The publication class is not a derived class so it doesn't inherit from anything. The Magazine IS A Publication so it derives from Publication. The Book IS A Publication so it derives from Publication.

                I can't tip you the code because I know how to do this. You will too but only if you do it yourself. Take your best shot and post again. We'll get through this.

                Comment

                • Mobola
                  New Member
                  • Feb 2014
                  • 7

                  #9
                  Ok, like this....

                  Class publication

                  {
                  public:
                  char title [30];
                  char author [30];
                  char editor [30];
                  char publisher [30];
                  char ISBN [20];
                  };

                  class book: public publication

                  {
                  private:
                  char title [30];
                  char author [30];
                  char publisher [30];
                  char ISBN [20];
                  };

                  class magazine: public publication

                  {
                  private:
                  char title [30];
                  char editor [30];
                  char publisher [30];
                  };


                  {Edit: Request to write code removed}
                  Last edited by Stewart Ross; Feb 9 '14, 08:40 AM. Reason: Please do not ask for others to write your code for you.

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Close.

                    Remember the derived class inherits from the base class. Therefore, the derived class does not have any data members that duplicate data members in the base class.

                    In your case, the base class Publication does not have an ISBN member because not all publications have an ISBN. Only books do. The same applies to author. So remove these from the base class:

                    Code:
                    Class publication
                    
                     { 
                     public:
                     char title [30];
                     char editor [30];
                     char publisher [30];
                      };
                    The book class does not need title, editor or publisher because title and publisher are inherited from publication. Books don't have editors so now editor needs to be removed from publication. Now things look like:

                    Code:
                    Class publication
                    
                     { 
                     public:
                     char title [30];
                     char publisher [30];
                      };
                    
                     class book: public publication
                    
                     {
                     private:
                    
                      char author [30];
                      char ISBN [20];
                     };
                    Finally, the magazine class has an editor, a title, and a publisher. The title and the publisher come from the publication class and the editor becomes a member of the magazine class:

                    Code:
                    class magazine: public publication
                    
                     {
                     private:
                      char editor [30];
                     };
                    You should be able to see that there are no duplications of data members and that each class either has the data or inherits the data it needs.

                    Does any of this make sense to you?
                    Last edited by weaknessforcats; Feb 9 '14, 04:39 PM. Reason: logic error

                    Comment

                    • Mobola
                      New Member
                      • Feb 2014
                      • 7

                      #11
                      Yes. It does. Thank you. I need to note that, a derived class does not duplicate its member of thesame member of the base class, it inherit. Thank you

                      Comment

                      Working...