Beginner C++ class problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kackson

    Beginner C++ class problem

    Hi.
    What does it mean when it is written as:

    in the test.hpp file->

    class MYFUNCTION: public TOP
    {
    public:
    ....some variables...
    MYFUNCTION(int x, int y, int z.. and some more argument);
    .... and some more other variables and functions...;
    }

    and then in the test.cpp file->

    MYFUNCTION::MYF UNCTION(int z, int y, int z,.. and some more
    argument):TOP(x ,y,z,w),_aa(a), _bb(b),_cc(c)
    {
    ....some functions...;
    }

    What I dont understand is the meaning of the test.cpp ->
    MYFUNCTION::MYF UNCTION:TOP(){}

    What does this mean :: and follow by : and then a pair of {} means?
    Could someone point me to a website where I could read such basic
    things?

    Thanks.
  • Gianni Mariani

    #2
    Re: Beginner C++ class problem

    kackson wrote:[color=blue]
    > Hi.
    > What does it mean when it is written as:
    >[/color]
    .....[color=blue]
    >
    > What I dont understand is the meaning of the test.cpp ->
    > MYFUNCTION::MYF UNCTION:TOP(){}
    >
    > What does this mean :: and follow by : and then a pair of {} means?
    > Could someone point me to a website where I could read such basic
    > things?[/color]

    I think you're referring to the initializer list.

    This is a simpler *compilable* example.

    #include <iostream>

    class B
    {
    public:
    B( int );

    int b;
    };

    class A : public B
    {
    public:

    A( int );
    };


    A::A( int val )
    : B( val ) // <<< initializer list
    {
    std::cout << "A is constructed\n";
    }

    B::B( int val )
    : b( val ) // <<< initializer list
    {
    std::cout << "B is constructed\n";
    }


    A a( 2 );

    int main()
    {
    }

    The initializer list is only somthing you can place on a constructor.

    Comment

    • Jon Bell

      #3
      Re: Beginner C++ class problem

      In article <90250227.03091 50834.785ae975@ posting.google. com>,
      kackson <kackson@yahoo. com> wrote:[color=blue]
      >
      >in the test.hpp file->
      >
      >class MYFUNCTION: public TOP
      >{
      >public:
      >...some variables...
      >MYFUNCTION(i nt x, int y, int z.. and some more argument);
      >... and some more other variables and functions...;
      >}
      >
      >and then in the test.cpp file->
      >
      >MYFUNCTION::MY FUNCTION(int z, int y, int z,.. and some more
      >argument):TOP( x,y,z,w),_aa(a) , _bb(b),_cc(c)
      >{
      >...some functions...;
      >}
      >
      >What I dont understand is the meaning of the test.cpp ->
      >MYFUNCTION::MY FUNCTION:TOP(){ }
      >
      >What does this mean ::[/color]

      That's the "scope resolution operator." On its left is the name of a
      class, and on the right is the name of a member function of that class.
      You use this notation when you define a member function because different
      classes can have member functions with the same name, so when you define a
      member function, you have to say which class it belongs to.
      [color=blue]
      > and follow by :[/color]

      This particular member function is the constructor for class
      MYFUNCTION. (You can tell it's the constructor because it has the same
      name as the class itself.) The list of things after the : is the
      constructor's "initializa tion list." Basically it says, take the
      arguments x,y,z,w and use them to initialize the member data of class TOP
      (from which MYFUNCTION is derived), then take the argument a and use it to
      initialize the member data _aa of MYFUNCTION, etc.
      [color=blue]
      > and then a pair of {} means?[/color]

      If this constructor had any actual executable statements, they would go
      between these braces, just like in any other function. In this particular
      constructor, all the work is done in the initialization list, so it
      doesn't need any executable statements, but you still have to have the
      empty {} to keep the compiler happy.
      [color=blue]
      >Could someone point me to a website where I could read such basic
      >things?[/color]

      Try a decent textbook instead. If you don't want to pay for a printed
      book, I understand the free downloadable e-book "Thinking in C++" at
      <http://www.bruceeckel. com/> is OK.

      --
      Jon Bell <jtbellap8@pres by.edu> Presbyterian College
      Dept. of Physics and Computer Science Clinton, South Carolina USA

      Comment

      • John Carson

        #4
        Re: Beginner C++ class problem

        "kackson" <kackson@yahoo. com> wrote in message
        news:90250227.0 309150834.785ae 975@posting.goo gle.com[color=blue]
        > Hi.
        > What does it mean when it is written as:
        >
        > in the test.hpp file->
        >
        > class MYFUNCTION: public TOP
        > {
        > public:
        > ...some variables...
        > MYFUNCTION(int x, int y, int z.. and some more argument);
        > ... and some more other variables and functions...;
        > }
        >
        > and then in the test.cpp file->
        >
        > MYFUNCTION::MYF UNCTION(int z, int y, int z,.. and some more
        > argument):TOP(x ,y,z,w),_aa(a), _bb(b),_cc(c)
        > {
        > ...some functions...;
        > }
        >
        > What I dont understand is the meaning of the test.cpp ->
        > MYFUNCTION::MYF UNCTION:TOP(){}
        >
        > What does this mean :: and follow by : and then a pair of {} means?
        > Could someone point me to a website where I could read such basic
        > things?
        >
        > Thanks.[/color]

        You need a book. This one can be downloaded for free:




        --

        John Carson
        1. To reply to email address, remove donald
        2. Don't reply to email address (post here instead)


        Comment

        Working...