#include<iostream>
using namespace std;
class Student
{
public:
Student()
:a(50),b(60)
{
}
friend ostream &operator<<(ostream &out,Student &stu);
private:
int a;
int b;
};
ostream &operator<<(ostream &out,Student &stu)
{
out<<stu.a<<"--"<<stu.b<<endl;
};
class Gradate:public Student
{
/*按道理上面那个函数是全局的,一个函数可以作为多个友员*/
public:
Gradate()
:c(70)
{
}
private:
int c;
};
int main()
{
/*下面这段代码居然能运行成功,你知道发生了什么行为吗,其实在这个地方其实是
*他把g的数据拷贝到它的父类,这个地方叫做赋值兼容,我把g类的数据赋值给了s
然后s就可以访问这个函数了。这里的输出是 50 60 我如果不想输出50 60
*我想输出70,怎么办呢,看下面的代码
*
* */
Student s;
cout<<s;
Gradate g;
cout<<g;
return 0;
}
#include<iostream>
using namespace std;
class Student
{
public:
Student()
:a(50),b(60)
{
}
friend ostream &operator<<(ostream &out,Student &stu);
private:
int a;
int b;
};
ostream &operator<<(ostream &out,Student &stu)
{
out<<stu.a<<"--"<<stu.b<<endl;
return out;
};
class Gradate:public Student
{
/*按道理上面那个函数是全局的,一个函数可以作为多个友员*/
public:
Gradate()
:c(70)
{
}
friend ostream &operator<<(ostream &out,Gradate &gra);
private:
int c;
};
ostream &operator<<(ostream &out,Gradate &gra)
{
cout<<(Student &)gra<<gra.c<<endl;
return out;
}
int main()
{
/*下面这段代码居然能运行成功,你知道发生了什么行为吗,其实在这个地方其实是
*他把g的数据拷贝到它的父类,这个地方叫做赋值兼容,我把g类的数据赋值给了s
然后s就可以访问这个函数了。这里的输出是 50 60 我如果不想输出50 60
*我想输出70,怎么办呢,看下面的代码
*
* */
Student s;
cout<<s;
Gradate g;
cout<<g;
return 0;
}