P481tabtenn0
编程环境为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的更多相关文章
随机推荐
- tls 双向认证 client端代码例子
example: python import httplib import json import ssl import urllib2 import requests CA_FILE = " ...
- Linux启动报:UNEXPECTED INCONSISTENCY: RUN fsck MANUALLY问题解决
现象: 在此界面输入下root的密码.会进入到修复模式 在修复模式下,输入命令fsck –y /dev/mapper/vg_swnode1-lv_root 这个后面跟的路径就是你上面提示出错的那个路 ...
- 20145320《WEB基础实践》
20145320WEB基础实践 实验问题回答 1.什么是表单 表单可以收集用户的信息和反馈意见,是网站管理者与浏览者之间沟通的桥梁. 一个表单有三个基本组成部分: 表单标签 表单域:包含了文本框.密码 ...
- Hacker
https://hackertarget.com/nikto-website-scanner/
- Python3基础 list pop 取出列表的最后一个元素
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Redis 安装,配置以及数据操作
Nosql介绍 Nosql:一类新出现的数据库(not only sql)的特点 不支持SQL语法 存储结构跟传统关系型数据库中那种关系表完全不同,nosql中存储的数据都是k-v形式 Nosql的世 ...
- CentOS 安装 Redis 笔记
Redis 安装 yum install redis -y 在启动 redis-server 之前,你需要修改配置文件/etc/redis.conf: 找到 bind 127.0.0.1,将其注释,这 ...
- centos 编译lantrn
github上的安装指导: Custom fork of Go is currently required. We'll eventually switch to Go 1.7 which suppo ...
- Docker中的Cgroup Driver:Cgroupfs 与 Systemd
在安装kubernetes的过程中,会出现 failed to create kubelet: misconfiguration: kubelet cgroup driver: "cgrou ...
- 解决Win10 Git图标不显示问题
升级系统到win10 1803版本以后发现TortoiseGit的忽略图标不显示了 开始以为是版本问题,将TortoiseGit版本进行了升级还是不行 网上查找以后发现 Windows Explore ...