CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2014
    Posts
    17

    Function of the same name as the class

    I was reading the following in order to understand polymorphism. You can see that the function "Shape" is of the same name as that of the class.

    What is the significance here?


    Code:
    #include <iostream> 
    using namespace std;
     
    class Shape {
       protected:
          int width, height;
       public:
          Shape( int a=0, int b=0)
          {
             width = a;
             height = b;
          }
          int area()
          {
             cout << "Parent class area :" <<endl;
             return 0;
          }
    };
    class Rectangle: public Shape{
       public:
          Rectangle( int a=0, int b=0):Shape(a, b) { }
          int area ()
          { 
             cout << "Rectangle class area :" <<endl;
             return (width * height); 
          }
    };
    class Triangle: public Shape{
       public:
          Triangle( int a=0, int b=0):Shape(a, b) { }
          int area ()
          { 
             cout << "Triangle class area :" <<endl;
             return (width * height / 2); 
          }
    };
    // Main function for the program
    int main( )
    {
       Shape *shape;
       Rectangle rec(10,7);
       Triangle  tri(10,5);
    
       // store the address of Rectangle
       shape = &rec;
       // call rectangle area.
       shape->area();
    
       // store the address of Triangle
       shape = &tri;
       // call triangle area.
       shape->area();
       
       return 0;
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Function of the same name as the class

    That's a contstuctor, a function that's automatically called when an object is created.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Function of the same name as the class

    A class function with the same name as the class is a constructor for the class and is called when the class is instantiated.
    In your example above,
    Code:
    Rectangle rec(10,7);
    calls
    Code:
    Rectangle( int a=0, int b=0):Shape(a, b) { }
    in the class Rectangle
    with a = 10 and b = 7 which then calls
    Code:
    Shape( int a=0, int b=0)
          {
             width = a;
             height = b;
          }
    in the class Shape with a = 10 and b = 7.

    See http://www.cplusplus.com/doc/tutorial/polymorphism/
    Last edited by 2kaud; April 16th, 2014 at 07:22 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Function of the same name as the class

    A constructor isn't really a function or class method, but if you want to call it one, it would be a method without a return type. Same for destructors too.

  5. #5
    Join Date
    Apr 2014
    Posts
    17

    Re: Function of the same name as the class

    Thanks a lot.

    That means when I craete object of a derived class (in my code that derived class being Rectangle), the constructor of the derived class is called followed by the constructor of the base class.

    Right?

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Function of the same name as the class

    the constructor of the derived class is called followed by the constructor of the base class.
    Use the debugger to trace through the program to see the order of execution then you'll see which comes first. The ability to use the debugger is an essential skill that a programmer needs to master so now seems like a good time for you to get to grips with it. What os/compiler are you using?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Apr 2014
    Posts
    17

    Re: Function of the same name as the class

    Yes, I confirmed that I was right using the debugger

    I use Visual Studio 2008.How can I check the compiler version?Sorry for asking this question..

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Function of the same name as the class

    I use Visual Studio 2008.How can I check the compiler version?Sorry for asking this question..
    That's fine (use Help/About for more specific info). The debugger is specific to the compiler. There's Visual Studio xxx, gcc etc for different compilers.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Function of the same name as the class

    Quote Originally Posted by atee View Post
    Yes, I confirmed that I was right using the debugger

    I use Visual Studio 2008.How can I check the compiler version?Sorry for asking this question..
    then you aren't using the debugger correctly, or drawing incorrect conclusions.
    base class is constructed first then derived constructor code is called.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured