473,473 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

unexpected return value


I'm expecting to see itsAge = 0 in the output of the following code.
However, in the main() function, itsAge = 1 . Can someone explain??
thanks,

Yin99

===== begin code ======
#include <iostream>
using namespace std;
class Cat
{
public:
Cat(){
itsAge = 0;
cout << "constuctor age is: " << itsAge; //zero here!
}

void setAge(int age){
itsAge = age;
}

int getAge(){
return itsAge;
}
private:
int itsAge;
};
int main ()
{

Cat *c = new Cat();
//c->getAge returns 1 here, Why and Where does 1 come from?
cout << "Cat's Age is: " << c->getAge;
return 0;

}

Aug 4 '05 #1
3 1877

"Yin99" <ws@ziowave.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

I'm expecting to see itsAge = 0 in the output of the following code.
However, in the main() function, itsAge = 1 . Can someone explain??
thanks,

Yin99

===== begin code ======
#include <iostream>
using namespace std;
class Cat
{
public:
Cat(){
itsAge = 0;
cout << "constuctor age is: " << itsAge; //zero here!
}
This is not part of your reported problem, but a constructor
should use an initializer list instead of assigning values to
members after the fact:

Cat() : itsAge(0) {
cout << "constuctor age is: " << itsAge << '\n';
}

void setAge(int age){
itsAge = age;
}

int getAge(){
return itsAge;
}
Again, not part of your problem, but more correct is:

int getAge() const {
return itsAge;
}


private:
int itsAge;
};
int main ()
{

Cat *c = new Cat();
Yet again, not part of your problem, but why are you dynamically
allocating your object? Why not just define it, e.g.:

Cat my_cat;
//c->getAge returns 1 here, Why and Where does 1 come from?
cout << "Cat's Age is: " << c->getAge;
Try:

cout << "Cat's Age is: " << c->getAge();

return 0;

}


I suspect the output of 1 is because you were passing the address
of a function, for which theres no << overload, so it's probably
being converted to a bool, to which assigning any nonzero value
will result in a value of one.

-Mike
Aug 4 '05 #2
or it's a simple mistake. Instead of
cout << "Cat's Age is: " << c->getAge;
it should be

cout << "Cat's Age is: " << c->getAge();

-haro
Mike Wahler wrote: "Yin99" <ws@ziowave.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I'm expecting to see itsAge = 0 in the output of the following code.
However, in the main() function, itsAge = 1 . Can someone explain??
thanks,

Yin99

===== begin code ======
#include <iostream>
using namespace std;
class Cat
{
public:
Cat(){
itsAge = 0;
cout << "constuctor age is: " << itsAge; //zero here!
}

This is not part of your reported problem, but a constructor
should use an initializer list instead of assigning values to
members after the fact:

Cat() : itsAge(0) {
cout << "constuctor age is: " << itsAge << '\n';
}

void setAge(int age){
itsAge = age;
}

int getAge(){
return itsAge;
}

Again, not part of your problem, but more correct is:

int getAge() const {
return itsAge;
}


private:
int itsAge;
};
int main ()
{

Cat *c = new Cat();

Yet again, not part of your problem, but why are you dynamically
allocating your object? Why not just define it, e.g.:

Cat my_cat;

//c->getAge returns 1 here, Why and Where does 1 come from?
cout << "Cat's Age is: " << c->getAge;

Try:

cout << "Cat's Age is: " << c->getAge();
return 0;

}

I suspect the output of 1 is because you were passing the address
of a function, for which theres no << overload, so it's probably
being converted to a bool, to which assigning any nonzero value
will result in a value of one.

-Mike

Aug 4 '05 #3
thanks! (I should have seen that)

Aug 4 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: alacrite | last post by:
#include <iostream> #include <vector> using namespace std; int binarySearch(vector<int>, int, int, int); int main() { vector<intarrInt;
4
by: duffdevice | last post by:
Hi, I came across this unexpected behavior while working on something else. I am attempting to return a custom type by value from a global function. I have a trace in the custom class's copy...
2
by: fburn | last post by:
I need some help with an error I'm getting using php 5.2.5 running on linux. I receive an error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or...
9
akohistani
by: akohistani | last post by:
I am having Parse error problem with my newly purchased Php upload script I have uploaded the script and I get the error below Parse error: syntax error, unexpected $end in URL/functions.php on...
3
by: broll911 | last post by:
I am gettin a error message on this script can someone help me. I am a complete newbie on this stuff. Thanks in advance for any help you can give me. here is the error. Parse error: syntax error,...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.