Need help

Discussion in 'C++' started by ravi, Aug 30, 2006.

  1. ravi

    ravi Guest

    Hi,
    I am new to C++ .Can anyone please tell me what does ":" operator
    mean in C++.For ex: there is a program snippet which looks like that

    class convert {
    protected:
    double val1; // initial value
    double val2; // converted value
    public:
    convert(double i) {
    val1 = i;
    }
    virtual void compute() = 0;
    };
    // Liters to gallons.
    class l_to_g : public convert {
    public:
    l_to_g(double i) : convert(i) { }
    void compute() {
    val2 = val1 / 3.7854;
    }
    };

    In the above piece of code there is a line as
    l_to_g(double i) : convert(i) { }

    Can anybody tell me wht does the above line mean

    Thanks
    Ravi
     
    ravi, Aug 30, 2006
    #1
    1. Advertising

  2. ravi

    Marco Wahl Guest

    "ravi" <> writes:

    > I am new to C++ .Can anyone please tell me what does ":" operator
    > mean in C++.For ex: there is a program snippet which looks like that


    ":" is merely a seperator. Your question is basic and
    should be answered in C++-learning books.

    Anyway, after reindenting and commenting some your code I come to the
    following code:

    class convert {
    protected:
    double val1; // initial value
    double val2; // converted value
    public:
    convert(double i) {
    val1 = i;
    }
    virtual void compute() = 0;
    };

    class l_to_g : // Seperator for the baseclass-list,
    // here class convert.
    public convert {
    public:
    l_to_g(double i) : // After this seperator follows
    // the initializer-list,
    // e.g. initialize the base-class
    // convert.
    convert(i) { }
    void compute() {
    val2 = val1 / 3.7854;
    }
    };
    >
    > In the above piece of code there is a line as
    > l_to_g(double i) : convert(i) { }
    >
    > Can anybody tell me wht does the above line mean


    Hopefully the comments in the code above help you.


    Best wishes
     
    Marco Wahl, Aug 30, 2006
    #2
    1. Advertising

  3. ravi

    David Harmon Guest

    On 29 Aug 2006 23:39:21 -0700 in comp.lang.c++, "ravi"
    <> wrote,
    >In the above piece of code there is a line as
    > l_to_g(double i) : convert(i) { }
    >
    >Can anybody tell me wht does the above line mean


    This particular ":" is an example of the "constructor initializer
    list". It passes information to the constructors of members or (as
    in this example) base classes that are created _before_ the
    constructor body runs.

    This issue is mentioned in Marshall Cline's C++ FAQ. See the topic
    "[10.6] Should my constructors use "initialization lists" or
    "assignment". It is always good to check the FAQ before posting.
    You can get the FAQ at:
    http://www.parashift.com/c -faq-lite/

    Please see also the topic "[5.7] What makes a good Subject: line?"
     
    David Harmon, Aug 30, 2006
    #3
  4. ravi

    Bart Guest

    ravi wrote:
    <snip>
    > In the above piece of code there is a line as
    > l_to_g(double i) : convert(i) { }
    >
    > Can anybody tell me wht does the above line mean


    This is a constructor initializer list. It initializes the members and
    base clases. Normally, when you have a base class with a default
    constructor you can just omit this initializer. For example:

    class Base
    {
    public:
    Base(); // default constructor
    };

    class Derived : public Base
    {
    public:
    Derived() {} // Don't need to initialize the base class. Default
    constructor available.
    };

    But when there is no default constructor you need to explicitly
    initialize any base class without one. You also need to explicitly
    initialize members that need an initializer, like references and const
    members. You can separate the initializers with commas, like so:

    MyClassConstructor()
    : member1(/* something */)
    , member2(/* something */)
    , member3(/* something */)
    {
    }

    Generally, it is better form to initialize your members in the
    initializer list rather than using assignments inside the body of the
    constructor.

    Regards,
    Bart.
     
    Bart, Aug 30, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TN Bella
    Replies:
    1
    Views:
    2,478
    Edward
    Jun 18, 2004
  2. Teep
    Replies:
    2
    Views:
    767
    Larry Marburger
    Jun 21, 2004
  3. John
    Replies:
    1
    Views:
    402
    John C. Bollinger
    Aug 19, 2004
  4. =?Utf-8?B?Q2hyaXM=?=

    Help Help. I really need some help with this

    =?Utf-8?B?Q2hyaXM=?=, Jan 31, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    568
    =?Utf-8?B?SmFzb24gVmVybWlsbGlvbg==?=
    Jan 31, 2007
  5. ElementX
    Replies:
    9
    Views:
    497
    RedGrittyBrick
    Oct 1, 2008
Loading...

Share This Page