为什么以下程序运行是结果的第六行是“PenStyler is 0",而不是我在程序中写的
"PenStyler is 虚线."
#include<iostream.h>
#include<Afx.h>
class CPen
{ private:
CString Color;
protected:
int Styler;
public:
int Width;
void PenInit(int,int,CString);
void PenInfo();
virtual void StyleInfo()
{cout<<"PenStyler is "<<Styler<<endl;
}
};
class CSubPen:public CPen
{ public:
void WidthInfo()
{ cout<<"PenWidth is "<<Width<<endl;
}
void WidthInfo(CString kk)
{ cout<<"PenWidth is "<<Width<<" "<<kk<<endl;
}
virtual void StylerInfo()
{ cout<<"PenStyler is ÐéÏß."<<endl;
}
};
void CPen::PenInit(int kd,int fg,CString ys)
{ Color=ys;
Styler=fg;
Width=kd;
}
void CPen::PenInfo()
{ cout<<"PenWidth: "<<Width<<" PenColor: "<<Color<<" PenStyler: "<<Styler<<endl;
}
void main()
{ CPen pen,*pp;
CSubPen subpen;
pen.PenInit(30,1,"blue");
pen.PenInfo();
subpen.PenInit(20,0,"black");
subpen.PenInfo();
subpen.WidthInfo();
subpen.WidthInfo("ÏóËØ");
pp=&pen;
pp->StyleInfo();
pp=&subpen;
pp->StyleInfo();
}
"PenStyler is 虚线."
#include<iostream.h>
#include<Afx.h>
class CPen
{ private:
CString Color;
protected:
int Styler;
public:
int Width;
void PenInit(int,int,CString);
void PenInfo();
virtual void StyleInfo()
{cout<<"PenStyler is "<<Styler<<endl;
}
};
class CSubPen:public CPen
{ public:
void WidthInfo()
{ cout<<"PenWidth is "<<Width<<endl;
}
void WidthInfo(CString kk)
{ cout<<"PenWidth is "<<Width<<" "<<kk<<endl;
}
virtual void StylerInfo()
{ cout<<"PenStyler is ÐéÏß."<<endl;
}
};
void CPen::PenInit(int kd,int fg,CString ys)
{ Color=ys;
Styler=fg;
Width=kd;
}
void CPen::PenInfo()
{ cout<<"PenWidth: "<<Width<<" PenColor: "<<Color<<" PenStyler: "<<Styler<<endl;
}
void main()
{ CPen pen,*pp;
CSubPen subpen;
pen.PenInit(30,1,"blue");
pen.PenInfo();
subpen.PenInit(20,0,"black");
subpen.PenInfo();
subpen.WidthInfo();
subpen.WidthInfo("ÏóËØ");
pp=&pen;
pp->StyleInfo();
pp=&subpen;
pp->StyleInfo();
}
pp=&subpen;
可以看作是
pp=(CPen*)&subpen;
virtual void StylerInfo()
{ cout<<"PenStyler is ÐéÏß."<<endl;
}
是不是把基类的StyleInfo多写了一个"r"