constructors in derived class...

This is a discussion on constructors in derived class... within the C++ Programming forums, part of the General Programming Boards category; i was going through a program on run-time polymorphism and i encountered a program which had 1 base class and ...

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

    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
    21,442
    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) {}
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    41
    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
    21,442
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    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-28-2002, 11:29 PM
  3. Constructors + Derived class
    By MethodMan in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2002, 04:05 PM
  4. Excaption in Derived Constructors?
    By Davros in forum C++ Programming
    Replies: 6
    Last Post: 10-30-2002, 09:01 AM
  5. Replies: 2
    Last Post: 01-15-2002, 05:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21