-
April 16th, 2014, 07:04 AM
#1
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;
}
-
April 16th, 2014, 07:15 AM
#2
Re: Function of the same name as the class
That's a contstuctor, a function that's automatically called when an object is created.
-
April 16th, 2014, 07:17 AM
#3
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)
-
April 16th, 2014, 11:55 AM
#4
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.
-
April 21st, 2014, 03:28 AM
#5
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?
-
April 21st, 2014, 06:08 AM
#6
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)
-
April 21st, 2014, 07:41 AM
#7
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..
-
April 21st, 2014, 08:36 AM
#8
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)
-
April 22nd, 2014, 09:29 AM
#9
Re: Function of the same name as the class
Originally Posted by atee
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|