编程环境为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. linux释放页面缓存drop_caches

    关于drop_caches文件:系统默认为0 在Documentation/sysctl/vm.txt中有如下描述: drop_caches Writing to this will cause th ...

  2. Python3 tkinter基础 Label imag显示图片

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  3. Nginx 配置 Jenkins 反向代理

    安装 Nginx 参考之前的一篇文章 Nginx 安装配置 安装 Jenkins 参考之前的一篇文章 Linux 搭建 Jenkins Nginx 配置 Jenkins 的反向代理 # /etc/ng ...

  4. JWT、OAUTH2与SSO资料补充

    JWT: 阮一峰:http://www.ruanyifeng.com/blog/2018/07/json_web_token-tutorial.html https://blog.csdn.net/q ...

  5. .NET Standard vs. .NET Core

    What is the difference between .NET Core and .NET Standard Class Library project types? Answer1 When ...

  6. size_t和unsigned int区别

    size_t和unsigned int有所不同,size_t的取值range是目标平台下最大可能的数组尺寸,一些平台下size_t的范围小于int的正数范围,又或者大于unsigned int.最典型 ...

  7. How to Install Apache Tomcat 8.5 on CentOS 7.3

    How to Install Apache Tomcat 8.5 on CentOS 7.3 From: https://www.howtoforge.com/tutorial/how-to-inst ...

  8. HDU 6061 RXD and functions(NTT)

    题意 给定一个\(n​\) 次的 \(f​\) 函数,向右移动 \(m​\) 次得到 \(g​\) 函数,第 \(i​\) 次移动长度是 \(a_i​\) ,求 \(g​\) 函数解析式的各项系数,对 ...

  9. HDU 6191 Query on A Tree(可持久化Trie)

    题意 \(n\) 个点的有根树,根为 \(1\) .每个点有点权,有 \(q\) 个询问,每次询问以 \(u\) 为根的子树的点的点权中异或 \(x\) 所得的最大值是多少. 思路 求出整棵树的 \( ...

  10. 【JS】js操作json object

    //将表单序列化成字符串 $.fn.serializeObject = function () { var obj = {}; var count = 0; $.each(this.serialize ...