using operator<<() , how to display base class members?

#include<iostream>
using namespace std;

class A {

private:
int a ;
int b;
public:
A(int a1=0, int b1=0):a(a1),b(b1){
cout <<"A's constructor is called \n";
}

friend ostream& operator<<(ostream &dout, A& d)
{
dout << "\n a= "<< d.a<<" b= "<< d.b<< "\n";
return dout;
}

};
class B:public A
{
private:
int c;
int d;
public:
B(int c1=0, int d1=0):c(c1),d(d1){
cout <<"B's constructor is called \n";
}

friend ostream& operator<<(ostream &dout, B& d1)
{
dout << "\n c= "<< d1.c <<" d= "<< d1.d<< "\n";
return dout;
}

};

int main()
{
B x(10,20);

cout<<x; // using this i want to display inherited a & b also .. HOW?
return 0;
}
If you implement B::operator<<(...) to call
A::operator<<(dout, d1);
it should have the effect you are looking for.

--Rollie
cout << (A&)x;
Topic archived. No new replies allowed.