Thread: constructors in derived class...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    45

    constructors in derived class...

    i was going through a program on run-time polymorphism and i encountered a program which had 1 base class and 2 classes derived from the base class..
    the base class is as follows

    Code:
    class media
    {
      protected:
        char title[50];
        float price;
      public:
        media(char *s,float a)
        {
          strcpy(title,s);
         price = a;
        }
      virtual void display() {  } // empty virtual function..
    };
    and one of the base class is as follows..

    Code:
    class book:public media
    {
       int pages;
     public:
      book(char *s,float a,int p):media(s,a)
       {
         pages = p;
        }
      void display();
    };
    Now my doubt is with this line..in the derived class..

    Code:
    book(char *s,float a,int p):media(s,a)
    i understand that this is a constructor with 3 parameters..
    but why is this included..
    Code:
    :media(s,a)
    is the constructor derived..?? or what..??
    can someone please clarify..!!!!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is part of the initialiser list. Here it is used to invoke the base class constructor. In fact, we could have written:
    Code:
    book(char* s, float a, int p) : media(s, a), pages(p) {}
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    hmm..i still dont understand..
    are we trying to initialize the values of the memebers of the base class also..along with the members of the derived class..??

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by narendrav
    are we trying to initialize the values of the memebers of the base class also..along with the members of the derived class
    Effectively, yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    ok..thank you...very much..for the quick reply..!!!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    FYI, be aware that this is dangerous practice.

    >>char title[50];
    This is a limited size buffer as thus you need to make sure you don't write past its limits.

    >>strcpy(title,s);
    This is a ticking time bomb. You don't check the length of s to make sure it fits in title.

    But instead of messing with this, use std::string:

    std::string title;
    title = s;

    Done. Safe. Easy. Quick.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-19-2008, 03:04 AM
  2. Replies: 4
    Last Post: 12-29-2002, 12:29 AM
  3. Constructors + Derived class
    By MethodMan in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2002, 05:05 PM
  4. Excaption in Derived Constructors?
    By Davros in forum C++ Programming
    Replies: 6
    Last Post: 10-30-2002, 10:01 AM
  5. Replies: 2
    Last Post: 01-15-2002, 06:00 PM