Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 23rd, 2006, 9:54 AM   #1
biohazard
Newbie
 
biohazard's Avatar
 
Join Date: Feb 2006
Posts: 18
Rep Power: 0 biohazard is on a distinguished road
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>
biohazard is offline   Reply With Quote
Old Apr 23rd, 2006, 10:01 AM   #2
t.i.g.e.r
Newbie
 
t.i.g.e.r's Avatar
 
Join Date: Apr 2006
Location: CZ-brno
Posts: 7
Rep Power: 0 t.i.g.e.r is on a distinguished road
use add::add() instead of int add::add()
t.i.g.e.r is offline   Reply With Quote
Old Apr 23rd, 2006, 10:03 AM   #3
biohazard
Newbie
 
biohazard's Avatar
 
Join Date: Feb 2006
Posts: 18
Rep Power: 0 biohazard is on a distinguished road
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>
biohazard is offline   Reply With Quote
Old Apr 23rd, 2006, 10:05 AM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Constructors and deconstructors, by definition, cannot return anything. You're therefore not allowed to have a return type. Not even void.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 23rd, 2006, 10:10 AM   #5
biohazard
Newbie
 
biohazard's Avatar
 
Join Date: Feb 2006
Posts: 18
Rep Power: 0 biohazard is on a distinguished road
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>
biohazard is offline   Reply With Quote
Old Apr 23rd, 2006, 10:17 AM   #6
t.i.g.e.r
Newbie
 
t.i.g.e.r's Avatar
 
Join Date: Apr 2006
Location: CZ-brno
Posts: 7
Rep Power: 0 t.i.g.e.r is on a distinguished road
yeah, cons. and dest. w/out return types
t.i.g.e.r is offline   Reply With Quote
Old Apr 23rd, 2006, 10:20 AM   #7
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
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.
InfoGeek is offline   Reply With Quote
Old Apr 23rd, 2006, 11:08 AM   #8
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by InfoGeek
Yeah, It should. The constructor doesn't return anything via the return statement. It implicitely returns the class object.
Your first comment is correct, your second is not; constructors do not return anything. As Ooble said, no return type is allowed to be specified.

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).
grumpy is offline   Reply With Quote
Old Apr 23rd, 2006, 11:16 AM   #9
java_roshan
Professional Programmer
 
Join Date: Mar 2005
Location: Student of University of Mumbai, Maharashtra State, India
Posts: 344
Rep Power: 4 java_roshan is on a distinguished road
<snip>
Quote:
Originally Posted by grumpy
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.

Could you please elaborate on Raw Memory?
__________________
Visit: http://www.somaiya.edu
java_roshan is offline   Reply With Quote
Old Apr 23rd, 2006, 11:42 AM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:36 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC