• 第13章 类继承


    一个普通的类继承,包括构造函数传对象引用和参数列表初始化
    #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;
    }
  • 相关阅读:
    CentOS同步时间
    使用dnsmasq来提升CentOS上网速度
    bash的变量设置
    CentOS找回root密码
    知识学习网站
    webservice接口测试,使用SoapUI工具进行接口测试
    js中字符串转换为数字
    css颜色大全
    Table分页显示调整
    iframe中,页面转换后回到页面的顶部
  • 原文地址:https://www.cnblogs.com/yanlixin1993/p/4899820.html
Copyright © 2020-2023  润新知