多态性典型案例.doc
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《多态性典型案例.doc》由会员分享,可在线阅读,更多相关《多态性典型案例.doc(7页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、第八章 多态性典型案例【案例8-1】编写矩形类重载成员函数#include#include#includeusing namespace std;class CRectlong left; long top;long right; long bottom;public: CRect(int l=1, int t=2, int r=3, int b=4);void SetRect(int x1, int y1, int x2, int y2); /重载的成员函数void SetRect(const CRect& r) ; /重载的成员函数 void Disp();void CRect:Disp(
2、) /成员函数显示自身的数据coutleft=left; top=top; right=right; bottom=right = right;this-bottom = bottom;int main()CRect r,s; /调用构造函数CRect(1, 2, 3, 4)定义对象r.Disp(); /r对象显示数据r.SetRect(5,6,7,8); s.SetRect(r); /调用重载的成员函数 s.Disp(); /显示s对象数据return 0; 【案例8-2】电话号码类PhoneNumber重载流提取与流插入运算符#include #include using namespac
3、e std;class PhoneNumber /重载流插入操作符,如果需要使用cout somePhoneNumber;则不能重载为成员函数friend ostream &operator( ostream &output, const PhoneNumber &num ) output ( num.areaCode ) num.exchange - somePhoneNumber;则不能重载为成员函数friend istream &operator( istream &input, PhoneNumber &num )input.ignore(); input setw( 4 ) num.
4、areaCode; input.ignore( 2 ); input setw( 4 ) num.exchange; input.ignore(); input setw( 5 ) num.line; return input; private: char areaCode4,exchange4,line5; ; int main() PhoneNumber phone; /创建对象phone cout phone; /语句1:使用流提取运算符 cout The phone number entered was: phone endl; /语句2:使用流插入运算符 return 0;【案例8-
5、3】编写词汇类Words重载下标操作符#include #include using namespace std;class Words char *str;public:Words (char *s) str=new charstrlen (s) +1 ; strcpy(str, s) ; void disp() cout str =0) word n-1 =word n-1 -32 ; /语句2:字母转化为大写 n-; word.disp () ;return 0;【案例8-4】显示和隐式调用虚函数#include using namespace std;class Base public
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 多态性 典型 案例
![提示](https://www.deliwenku.com/images/bang_tan.gif)
限制150内