第13章 类继承
2015-10-22 09:13
204 查看
一个普通的类继承,包括构造函数传对象引用和参数列表初始化 #ifndef TABLETENN1_H_ #define TABLETENN1_H_ #include<string> using namespace std; class TableTennisPlayer { private: string firstname; string lastname; bool hasTable; public: TableTennisPlayer(const string &fn="none",const string &ln="none",bool ht=false); void Name()const; bool HasTable()const { return hasTable; } void ResetTable(bool v) { hasTable=v; } }; class RatedPlayer:public TableTennisPlayer { private: int rating; public: RatedPlayer(int r=0,const string &fn="none",const string &ln="none",bool ht=false); RatedPlayer(int r,const TableTennisPlayer &tp); int Rating()const { return rating;} void ResetRating(int r) { rating=r;} }; #endif
#include<iostream> #include"tabtenn1.h" using namespace std; TableTennisPlayer::TableTennisPlayer(const string &fn,const string &ln,bool ht) { firstname=fn; lastname=ln; hasTable=ht; } void TableTennisPlayer::Name()const { cout<<lastname<<", "<<firstname; } RatedPlayer::RatedPlayer(int r,const string &fn,const string &ln,bool ht) { TableTennisPlayer(fn,ln,ht); rating=r; } RatedPlayer::RatedPlayer(int r, const TableTennisPlayer &tp):TableTennisPlayer(tp) { rating=r; }
#include<iostream> #include"tabtenn1.h" using namespace std; int main() { TableTennisPlayer player1("Tara","Boomdea",false); RatedPlayer rplayer1(1140,"Maloory","Duck",true); rplayer1.Name(); if(rplayer1.HasTable()) cout<<" has a table!"<<endl; else cout<<" hasn't a table!"<<endl; player1.Name(); if(player1.HasTable()) cout<<" has a table!"<<endl; else cout<<" hasn't a table!"<<endl; cout<<"Name:"; rplayer1.Name(); cout<<";Rating: "<<rplayer1.Rating(); RatedPlayer rplayer2(1212,player1); cout<<"Name: "; rplayer2.Name(); cout<<";Rating: "<<rplayer2.Rating()<<endl; return 0; }
相关文章推荐
- lua学习(二)
- Xcode中的一些快捷键
- 银行真的是这样?还是一个地方一个样?
- Kickdown
- iphone6 iphone6 plus 放大显示模式高分辨率模式问题
- 编译中遇到的小问题
- 学习socket nio 之 mina实例
- easyUI datagrid列字段添加按钮
- sqlserver 游标用法
- linux find grep组合使用
- [国嵌笔记][029][ARM处理器启动流程分析v2]
- 当电桥为恒流源时惠斯通电桥电压的计算方法
- 现有的一些人脸数据库
- MySQL系列二
- 系统调用
- android 的SDK的更新
- 【Unity Shaders】Surface Shader 概述
- [Mathworks Matlab][R2014a]安装过程存档
- (转)大型网站架构演化发展历程
- 【scoi 2011】糖果 差分约束