编程环境为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. php 腾讯云 对象存储V5版本 获取返回的上传文件的链接方法

    腾讯云 对象存储V5版本 文档地址:https://github.com/tencentyun/cos-php-sdk-v5 调用简单文件上传方法: 返回数据如下 Array ( [data:prot ...

  2. 阿里云centos远程连接mysql

    首先在服务器管理控制台设置防火墙规则 添加规则 使用root登录到mysql 添加一个用户名,权限为%的远程连接用户 grant all on *.* to 'yuancheng'@'%' ident ...

  3. glibc 2.x release note

    glibc 2.x release note,参见: https://sourceware.org/glibc/wiki/Glibc%20Timeline https://www.gnu.org/so ...

  4. 头像上传uploadPreview插件

    原文链接:https://blog.csdn.net/Alisa_L/article/details/52923953 uploadPreview 今天写头像上传,使用到uploadPreview插件 ...

  5. 解决*.props打开失败问题

    由于不同机器的绝对地址不一样,可能会出现解决*.props打开失败问题,解决方向如下: 1.找到这里缺失的.props文件,复制到固定路径下: 2.强行打开代码,这个时候是报错的 3.选择编辑 4.将 ...

  6. ODAC(V9.5.15) 学习笔记(二)控件列表

    ODAC的控件有26个,简单介绍如下: TOraSession  管理Oracle的连接  TOraQuery  使用SQL进行数据获取,自动将更新提交数据库  TSmartQuery    在处理字 ...

  7. P4097 [HEOI2013]Segment(李超树)

    链接 https://www.luogu.org/problemnew/show/P4097 https://www.lydsy.com/JudgeOnline/problem.php?id=3165 ...

  8. 【做题】uoj#370滑稽树上滑稽果——巧妙dp

    一个显然的结论是最终树的形态必然是一条链.具体证明只要考虑选定树上的某一条链,然后把其他部分全部接在它后面,这样答案一定不会变劣. 那么,一开始的想法是考虑每一位的最后出现位置,但这并不容易实现.注意 ...

  9. 分布式知识点总结(来自CS-Notes)

    转载地址:https://github.com/CyC2018/CS-Notes/blob/master/notes/%E5%88%86%E5%B8%83%E5%BC%8F.md 注:如Paxos等的 ...

  10. 【命令】MongoDB常用命令记录

    如果你想创建一个“myTest”的数据库,先运行use myTest命令,之后就做一些操作(如:db.createCollection('user')),这样就可以创建一个名叫“myTest”的数据库 ...