zoukankan      html  css  js  c++  java
  • 第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;
    }
  • 相关阅读:
    springboot controller传参,对象映射
    将已有的lng lat 字段转换成point类型字段
    导入csv 到mysql数据库
    spring 数据库字段映射
    spring restTemplate使用方法
    mongo 大数据量更新注意事项
    mongo大数据量更新服务端超时解决: Cursor not found, cursor id: 82792803897
    JS 判断是否为null
    JS 日期格式化
    杨氏矩阵:查找x是否在矩阵中,第K大数
  • 原文地址:https://www.cnblogs.com/yanlixin1993/p/4899820.html
Copyright © 2011-2022 走看看