Function of the same name as the class
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Function of the same name as the class

  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
    Posts
    12,106

    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
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,441

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,307

    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
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,441

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  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
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,441

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  9. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,917

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center