473,399 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

why this behavious abnormal ?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. class A
  7. {
  8. private:
  9. int a;
  10. int b;
  11.  
  12. public:
  13.  
  14.  
  15.  
  16. A( int i, int j) : a(i), b(j)
  17.  
  18. {
  19. cout << "intitialize a = " << a << endl;
  20. cout << "intitialize b = " << b << endl;
  21.  
  22. }
  23.  
  24.  
  25.  
  26. friend ostream& operator<<(ostream &os, const A &obj)
  27.  
  28. {
  29.  
  30. os << obj.a << obj.b ;
  31.  
  32. return os; // if i forget to return this referance.....
  33.  
  34. }
  35.  
  36. };
  37.  
  38.  
  39.  
  40. int main()
  41.  
  42. {
  43.  
  44.  
  45. A *aobj = new A(5,10);
  46.  
  47. cout<< aobj;
  48.  
  49. cout<<endl;
  50.  
  51.  
  52.  
  53. return 0;
  54.  
  55. }
  56.  
code as shown above...

i am getting :

my o/p : 004800f0

but expected is 510

why ? // i used vc6 IDE
Jun 15 '07 #1
2 1370
#include <iostream>

using namespace std;


class A
{
private:
int a;
int b;

public:



A( int i, int j) : a(i), b(j)

{
cout << "intitialize a = " << a << endl;
cout << "intitialize b = " << b << endl;

}



friend ostream& operator<<(ostream &os, const A &obj)

{

os << obj.a << obj.b ;

return os; // if i forget to return this referance.....

}

};



int main()

{


A *aobj = new A(5,10);

cout<< aobj;

cout<<endl;



return 0;

}

code as shown above...

i am getting :

my o/p : 004800f0

but expected is 510

why ? // i used vc6 IDE
i had fixed this problem.....


in two ways...

1. object from stack...


A aobj(5,10);

cout<< aobj;

2. object from heap...

A *aobj = new A(5,10);

cout<< *aobj;

both o/p will be 5 10
Jun 15 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
I'm glad you fixed it.

This is a common error:
Expand|Select|Wrap|Line Numbers
  1. A *aobj = new A(5,10);
  2.  
  3. cout<< aobj;
  4.  
The address of aobj is displayed. So your fix is correct:

Expand|Select|Wrap|Line Numbers
  1. cout << *aobj;
  2.  
It's a little different with the stack object since stack object's address is &aobj. Therefore, cout << aobj will work. To se the address of the stack objct oyuwould need to cout << &aobj.
Jun 15 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: John Pote | last post by:
Running my programme in Python 2.3.4 I received the following msg in the consol :- (Pent III running W2K prof) """ Exception in Tkinter callback Traceback (most recent call last): File...
1
by: R6_2003 | last post by:
Hello all, i dunno if that's the right newsgroup to ask, but i'll try, please ignore me if u feel its not 0:) i've been messing with a control panel app for so long.. previously i was using...
3
by: Mahmood Ahmad | last post by:
Hello, I have written a program that reads three types of records, validates them acording to certain requirements and writes the valid records into a binary file. The invalid records are...
2
by: Hugh | last post by:
Hello, Apologies if this has already been answered in here and I can't find it, but can anyone help with this problem? I hope the example code and comments state clearly enough what is...
0
by: Alexander F?hrmann | last post by:
Grüß euch! Wir haben hier in der Firma ein Problem mit Access! Sobald man im Access den VBA-Editor aufrufen will, kommt folgender fehler! -------------------- Runtime Error!
0
by: Sun | last post by:
Hi I developed some application with VS-C# on Framework1.1 My app included network, serial communication, ado, GDI+, etc.. and I used many Dll calls. My develop machine is Windows XP...
0
by: zhaopzhi | last post by:
The Bookmarks of VC6.0 become abnormal ! For example ,add the bookmark "1111" successfully ,but can't add the bookmark "11"! Why??? Did you encounter this Who can help me? Thanks!
9
by: ehabaziz2001 | last post by:
I am facing that error message with no idea WHY the reason ? "Abnormal program termination" E:\programs\c_lang\iti01\tc201\ch06\ownarr01o01 Enter a number : 25 More numbers (y/n)? y...
6
by: k3xji | last post by:
Hi all, Is there anyway to detect abnormal interpreter shutdown like (closing from task manager, power shutdown of the PC..etc)? Regards,
0
by: neovantage | last post by:
Hey all, I am working on a page which list categories in box format. I want to show the child categories of the parent category and the items under child category. I am using thickbox technology for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.