编程环境为Qt Creator 4.0.3 (Community)

tabtenn0.h

#ifndef TABTENN0_H
#define TABTENN0_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 ResetTabel(bool v) {hasTable=v;}
};
//简单的继承类
class RatePlayer:public TableTennisPlayer
{
private:
unsigned int rating;
public:
RatePlayer(unsigned int r=0,const string &fn="none",
const string &ln="none",bool ht=false); //继承类的构造函数
RatePlayer(unsigned int r,const TableTennisPlayer &tp);
unsigned int Rating() const{return rating;}
void ResetRating(unsigned int r){rating=r;}
};
#endif // TABTENN0_H

main.cpp

#include <iostream>
#include "tabtenn0.h" using namespace std; void Show(const TableTennisPlayer &rt); TableTennisPlayer::TableTennisPlayer(const string &fn,
const string &ln, bool ht):firstname(fn),lastname(ln),hasTable(ht){} void TableTennisPlayer::Name() const
{
std::cout<<lastname<<","<<firstname;
}
RatePlayer::RatePlayer(unsigned int r,
const string &fn,
const string &ln, bool ht):TableTennisPlayer(fn,ln,ht)
{
rating=r;
}
RatePlayer::RatePlayer(unsigned int r,
const TableTennisPlayer &tp):TableTennisPlayer(tp),rating(r)
{ }
int main(int argc, char *argv[])
{ cout << "Hello World!" << endl;
TableTennisPlayer player1("Tara","Boomdea",false);
RatePlayer rplayer1(1140,"Mallory","Duck",true);
rplayer1.Name(); //继承的类使用基类的方法
if(rplayer1.HasTable())
{
cout<<":has a table\n"<<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;
}
//使用基类的对象初始化继承类
RatePlayer rplayer2(1212,player1);
cout<<"Name:";
rplayer2.Name();
cout<<": Rating:"<<rplayer2.Rating()<<endl;
Show(player1);
Show(rplayer1);
//将基类对象初始化为派生类对象
RatePlayer olaf1(1840,"Olaf","Loaf",true); //派生类对象
TableTennisPlayer olaf2(olaf1); //基类对象
olaf2.Name(); return 0;
}
void Show(const TableTennisPlayer &rt)
{
using std::cout;
cout<<"Name:";
rt.Name();
cout<<"\nTable:";
if(rt.HasTable())
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}
}

运行结果如下

P481tabtenn0的更多相关文章

随机推荐

  1. Nodejs的npm安装模块时候报错:npm ERR! Error: CERT_UNTRUSTED的解决方法

    npm http GET https://registry.npmjs.org/grunt-cli npm http GET https://registry.npmjs.org/grunt-cli ...

  2. linux中权限对文件和目录的意义

    1.权限对文件的意义: 读:可查看文件的内容 写:可修改文件的内容(但不能删除文件) 执行:可执行文件 2.权限对目录的意义: 读:可以查看目录下的内容,即可以读取该目录下的结构列表 写:可修改目录下 ...

  3. 电影编码JPEG2000与H.264

    电影的第三次革命是数字电影的诞生,数字电影取代了胶片,那么数字电影就一定有其独特的封装(压缩)格式.在网络上,我们经常见到许多视频格式,诸如mp4.mkv.flv.rmvb等,这些都是在通用计算机上播 ...

  4. 20165211 2017-2018-2 《Java程序设计》课程总结

    20165211 2017-2018-2 <Java程序设计>课程总结 一.每周作业及实验报告博客链接汇总 预备作业1:我期望的师生关系 预备作业2:学习基础和C语言调查 预备作业3:Li ...

  5. python --- 05 字典 集合

    一.字典 可变数据类型 {key:value}形式   查找效率高   key值必须是不可变的数据类型 1.增删改查 1).增    dic["新key"] = "新va ...

  6. Maven3版本的超级POM位置及中央仓库位置

    背景 之所以想到这个问题,是因为在配置Nexus-Maven 私服的时候,需要在Maven的settings.xml中对<mirror>进行配置,在配置中央仓库的镜像时,<mirro ...

  7. FJUT3568 中二病也要敲代码(线段树维护区间连续最值)题解

    题意:有一个环,有1~N编号,m次操作,将a位置的值改为b,问你这个环当前最小连续和多少(不能全取也不能不取) 思路:用线段树维护一个区间最值连续和.我们设出两个变量Lmin,Rmin,Mmin表示区 ...

  8. 【重新分配分片】Elasticsearch通过reroute api重新分配分片

    elasticsearch可以通过reroute api来手动进行索引分片的分配. 不过要想完全手动,必须先把cluster.routing.allocation.disable_allocation ...

  9. sed 替换换行回车

    A carriage return linefeed (CRLF) is a special sequence of characters, used by DOS/Windows, to signi ...

  10. ifconfig 输出里没有IP地址

    转载: http://blog.csdn.net/johnstrive/article/details/5625121 inet addr:....Bcast:.....Mask:255.255.25 ...