![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2006
Posts: 18
Rep Power: 0
![]() |
Constructor problem
I wrote the following program:
#include<iostream>
using namespace std;
class add
{
public:
add();
add(int,int);
};
int add::add()
{
cout<<"Constructor"<<endl;
}
int add::add(int x,int y)
{
cout<<"The sum of the two no.s is"<<x+y<<endl;
}
int main()
{
add a;
a.add();
a.add(5,6);
return 0;
}The compiler is saying that I am not using the correct return type for the constructors.
__________________
Let us be thankful for the fools. But for them the rest of us could not succeed<Mark Twain> Get your facts first, and then you can distort them as much as you please<Mark Twain> |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Apr 2006
Location: CZ-brno
Posts: 7
Rep Power: 0
![]() |
use add::add() instead of int add::add()
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2006
Posts: 18
Rep Power: 0
![]() |
Are you sure? Functions should have a rreturn type specified.
__________________
Let us be thankful for the fools. But for them the rest of us could not succeed<Mark Twain> Get your facts first, and then you can distort them as much as you please<Mark Twain> |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Constructors and deconstructors, by definition, cannot return anything. You're therefore not allowed to have a return type. Not even void.
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Feb 2006
Posts: 18
Rep Power: 0
![]() |
Okay. If I remove the return types, the program will work fine?
__________________
Let us be thankful for the fools. But for them the rest of us could not succeed<Mark Twain> Get your facts first, and then you can distort them as much as you please<Mark Twain> |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Apr 2006
Location: CZ-brno
Posts: 7
Rep Power: 0
![]() |
yeah, cons. and dest. w/out return types
|
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Yeah, It should. The constructor doesn't return anything via the return statement. It implicitely returns the class object.
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#8 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Quote:
A constructor initialises an object; it does not return it. The process of constructing an object goes in two phases: first, raw memory is found that will be used to represent the object. Second, an appropriate constructor is invoked to turn that raw memory into an actual object. The process of destroying an object works in reverse: a destructor is invoked to turn the object back to raw memory, and then the memory is released (as far as the program is concerned). |
|
|
|
|
|
|
#9 | |
|
Professional Programmer
Join Date: Mar 2005
Location: Student of University of Mumbai, Maharashtra State, India
Posts: 344
Rep Power: 4
![]() |
<snip>
Quote:
Could you please elaborate on Raw Memory?
__________________
Visit: http://www.somaiya.edu |
|
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Memory to hold an instantiated object. The class definition is just a spec, a blueprint. When you instantiate, actual memory must be used. This may be local or dynamically allocated, for instance, depending upon the statement you write to instantiate. The appropriate amount of memory, once acquired, is just a block of junk. Construction of the object, and any initialization, take care of making it useful.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|