<分区>
我正在学习在一个非常简单的程序中重载“<<”,&在我的学习过程中,我发现我的程序有以下令人惊讶的输出。
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class student
{
int age;
public:
student(){}
student(int a){age=a;}
friend ostream &operator<<(ostream &stream,student o);
};
/*operator overloaded in this block*/
ostream &operator<<(ostream &stream,student o)
{
stream<<o.age;
return stream;
}
int main()
{
student ob1(20),ob2(020);
cout<<ob1; /*will yield 20(as desired)*/
cout<<"\n"<<ob2; /*yielding 16(why so)*/
_getch();
return 0;
}
请解释一下